Skip to content
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

Beta: Collapse HTTPClientAsync into HTTPClient #1239

Merged
merged 3 commits into from
Feb 17, 2024
Merged
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 flake8_stripe/flake8_stripe.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class TypingImportsChecker:
"Set",
"Callable",
"Generator",
"Iterable",
]

def __init__(self, tree: ast.AST):
Expand Down
2 changes: 0 additions & 2 deletions stripe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
verify_ssl_certs: bool = True
proxy: Optional[str] = None
default_http_client: Optional["HTTPClient"] = None
default_http_client_async: Optional["HTTPClientAsync"] = None
app_info: Optional[AppInfo] = None
enable_telemetry: bool = True
max_network_retries: int = 0
Expand Down Expand Up @@ -162,7 +161,6 @@ def set_app_info(
# HttpClient
from stripe._http_client import (
HTTPClient as HTTPClient,
HTTPClientAsync as HTTPClientAsync,
PycurlClient as PycurlClient,
RequestsClient as RequestsClient,
UrlFetchClient as UrlFetchClient,
Expand Down
34 changes: 11 additions & 23 deletions stripe/_api_requestor.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@
)
from stripe._http_client import (
HTTPClient,
HTTPClientAsync,
new_default_http_client,
new_default_http_client_async,
new_http_client_async_fallback,
)
from stripe._app_info import AppInfo

Expand All @@ -79,7 +78,6 @@ def __init__(
):
self._options = options
self._client = client
self._client_async = None

# In the case of client=None, we should use the current value of stripe.default_http_client
# or lazily initialize it. Since stripe.default_http_client can change throughout the lifetime of
Expand All @@ -91,12 +89,18 @@ def _get_http_client(self) -> HTTPClient:
global _default_proxy

if not stripe.default_http_client:
kwargs = {
"verify_ssl_certs": stripe.verify_ssl_certs,
"proxy": stripe.proxy,
}
# If the stripe.default_http_client has not been set by the user
# yet, we'll set it here. This way, we aren't creating a new
# HttpClient for every request.
stripe.default_http_client = new_default_http_client(
verify_ssl_certs=stripe.verify_ssl_certs,
proxy=stripe.proxy,
async_fallback_client=new_http_client_async_fallback(
**kwargs
),
**kwargs,
)
_default_proxy = stripe.proxy
elif stripe.proxy != _default_proxy:
Expand All @@ -113,22 +117,6 @@ def _get_http_client(self) -> HTTPClient:
return stripe.default_http_client
return client

def _get_http_client_async(self) -> HTTPClientAsync:
client_async = self._client_async

if client_async is None:
if not stripe.default_http_client_async:
stripe.default_http_client_async = (
new_default_http_client_async(
verify_ssl_certs=stripe.verify_ssl_certs,
proxy=stripe.proxy,
)
)
assert stripe.default_http_client_async is not None
return stripe.default_http_client_async

return client_async

def _replace_options(
self, options: Optional[RequestOptions]
) -> "_APIRequestor":
Expand Down Expand Up @@ -762,7 +750,7 @@ async def request_raw_async(
rcontent,
rcode,
rheaders,
) = await self._get_http_client_async().request_stream_with_retries_async(
) = await self._get_http_client().request_stream_with_retries_async(
method,
abs_url,
headers,
Expand All @@ -775,7 +763,7 @@ async def request_raw_async(
rcontent,
rcode,
rheaders,
) = await self._get_http_client_async().request_with_retries_async(
) = await self._get_http_client().request_with_retries_async(
method,
abs_url,
headers,
Expand Down
Loading
Loading