Casper van der Wel 1 anno fa
parent
commit
5237d590c4

+ 3 - 1
CHANGES.md

@@ -4,7 +4,9 @@
 0.6.6 (unreleased)
 ------------------
 
-- Nothing changed yet.
+- Fix blocking behaviour of `fetch_token` in `ApiProvider`.
+
+- Fix missing `api_client.Response`.
 
 
 0.6.5 (2023-10-04)

+ 1 - 0
clean_python/api_client/__init__.py

@@ -2,4 +2,5 @@ from .api_gateway import *  # NOQA
 from .api_provider import *  # NOQA
 from .exceptions import *  # NOQA
 from .files import *  # NOQA
+from .response import *  # NOQA
 from .sync_api_provider import *  # NOQA

+ 3 - 2
clean_python/api_client/api_provider.py

@@ -1,6 +1,7 @@
 import asyncio
 import re
 from http import HTTPStatus
+from typing import Awaitable
 from typing import Callable
 from typing import Optional
 from urllib.parse import quote
@@ -69,7 +70,7 @@ class ApiProvider:
     def __init__(
         self,
         url: AnyHttpUrl,
-        fetch_token: Callable[[ClientSession, int], Optional[str]],
+        fetch_token: Callable[[ClientSession, int], Awaitable[Optional[str]]],
         retries: int = 3,
         backoff_factor: float = 1.0,
     ):
@@ -99,7 +100,7 @@ class ApiProvider:
             "json": json,
             "data": fields,
         }
-        token = self._fetch_token(self._session, ctx.tenant.id)
+        token = await self._fetch_token(self._session, ctx.tenant.id)
         if token is not None:
             headers["Authorization"] = f"Bearer {token}"
         for attempt in range(self._retries):

+ 5 - 1
integration_tests/test_int_api_gateway.py

@@ -12,10 +12,14 @@ class BooksGateway(ApiGateway, path="v1/books/{id}"):
     pass
 
 
+async def fake_token(a, b):
+    return "token"
+
+
 @pytest.fixture
 def provider(fastapi_example_app) -> ApiProvider:
     ctx.tenant = Tenant(id=2, name="")
-    yield ApiProvider(fastapi_example_app + "/", lambda a, b: "token")
+    yield ApiProvider(fastapi_example_app + "/", fake_token)
     ctx.tenant = None
 
 

+ 5 - 1
integration_tests/test_int_api_provider.py

@@ -8,10 +8,14 @@ from clean_python.api_client import ApiException
 from clean_python.api_client import ApiProvider
 
 
+async def fake_token(a, b):
+    return "token"
+
+
 @pytest.fixture
 def provider(fastapi_example_app) -> ApiProvider:
     ctx.tenant = Tenant(id=2, name="")
-    yield ApiProvider(fastapi_example_app + "/", lambda a, b: "token")
+    yield ApiProvider(fastapi_example_app + "/", fake_token)
     ctx.tenant = None
 
 

+ 10 - 2
tests/api_client/test_api_provider.py

@@ -12,6 +12,14 @@ from clean_python.api_client import ApiProvider
 MODULE = "clean_python.api_client.api_provider"
 
 
+async def fake_token(a, b):
+    return f"tenant-{b}"
+
+
+async def no_token(a, b):
+    return None
+
+
 @pytest.fixture
 def tenant() -> Tenant:
     ctx.tenant = Tenant(id=2, name="")
@@ -36,7 +44,7 @@ def api_provider(tenant, response) -> ApiProvider:
     with mock.patch.object(ClientSession, "request", new=request):
         api_provider = ApiProvider(
             url="http://testserver/foo/",
-            fetch_token=lambda a, b: f"tenant-{b}",
+            fetch_token=fake_token,
         )
         api_provider._session.request.return_value = response
         yield api_provider
@@ -139,6 +147,6 @@ async def test_error_response(api_provider: ApiProvider, response, status):
 
 
 async def test_no_token(api_provider: ApiProvider):
-    api_provider._fetch_token = lambda a, b: None
+    api_provider._fetch_token = no_token
     await api_provider.request("GET", "")
     assert api_provider._session.request.call_args[1]["headers"] == {}