Skip to content

Adding support for http(s) proxies #92

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

Closed
wants to merge 2 commits into from
Closed
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
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