Skip to content

Commit

Permalink
Adding support for http/s proxies
Browse files Browse the repository at this point in the history
  • Loading branch information
tylern4 committed Oct 9, 2024
1 parent 5c8bc9d commit 07eff60
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
1 change: 1 addition & 0 deletions scripts/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def run_unasync():
"AsyncJobSacct": "JobSacct",
"AsyncJobSqueue": "JobSqueue",
"AsyncOAuth2Client": "OAuth2Client",
"AsyncHTTPTransport": "HTTPTransport",
"aclose": "close",
"_ASYNC_SLEEP": "_SLEEP",
}
Expand Down
9 changes: 8 additions & 1 deletion src/sfapi_client/_async/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from authlib.integrations.httpx_client.oauth2_client import AsyncOAuth2Client
from authlib.oauth2.rfc7523 import PrivateKeyJWT
import httpx
import urllib
import tenacity
from authlib.jose import JsonWebKey

Expand Down Expand Up @@ -260,6 +261,11 @@ async def __aenter__(self):

async def _http_client(self):
headers = {"accept": "application/json"}
# Get the users http/https proxies to use for the client
proxy_mounts = {
f"{_type}://": httpx.AsyncHTTPTransport(proxy=_proxy)
for _type, _proxy in urllib.request.getproxies().items()
}
# If we have a client_id then we need to use the OAuth2 client
if self._client_id is not None:
if self.__http_client is None:
Expand All @@ -272,6 +278,7 @@ async def _http_client(self):
token_endpoint=self._token_url,
timeout=10.0,
headers=headers,
mounts=proxy_mounts
)

await self.__http_client.fetch_token()
Expand All @@ -285,7 +292,7 @@ async def _http_client(self):
if self._access_token is not None:
headers.update({"Authorization": f"Bearer {self._access_token}"})

self.__http_client = httpx.AsyncClient(headers=headers)
self.__http_client = httpx.AsyncClient(headers=headers, mounts=proxy_mounts)

return self.__http_client

Expand Down
14 changes: 9 additions & 5 deletions src/sfapi_client/_sync/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from authlib.integrations.httpx_client.oauth2_client import OAuth2Client
from authlib.oauth2.rfc7523 import PrivateKeyJWT
import httpx
import urllib
import tenacity
from authlib.jose import JsonWebKey

Expand Down Expand Up @@ -260,6 +261,11 @@ def __enter__(self):

def _http_client(self):
headers = {"accept": "application/json"}
# Get the users http/https proxies to use for the client
proxy_mounts = {
f"{_type}://": httpx.HTTPTransport(proxy=_proxy)
for _type, _proxy in urllib.request.getproxies().items()
}
# If we have a client_id then we need to use the OAuth2 client
if self._client_id is not None:
if self.__http_client is None:
Expand All @@ -272,6 +278,7 @@ def _http_client(self):
token_endpoint=self._token_url,
timeout=10.0,
headers=headers,
mounts=proxy_mounts
)

self.__http_client.fetch_token()
Expand All @@ -285,7 +292,7 @@ def _http_client(self):
if self._access_token is not None:
headers.update({"Authorization": f"Bearer {self._access_token}"})

self.__http_client = httpx.Client(headers=headers)
self.__http_client = httpx.Client(headers=headers, mounts=proxy_mounts)

return self.__http_client

Expand Down Expand Up @@ -383,10 +390,7 @@ def get(self, url: str, params: Dict[str, Any] = {}) -> httpx.Response:
stop=tenacity.stop_after_attempt(MAX_RETRY),
)
def post(
self,
url: str,
data: Dict[str, Any] = None,
json: Dict[str, Any] = None
self, url: str, data: Dict[str, Any] = None, json: Dict[str, Any] = None
) -> httpx.Response:
client = self._http_client()

Expand Down

0 comments on commit 07eff60

Please sign in to comment.