Skip to content

Commit 95486e7

Browse files
committed
refactor: Extract default Authorization header setup into a helper
1 parent 19d2b72 commit 95486e7

2 files changed

Lines changed: 9 additions & 6 deletions

File tree

src/apify_client/_apify_client.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,8 @@ def call(self, *, method, url, **kwargs) -> HttpResponse:
253253
http_client: A custom HTTP client instance extending `HttpClient`.
254254
"""
255255
instance = cls(token=token, api_url=api_url, api_public_url=api_public_url)
256-
client_headers = http_client._headers # noqa: SLF001
257-
if token is not None and not any(key.title() == 'Authorization' for key in client_headers):
258-
client_headers['Authorization'] = f'Bearer {token}'
256+
if token is not None:
257+
http_client._set_default_authorization(token) # noqa: SLF001
259258
instance._http_client = http_client
260259
return instance
261260

@@ -616,9 +615,8 @@ async def call(self, *, method, url, **kwargs) -> HttpResponse:
616615
http_client: A custom HTTP client instance extending `HttpClientAsync`.
617616
"""
618617
instance = cls(token=token, api_url=api_url, api_public_url=api_public_url)
619-
client_headers = http_client._headers # noqa: SLF001
620-
if token is not None and not any(key.title() == 'Authorization' for key in client_headers):
621-
client_headers['Authorization'] = f'Bearer {token}'
618+
if token is not None:
619+
http_client._set_default_authorization(token) # noqa: SLF001
622620
instance._http_client = http_client
623621
return instance
624622

src/apify_client/http_clients/_base.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ def __init__(
145145

146146
self._headers = {**default_headers, **(headers or {})}
147147

148+
def _set_default_authorization(self, token: str) -> None:
149+
"""Set the `Authorization` header from the token, unless an authorization header is already configured."""
150+
if not any(key.title() == 'Authorization' for key in self._headers):
151+
self._headers['Authorization'] = f'Bearer {token}'
152+
148153
@staticmethod
149154
def _parse_params(params: dict[str, Any] | None) -> dict[str, Any] | None:
150155
"""Convert request parameters to Apify API-compatible formats.

0 commit comments

Comments
 (0)