Skip to content

python: Enable configuring proxies in SvixOptions #1928

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion python/svix/api/client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ssl
from typing import Dict, List, Union
from typing import Dict, List, Union, Optional

import attr

Expand Down Expand Up @@ -60,6 +60,7 @@ class AuthenticatedClient(Client):
token: str
prefix: str = "Bearer"
auth_header_name: str = "Authorization"
proxy: Optional[str] = attr.ib(default=None)

def get_headers(self) -> Dict[str, str]:
"""Get headers to be used in authenticated endpoints"""
Expand Down
23 changes: 20 additions & 3 deletions python/svix/api/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,29 @@ class ApiBase:

def __init__(self, client: AuthenticatedClient) -> None:
self._client = client

if self._client.proxy is not None:
proxy_mounts = {
"http://": httpx.HTTPTransport(proxy=httpx.Proxy(self._client.proxy)),
"https://": httpx.HTTPTransport(proxy=httpx.Proxy(self._client.proxy)),
}
async_proxy_mounts = {
"http://": httpx.AsyncHTTPTransport(
proxy=httpx.Proxy(self._client.proxy)
),
"https://": httpx.AsyncHTTPTransport(
proxy=httpx.Proxy(self._client.proxy)
),
}
else:
proxy_mounts = None
async_proxy_mounts = None

self._httpx_client = httpx.Client(
verify=client.verify_ssl, cookies=self._client.get_cookies()
mounts=proxy_mounts, cookies=self._client.get_cookies()
)
self._httpx_async_client = httpx.AsyncClient(
verify=client.verify_ssl, cookies=self._client.get_cookies()
mounts=async_proxy_mounts, cookies=self._client.get_cookies()
)

def _get_httpx_kwargs(
Expand Down Expand Up @@ -168,7 +186,6 @@ def _request_sync(
header_params=header_params,
json_body=json_body,
)

response = self._httpx_client.request(**httpx_kwargs)
for retry_count, sleep_time in enumerate(self._client.retry_schedule):
if response.status_code < 500:
Expand Down
3 changes: 3 additions & 0 deletions python/svix/api/svix.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class SvixOptions:
"""
timeout: float = 15.0

proxy: t.Optional[str] = None


class ClientBase:
_client: AuthenticatedClient
Expand Down Expand Up @@ -78,6 +80,7 @@ def __init__(self, auth_token: str, options: SvixOptions = SvixOptions()) -> Non
timeout=options.timeout,
follow_redirects=False,
raise_on_unexpected_status=True,
proxy=options.proxy,
)
self._client = client

Expand Down