8000 Added HTTP2 support for FCM API · AbdAftab/firebase-admin-python@3f1e546 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3f1e546

Browse files
committed
Added HTTP2 support for FCM API
1 parent 6011dd0 commit 3f1e546

File tree

4 files changed

+456
-19
lines changed

4 files changed

+456
-19
lines changed

firebase_admin/_http_client.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414

1515
"""Internal HTTP client module.
1616
17-
This module provides utilities for making HTTP calls using the requests library.
17+
This module provides utilities for making HTTP calls using the requests/httpx library.
1818
"""
1919

2020
from google.auth import transport
21+
import httpx
22+
from httpx import _utils as httpx_utils
2123
import requests
2224
from requests.packages.urllib3.util import retry # pylint: disable=import-error
23-
25+
from firebase_admin._httpx_utils import HttpxAuthorizedClient, CustomRetryTransport
2426

2527
if hasattr(retry.Retry.DEFAULT, 'allowed_methods'):
2628
_ANY_METHOD = {'allowed_methods': None}
@@ -33,6 +35,8 @@
3335
connect=1, read=1, status=4, status_forcelist=[500, 503],
3436
raise_on_status=False, backoff_factor=0.5, **_ANY_METHOD)
3537

38+
DEFAULT_TRANSPORT_CONFIG = CustomRetryTransport(status_forcelist=[500, 503],
39+
backoff_factor=0.5)
3640

3741
DEFAULT_TIMEOUT_SECONDS = 120
3842

@@ -46,7 +50,8 @@ class HttpClient:
4650

4751
def __init__(
4852
self, credential=None, session=None, base_url='', headers=None,
49-
retries=DEFAULT_RETRY_CONFIG, timeout=DEFAULT_TIMEOUT_SECONDS):
53+
retries=DEFAULT_RETRY_CONFIG, timeout=DEFAULT_TIMEOUT_SECONDS,
54+
http2=False, request_handler=DEFAULT_TRANSPORT_CONFIG):
5055
"""Creates a new HttpClient instance from the provided arguments.
5156
5257
If a credential is provided, initializes a new HTTP session authorized with it. If neither
@@ -62,19 +67,33 @@ def __init__(
6267
Pass a False value to disable retries (optional).
6368
timeout: HTTP timeout in seconds. Defaults to 120 seconds when not specified. Set to
6469
None to disable timeouts (optional).
70+
http2: A boolean indicating whether to use HTTP/2 for requests (optional).
71+
request_handler: A custom httpx transport to use for HTTP/2 requests. Default settings
72+
would be similar to retries default (optional).
6573
"""
6674
if credential:
67-
self._session = transport.requests.AuthorizedSession(credential)
75+
self._session = (
76+
transport.requests.AuthorizedSession(credential)
77+
if not http2
78+
else HttpxAuthorizedClient(credential, request_handler)
79+
)
6880
elif session:
6981
self._session = session
7082
else:
71-
self._session = requests.Session() # pylint: disable=redefined-variable-type
83+
self._session = (
84+
requests.Session()
85+
if not http2
86+
else httpx.Client(http2=True, transport=request_handler)
87+
) # pylint: disable=redefined-variable-type
7288

7389
if headers:
7490
self._session.headers.update(headers)
75-
if retries:
91+
if retries and not http2:
7692
self._session.mount('http://', requests.adapters.HTTPAdapter(max_retries=retries))
7793
self._session.mount('https://', requests.adapters.HTTPAdapter(max_retries=retries))
94+
if request_handler and http2:
95+
self._session._mounts = {httpx_utils.URLPattern('http://'): request_handler, # pylint: disable=protected-access
96+
httpx_utils.URLPattern('https://'): request_handler}
7897
self._base_url = base_url
7998
self._timeout = timeout
8099

0 commit comments

Comments
 (0)
0