Quellcode durchsuchen

Add urlencoding to the path in ApiProvider

Casper van der Wel vor 2 Jahren
Ursprung
Commit
ac135cf9da

+ 2 - 1
clean_python/api_client/api_provider.py

@@ -3,6 +3,7 @@ import re
 from http import HTTPStatus
 from typing import Callable
 from typing import Optional
+from urllib.parse import quote
 from urllib.parse import urlencode
 from urllib.parse import urljoin
 
@@ -85,7 +86,7 @@ class SyncApiProvider:
         headers = {}
         request_kwargs = {
             "method": method,
-            "url": add_query_params(join(self._url, path), params),
+            "url": add_query_params(join(self._url, quote(path)), params),
             "timeout": timeout,
         }
         # for urllib3<2, we dump json ourselves

+ 5 - 0
integration_tests/fastapi_example/presentation.py

@@ -13,6 +13,7 @@ from clean_python.fastapi import delete
 from clean_python.fastapi import get
 from clean_python.fastapi import patch
 from clean_python.fastapi import post
+from clean_python.fastapi import put
 from clean_python.fastapi import RequestQuery
 from clean_python.fastapi import Resource
 from clean_python.fastapi import v
@@ -68,3 +69,7 @@ class V1Books(Resource, version=v(1), name="books"):
     @post("/file")
     async def file(self, file: UploadFile):
         return {file.filename: (await file.read()).decode()}
+
+    @put("/urlencode/{name}", response_model=Author)
+    async def urlencode(self, name: str):
+        return {"name": name}

+ 7 - 0
integration_tests/test_api_provider.py

@@ -82,3 +82,10 @@ def test_no_json_response(provider: SyncApiProvider):
         provider.request("GET", "v1/text")
 
     assert e.value.args[0] == "Unexpected content type 'text/plain; charset=utf-8'"
+
+
+def test_urlencode(provider: SyncApiProvider):
+    response = provider.request("PUT", "v1/urlencode/x?")
+
+    assert isinstance(response, dict)
+    assert response["name"] == "x?"