-
Notifications
You must be signed in to change notification settings - Fork 352
Expand file tree
/
Copy path_http_client.py
More file actions
357 lines (295 loc) · 13.9 KB
/
_http_client.py
File metadata and controls
357 lines (295 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# Copyright 2017 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Internal HTTP client module.
This module provides utilities for making HTTP calls using the requests library.
"""
from __future__ import annotations
import logging
from typing import Any, Dict, Generator, Optional, Tuple, Union
import httpx
import requests.adapters
from requests.packages.urllib3.util import retry # pylint: disable=import-error
from google.auth import credentials
from google.auth import transport
from google.auth.transport import requests as google_auth_requests
from firebase_admin import _utils
from firebase_admin._retry import HttpxRetry, HttpxRetryTransport
logger = logging.getLogger(__name__)
if hasattr(retry.Retry.DEFAULT, 'allowed_methods'):
_ANY_METHOD = {'allowed_methods': None}
else:
_ANY_METHOD = {'method_whitelist': None}
# Default retry configuration: Retries once on low-level connection and socket read errors.
# Retries up to 4 times on HTTP 500 and 503 errors, with exponential backoff. Returns the
# last response upon exhausting all retries.
DEFAULT_RETRY_CONFIG = retry.Retry(
connect=1, read=1, status=4, status_forcelist=[500, 503],
raise_on_status=False, backoff_factor=0.5, **_ANY_METHOD)
DEFAULT_HTTPX_RETRY_CONFIG = HttpxRetry(
max_retries=4, status_forcelist=[500, 503], backoff_factor=0.5)
DEFAULT_TIMEOUT_SECONDS = 120
METRICS_HEADERS = {
'x-goog-api-client': _utils.get_metrics_header(),
}
class HttpClient:
"""Base HTTP client used to make HTTP calls.
HttpClient maintains an HTTP session, and handles request authentication and retries if
necessary.
"""
def __init__(
self, credential=None, session=None, base_url='', headers=None,
retries=DEFAULT_RETRY_CONFIG, timeout=DEFAULT_TIMEOUT_SECONDS):
"""Creates a new HttpClient instance from the provided arguments.
If a credential is provided, initializes a new HTTP session authorized with it. If neither
a credential nor a session is provided, initializes a new unauthorized session.
Args:
credential: A Google credential that can be used to authenticate requests (optional).
session: A custom HTTP session (optional).
base_url: A URL prefix to be added to all outgoing requests (optional).
headers: A map of headers to be added to all outgoing requests (optional).
retries: A urllib retry configuration. Default settings would retry once for low-level
connection and socket read errors, and up to 4 times for HTTP 500 and 503 errors.
Pass a False value to disable retries (optional).
timeout: HTTP timeout in seconds. Defaults to 120 seconds when not specified. Set to
None to disable timeouts (optional).
"""
if credential:
self._session = transport.requests.AuthorizedSession(credential)
elif session:
self._session = session
else:
self._session = requests.Session() # pylint: disable=redefined-variable-type
if headers:
self._session.headers.update(headers)
if retries:
self._session.mount('http://', requests.adapters.HTTPAdapter(max_retries=retries))
self._session.mount('https://', requests.adapters.HTTPAdapter(max_retries=retries))
self._base_url = base_url
self._timeout = timeout
@property
def session(self):
return self._session
@property
def base_url(self):
return self._base_url
@property
def timeout(self):
return self._timeout
def parse_body(self, resp):
raise NotImplementedError
def request(self, method, url, **kwargs):
"""Makes an HTTP call using the Python requests library.
This is the sole entry point to the requests library. All other helper methods in this
class call this method to send HTTP requests out. Refer to
http://docs.python-requests.org/en/master/api/ for more information on supported options
and features.
Args:
method: HTTP method name as a string (e.g. get, post).
url: URL of the remote endpoint.
**kwargs: An additional set of keyword arguments to be passed into the requests API
(e.g. json, params, timeout).
Returns:
Response: An HTTP response object.
Raises:
RequestException: Any requests exceptions encountered while making the HTTP call.
"""
if 'timeout' not in kwargs:
kwargs['timeout'] = self.timeout
kwargs.setdefault('headers', {}).update(METRICS_HEADERS)
resp = self._session.request(method, self.base_url + url, **kwargs)
resp.raise_for_status()
return resp
def headers(self, method, url, **kwargs):
resp = self.request(method, url, **kwargs)
return resp.headers
def body_and_response(self, method, url, **kwargs):
resp = self.request(method, url, **kwargs)
return self.parse_body(resp), resp
def body(self, method, url, **kwargs):
resp = self.request(method, url, **kwargs)
return self.parse_body(resp)
def headers_and_body(self, method, url, **kwargs):
resp = self.request(method, url, **kwargs)
return resp.headers, self.parse_body(resp)
def close(self):
self._session.close()
self._session = None
class JsonHttpClient(HttpClient):
"""An HTTP client that parses response messages as JSON."""
def __init__(self, **kwargs):
HttpClient.__init__(self, **kwargs)
def parse_body(self, resp):
return resp.json()
class GoogleAuthCredentialFlow(httpx.Auth):
"""Google Auth Credential Auth Flow"""
def __init__(self, credential: credentials.Credentials):
self._credential = credential
self._max_refresh_attempts = 2
self._refresh_status_codes = (401,)
def apply_auth_headers(
self,
request: httpx.Request,
auth_request: google_auth_requests.Request
) -> None:
"""A helper function that refreshes credentials if needed and mutates the request headers
to contain access token and any other Google Auth headers."""
logger.debug(
'Attempting to apply auth headers. Credential validity before: %s',
self._credential.valid
)
self._credential.before_request(
auth_request, request.method, str(request.url), request.headers
)
logger.debug('Auth headers applied. Credential validity after: %s', self._credential.valid)
def auth_flow(self, request: httpx.Request) -> Generator[httpx.Request, httpx.Response, None]:
_original_headers = request.headers.copy()
_credential_refresh_attempt = 0
# Create a Google auth request object to be used for refreshing credentials
auth_request = google_auth_requests.Request()
while True:
# Copy original headers for each attempt
request.headers = _original_headers.copy()
# Apply auth headers (which might include an implicit refresh if token is expired)
self.apply_auth_headers(request, auth_request)
logger.debug(
'Dispatching request, attempt %d of %d',
_credential_refresh_attempt, self._max_refresh_attempts
)
response: httpx.Response = yield request
if response.status_code in self._refresh_status_codes:
if _credential_refresh_attempt < self._max_refresh_attempts:
logger.debug(
'Received status %d. Attempting explicit credential refresh. \
Attempt %d of %d.',
response.status_code,
_credential_refresh_attempt + 1,
self._max_refresh_attempts
)
# Explicitly force a credentials refresh
self._credential.refresh(auth_request)
_credential_refresh_attempt += 1
else:
logger.debug(
'Received status %d, but max auth refresh attempts (%d) reached. \
Returning last response.',
response.status_code, self._max_refresh_attempts
)
break
else:
# Status code is not one that requires a refresh, so break and return response
logger.debug(
'Status code %d does not require refresh. Returning response.',
response.status_code
)
break
# The last yielded response is automatically returned by httpx's auth flow.
class HttpxAsyncClient():
"""Async HTTP client used to make HTTP/2 calls using HTTPX.
HttpxAsyncClient maintains an async HTTPX client, handles request authentication, and retries
if necessary.
"""
def __init__(
self,
credential: Optional[credentials.Credentials] = None,
base_url: str = '',
headers: Optional[Union[httpx.Headers, Dict[str, str]]] = None,
retry_config: HttpxRetry = DEFAULT_HTTPX_RETRY_CONFIG,
timeout: int = DEFAULT_TIMEOUT_SECONDS,
http2: bool = True
) -> None:
"""Creates a new HttpxAsyncClient instance from the provided arguments.
If a credential is provided, initializes a new async HTTPX client authorized with it.
Otherwise, initializes a new unauthorized async HTTPX client.
Args:
credential: A Google credential that can be used to authenticate requests (optional).
base_url: A URL prefix to be added to all outgoing requests (optional).
headers: A map of headers to be added to all outgoing requests (optional).
retry_config: A HttpxRetry configuration. Default settings would retry up to 4 times for
HTTP 500 and 503 errors (optional).
timeout: HTTP timeout in seconds. Defaults to 120 seconds when not specified (optional).
http2: A boolean indicating if HTTP/2 support should be enabled. Defaults to `True` when
not specified (optional).
"""
self._base_url = base_url
self._timeout = timeout
self._headers = {**headers, **METRICS_HEADERS} if headers else {**METRICS_HEADERS}
self._retry_config = retry_config
# Only set up retries on urls starting with 'http://' and 'https://'
self._mounts = {
'http://': HttpxRetryTransport(retry=self._retry_config, http2=http2),
'https://': HttpxRetryTransport(retry=self._retry_config, http2=http2)
}
if credential:
self._async_client = httpx.AsyncClient(
http2=http2,
timeout=self._timeout,
headers=self._headers,
auth=GoogleAuthCredentialFlow(credential), # Add auth flow for credentials.
mounts=self._mounts
)
else:
self._async_client = httpx.AsyncClient(
http2=http2,
timeout=self._timeout,
headers=self._headers,
mounts=self._mounts
)
@property
def base_url(self):
return self._base_url
@property
def timeout(self):
return self._timeout
@property
def async_client(self):
return self._async_client
async def request(self, method: str, url: str, **kwargs: Any) -> httpx.Response:
"""Makes an HTTP call using the HTTPX library.
This is the sole entry point to the HTTPX library. All other helper methods in this
class call this method to send HTTP requests out. Refer to
https://www.python-httpx.org/api/ for more information on supported options
and features.
Args:
method: HTTP method name as a string (e.g. get, post).
url: URL of the remote endpoint.
**kwargs: An additional set of keyword arguments to be passed into the HTTPX API
(e.g. json, params, timeout).
Returns:
Response: An HTTPX response object.
Raises:
HTTPError: Any HTTPX exceptions encountered while making the HTTP call.
RequestException: Any requests exceptions encountered while making the HTTP call.
"""
if 'timeout' not in kwargs:
kwargs['timeout'] = self.timeout
resp = await self._async_client.request(method, self.base_url + url, **kwargs)
return resp.raise_for_status()
async def headers(self, method: str, url: str, **kwargs: Any) -> httpx.Headers:
resp = await self.request(method, url, **kwargs)
return resp.headers
async def body_and_response(
self, method: str, url: str, **kwargs: Any) -> Tuple[Any, httpx.Response]:
resp = await self.request(method, url, **kwargs)
return self.parse_body(resp), resp
async def body(self, method: str, url: str, **kwargs: Any) -> Any:
resp = await self.request(method, url, **kwargs)
return self.parse_body(resp)
async def headers_and_body(
self, method: str, url: str, **kwargs: Any) -> Tuple[httpx.Headers, Any]:
resp = await self.request(method, url, **kwargs)
return resp.headers, self.parse_body(resp)
def parse_body(self, resp: httpx.Response) -> Any:
return resp.json()
async def aclose(self) -> None:
await self._async_client.aclose()