File tree Expand file tree Collapse file tree 2 files changed +46
-2
lines changed
Expand file tree Collapse file tree 2 files changed +46
-2
lines changed Original file line number Diff line number Diff line change @@ -193,6 +193,9 @@ class AuthenticatedClient:
193193 prefix : str = "Bearer"
194194 auth_header_name : str = "Authorization"
195195
196+ api_prefix : str = "python-server"
197+ api_header_name : str = "x-fishjam-api-client"
198+
196199 def with_headers (self , headers : dict [str , str ]) -> "AuthenticatedClient" :
197200 """Get a new client matching this one with additional headers"""
198201 if self ._client is not None :
@@ -231,7 +234,7 @@ def get_httpx_client(self) -> httpx.Client:
231234 self ._headers [self .auth_header_name ] = (
232235 f"{ self .prefix } { self .token } " if self .prefix else self .token
233236 )
234- self ._headers ["x-sdk_version" ] = f"py -{ get_version ()} "
237+ self ._headers [self . api_header_name ] = f"{ self . api_prefix } -{ get_version ()} "
235238 self ._client = httpx .Client (
236239 base_url = self ._base_url ,
237240 cookies = self ._cookies ,
@@ -268,7 +271,7 @@ def get_async_httpx_client(self) -> httpx.AsyncClient:
268271 self ._headers [self .auth_header_name ] = (
269272 f"{ self .prefix } { self .token } " if self .prefix else self .token
270273 )
271- self ._headers ["x-sdk_version" ] = f"py -{ get_version ()} "
274+ self ._headers [self . api_header_name ] = f"{ self . api_prefix } -{ get_version ()} "
272275 self ._async_client = httpx .AsyncClient (
273276 base_url = self ._base_url ,
274277 cookies = self ._cookies ,
Original file line number Diff line number Diff line change 1+ import pytest
2+
3+ from fishjam ._openapi_client .client import AuthenticatedClient
4+ from fishjam .version import get_version
5+
6+
7+ def test_authenticated_client_sets_sdk_and_auth_headers_sync ():
8+ client = AuthenticatedClient (
9+ base_url = "https://example.com" ,
10+ token = "token123" ,
11+ headers = {"custom" : "value" },
12+ )
13+
14+ httpx_client = client .get_httpx_client ()
15+ try :
16+ headers = httpx_client .headers
17+
18+ assert headers [client .auth_header_name ] == "Bearer token123"
19+ assert headers [client .api_header_name ] == f"{ client .api_prefix } -{ get_version ()} "
20+ assert headers ["custom" ] == "value"
21+ finally :
22+ httpx_client .close ()
23+
24+
25+ @pytest .mark .asyncio
26+ async def test_authenticated_client_sets_sdk_and_auth_headers_async ():
27+ client = AuthenticatedClient (
28+ base_url = "https://example.com" ,
29+ token = "token456" ,
30+ headers = {"another" : "header" },
31+ )
32+
33+ async_client = client .get_async_httpx_client ()
34+ try :
35+ headers = async_client .headers
36+
37+ assert headers [client .auth_header_name ] == "Bearer token456"
38+ assert headers [client .api_header_name ] == f"{ client .api_prefix } -{ get_version ()} "
39+ assert headers ["another" ] == "header"
40+ finally :
41+ await async_client .aclose ()
You can’t perform that action at this time.
0 commit comments