Skip to content

Commit 51426b4

Browse files
committed
python: Enable configuring proxies in SvixOptions
1 parent 72d7e4d commit 51426b4

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

python/svix/api/client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import ssl
2-
from typing import Dict, List, Union
2+
from typing import Dict, List, Union, Optional
33

44
import attr
55

@@ -60,6 +60,7 @@ class AuthenticatedClient(Client):
6060
token: str
6161
prefix: str = "Bearer"
6262
auth_header_name: str = "Authorization"
63+
proxy: Optional[str] = attr.ib(default=None)
6364

6465
def get_headers(self) -> Dict[str, str]:
6566
"""Get headers to be used in authenticated endpoints"""

python/svix/api/common.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,29 @@ class ApiBase:
7575

7676
def __init__(self, client: AuthenticatedClient) -> None:
7777
self._client = client
78+
79+
if self._client.proxy is not None:
80+
proxy_mounts = {
81+
"http://": httpx.HTTPTransport(proxy=httpx.Proxy(self._client.proxy)),
82+
"https://": httpx.HTTPTransport(proxy=httpx.Proxy(self._client.proxy)),
83+
}
84+
async_proxy_mounts = {
85+
"http://": httpx.AsyncHTTPTransport(
86+
proxy=httpx.Proxy(self._client.proxy)
87+
),
88+
"https://": httpx.AsyncHTTPTransport(
89+
proxy=httpx.Proxy(self._client.proxy)
90+
),
91+
}
92+
else:
93+
proxy_mounts = None
94+
async_proxy_mounts = None
95+
7896
self._httpx_client = httpx.Client(
79-
verify=client.verify_ssl, cookies=self._client.get_cookies()
97+
mounts=proxy_mounts, cookies=self._client.get_cookies()
8098
)
8199
self._httpx_async_client = httpx.AsyncClient(
82-
verify=client.verify_ssl, cookies=self._client.get_cookies()
100+
mounts=async_proxy_mounts, cookies=self._client.get_cookies()
83101
)
84102

85103
def _get_httpx_kwargs(
@@ -168,7 +186,6 @@ def _request_sync(
168186
header_params=header_params,
169187
json_body=json_body,
170188
)
171-
172189
response = self._httpx_client.request(**httpx_kwargs)
173190
for retry_count, sleep_time in enumerate(self._client.retry_schedule):
174191
if response.status_code < 500:

python/svix/api/svix.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class SvixOptions:
4545
"""
4646
timeout: float = 15.0
4747

48+
proxy: t.Optional[str] = None
4849

4950
class ClientBase:
5051
_client: AuthenticatedClient
@@ -78,6 +79,7 @@ def __init__(self, auth_token: str, options: SvixOptions = SvixOptions()) -> Non
7879
timeout=options.timeout,
7980
follow_redirects=False,
8081
raise_on_unexpected_status=True,
82+
proxy=options.proxy,
8183
)
8284
self._client = client
8385

0 commit comments

Comments
 (0)