Skip to content

python: Reuse the same httpx client #1824

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 18, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions python/svix/api/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,17 @@ class PostOptions(BaseOptions):

class ApiBase:
_client: AuthenticatedClient
_httpx_client: httpx.Client
_httpx_async_client: httpx.AsyncClient

def __init__(self, client: AuthenticatedClient) -> None:
self._client = client
self._httpx_client = httpx.Client(
verify=client.verify_ssl, cookies=self._client.get_cookies()
)
self._httpx_async_client = httpx.AsyncClient(
verify=client.verify_ssl, cookies=self._client.get_cookies()
)

def _get_httpx_kwargs(
self,
Expand All @@ -89,7 +97,7 @@ def _get_httpx_kwargs(

headers: t.Dict[str, str] = {
**self._client.get_headers(),
"svix-req-id": f"{random.getrandbits(32)}",
"svix-req-id": f"{random.getrandbits(64)}",
}
if header_params is not None:
headers.update(header_params)
Expand All @@ -98,10 +106,8 @@ def _get_httpx_kwargs(
"method": method.upper(),
"url": url,
"headers": headers,
"cookies": self._client.get_cookies(),
"timeout": self._client.get_timeout(),
"follow_redirects": self._client.follow_redirects,
"verify": self._client.verify_ssl,
}

if query_params is not None:
Expand Down Expand Up @@ -133,18 +139,15 @@ async def _request_asyncio(
json_body=json_body,
)

response = httpx.request(**httpx_kwargs)
response = await self._httpx_async_client.request(**httpx_kwargs)

async with httpx.AsyncClient(verify=self._client.verify_ssl) as _client:
response = await _client.request(**httpx_kwargs)

for retry_count, sleep_time in enumerate(self._client.retry_schedule):
if response.status_code < 500:
break
for retry_count, sleep_time in enumerate(self._client.retry_schedule):
if response.status_code < 500:
break

await asyncio.sleep(sleep_time)
httpx_kwargs["headers"]["svix-retry-count"] = str(retry_count)
response = await _client.request(**httpx_kwargs)
await asyncio.sleep(sleep_time)
httpx_kwargs["headers"]["svix-retry-count"] = str(retry_count)
response = await self._httpx_async_client.request(**httpx_kwargs)

return _filter_response_for_errors_response(response)

Expand All @@ -166,14 +169,14 @@ def _request_sync(
json_body=json_body,
)

response = httpx.request(**httpx_kwargs)
response = self._httpx_client.request(**httpx_kwargs)
for retry_count, sleep_time in enumerate(self._client.retry_schedule):
if response.status_code < 500:
break

time.sleep(sleep_time)
httpx_kwargs["headers"]["svix-retry-count"] = str(retry_count)
response = httpx.request(**httpx_kwargs)
response = self._httpx_client.request(**httpx_kwargs)

return _filter_response_for_errors_response(response)

Expand Down