nextlayer-sdk-python 1.1.0__tar.gz → 1.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: nextlayer-sdk-python
3
- Version: 1.1.0
3
+ Version: 1.2.0
4
4
  Summary: Client utilities to interact with next layer public APIs
5
5
  Author: Wolfgang Powisch
6
6
  Author-email: wolfgang.powisch@nextlayer.at
@@ -3,7 +3,7 @@ name = "nextlayer-sdk-python"
3
3
  packages = [
4
4
  { include = "nextlayer", from="src" }
5
5
  ]
6
- version = "v1.1.0"
6
+ version = "1.2.0"
7
7
  description = "Client utilities to interact with next layer public APIs"
8
8
  readme = "README.md"
9
9
  authors = ["Wolfgang Powisch <wolfgang.powisch@nextlayer.at>"]
@@ -1,2 +1,2 @@
1
1
  # do not edit this version line, it will be updated in CI-Pipeline!
2
- __version__ = "v1.1.0"
2
+ __version__ = "v1.2.0"
@@ -0,0 +1,35 @@
1
+ import asyncio
2
+ from typing import AsyncGenerator, Generator
3
+
4
+ import httpx
5
+
6
+ from .auth import NlAuth
7
+
8
+
9
+ class NlHttpxAuth(httpx.Auth):
10
+ def __init__(self, *args, **kwargs):
11
+ if len(args) == 1 and len(kwargs) == 0 and isinstance(args[0], NlAuth):
12
+ # pass an existing NlAuth instance directly
13
+ self.nlauth = args[0]
14
+ else:
15
+ # pass all arguments to NlAuth constructor and create NlAuth instance internally
16
+ self.nlauth = NlAuth(*args, **kwargs)
17
+ self._lock = asyncio.Lock()
18
+
19
+ def auth_flow(
20
+ self, request: httpx.Request
21
+ ) -> Generator[httpx.Request, httpx.Response, None]:
22
+ request.headers["Authorization"] = "Bearer " + self.nlauth.get_access_token()
23
+ yield request
24
+
25
+ async def async_auth_flow(
26
+ self, request: httpx.Request
27
+ ) -> AsyncGenerator[httpx.Request, httpx.Response]:
28
+ # NlAuth.get_access_token() does blocking file I/O and, on refresh/login,
29
+ # a blocking HTTP call to Keycloak - offload it so it doesn't stall the
30
+ # event loop. The lock avoids concurrent coroutines all triggering
31
+ # redundant refresh/login calls when the token expires under load.
32
+ async with self._lock:
33
+ access_token = await asyncio.to_thread(self.nlauth.get_access_token)
34
+ request.headers["Authorization"] = "Bearer " + access_token
35
+ yield request
@@ -1,21 +0,0 @@
1
- from typing import Generator
2
-
3
- import httpx
4
-
5
- from .auth import NlAuth
6
-
7
-
8
- class NlHttpxAuth(httpx.Auth):
9
- def __init__(self, *args, **kwargs):
10
- if len(args) == 1 and len(kwargs) == 0 and isinstance(args[0], NlAuth):
11
- # pass an existing NlAuth instance directly
12
- self.nlauth = args[0]
13
- else:
14
- # pass all arguments to NlAuth constructor and create NlAuth instance internally
15
- self.nlauth = NlAuth(*args, **kwargs)
16
-
17
- def auth_flow(
18
- self, request: httpx.Request
19
- ) -> Generator[httpx.Request, httpx.Response, None]:
20
- request.headers["Authorization"] = "Bearer " + self.nlauth.get_access_token()
21
- yield request