forked from DataDog/datadogpy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_client.py
More file actions
256 lines (206 loc) · 8.29 KB
/
Copy pathhttp_client.py
File metadata and controls
256 lines (206 loc) · 8.29 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
# Unless explicitly stated otherwise all files in this repository are licensed under the BSD-3-Clause License.
# This product includes software developed at Datadog (https://www.datadoghq.com/).
# Copyright 2015-Present Datadog, Inc
"""
Available HTTP Client for Datadog API client.
Priority:
1. `requests` 3p module
2. `urlfetch` 3p module - Google App Engine only
"""
# stdlib
import copy
import logging
import platform
import urllib
from threading import Lock
# 3p
try:
import requests
import requests.adapters
except ImportError:
requests = None # type: ignore
try:
from google.appengine.api import urlfetch, urlfetch_errors
except ImportError:
urlfetch, urlfetch_errors = None, None
try:
import urllib3 # type: ignore
except ImportError:
urllib3 = None
# datadog
from datadog.api.exceptions import ProxyError, ClientError, HTTPError, HttpTimeout
log = logging.getLogger("datadog.api")
def _get_user_agent_header():
from datadog import version
return "datadogpy/{version} (python {pyver}; os {os}; arch {arch})".format(
version=version.__version__,
pyver=platform.python_version(),
os=platform.system().lower(),
arch=platform.machine().lower(),
)
def _remove_context(exc):
"""Python3: remove context from chained exceptions to prevent leaking API keys in tracebacks."""
exc.__cause__ = None
return exc
class HTTPClient(object):
"""
An abstract generic HTTP client. Subclasses must implement the `request` methods.
"""
@classmethod
def request(cls, method, url, headers, params, data, timeout, proxies, verify, max_retries):
"""
Main method to be implemented by HTTP clients.
The returned data structure has the following fields:
* `content`: string containing the response from the server
* `status_code`: HTTP status code returned by the server
Can raise the following exceptions:
* `ClientError`: server cannot be contacted
* `HttpTimeout`: connection timed out
* `HTTPError`: unexpected HTTP response code
"""
raise NotImplementedError(u"Must be implemented by HTTPClient subclasses.")
class RequestClient(HTTPClient):
"""
HTTP client based on 3rd party `requests` module, using a single session.
This allows us to keep the session alive to spare some execution time.
"""
_session = None
_session_lock = Lock()
@classmethod
def request(cls, method, url, headers, params, data, timeout, proxies, verify, max_retries):
try:
with cls._session_lock:
if cls._session is None:
cls._session = requests.Session()
http_adapter = requests.adapters.HTTPAdapter(max_retries=max_retries)
cls._session.mount("https://", http_adapter)
cls._session.headers.update({"User-Agent": _get_user_agent_header()})
result = cls._session.request(
method, url, headers=headers, params=params, data=data, timeout=timeout, proxies=proxies, verify=verify
)
result.raise_for_status()
except requests.exceptions.ProxyError as e:
raise _remove_context(ProxyError(method, url, e))
except requests.ConnectionError as e:
raise _remove_context(ClientError(method, url, e))
except requests.exceptions.Timeout:
raise _remove_context(HttpTimeout(method, url, timeout))
except requests.exceptions.HTTPError as e:
if e.response.status_code in (400, 401, 403, 404, 409, 429):
# This gets caught afterwards and raises an ApiError exception
pass
else:
raise _remove_context(HTTPError(e.response.status_code, result.reason))
except TypeError:
raise TypeError(
u"Your installed version of `requests` library seems not compatible with"
u"Datadog's usage. We recommend upgrading it ('pip install -U requests')."
u"If you need help or have any question, please contact support@datadoghq.com"
)
return result
class URLFetchClient(HTTPClient):
"""
HTTP client based on Google App Engine `urlfetch` module.
"""
@classmethod
def request(cls, method, url, headers, params, data, timeout, proxies, verify, max_retries):
"""
Wrapper around `urlfetch.fetch` method.
TO IMPLEMENT:
* `max_retries`
"""
# No local certificate file can be used on Google App Engine
validate_certificate = True if verify else False
# Encode parameters in the url
url_with_params = "{url}?{params}".format(url=url, params=urllib.urlencode(params))
newheaders = copy.deepcopy(headers)
newheaders["User-Agent"] = _get_user_agent_header()
try:
result = urlfetch.fetch(
url=url_with_params,
method=method,
headers=newheaders,
validate_certificate=validate_certificate,
deadline=timeout,
payload=data,
# setting follow_redirects=False may be slightly faster:
# https://cloud.google.com/appengine/docs/python/microservice-performance#use_the_shortest_route
follow_redirects=False,
)
cls.raise_on_status(result)
except urlfetch.DownloadError as e:
raise ClientError(method, url, e)
except urlfetch_errors.DeadlineExceededError:
raise HttpTimeout(method, url, timeout)
return result
@classmethod
def raise_on_status(cls, result):
"""
Raise on HTTP status code errors.
"""
status_code = result.status_code
if (status_code / 100) != 2:
if status_code in (400, 401, 403, 404, 409, 429):
pass
else:
raise HTTPError(status_code)
class Urllib3Client(HTTPClient):
"""
HTTP client based on 3rd party `urllib3` module.
"""
_pool = None
_pool_lock = Lock()
@classmethod
def request(cls, method, url, headers, params, data, timeout, proxies, verify, max_retries):
"""
Wrapper around `urllib3.PoolManager.request` method. This method will raise
exceptions for HTTP status codes that are not 2xx.
"""
try:
with cls._pool_lock:
if cls._pool is None:
cls._pool = urllib3.PoolManager(
retries=max_retries,
timeout=timeout,
cert_reqs="CERT_REQUIRED" if verify else "CERT_NONE",
)
newheaders = copy.deepcopy(headers)
newheaders["User-Agent"] = _get_user_agent_header()
response = cls._pool.request(
method, url, body=data, fields=params, headers=newheaders
)
cls.raise_on_status(response)
except urllib3.exceptions.ProxyError as e:
raise _remove_context(ProxyError(method, url, e))
except urllib3.exceptions.MaxRetryError as e:
raise _remove_context(ClientError(method, url, e))
except urllib3.exceptions.TimeoutError as e:
raise _remove_context(HttpTimeout(method, url, e))
except urllib3.exceptions.HTTPError as e:
raise _remove_context(HTTPError(e))
return response
@classmethod
def raise_on_status(cls, response):
"""
Raise on HTTP status code errors.
"""
status_code = response.status
if status_code < 200 or status_code >= 300:
if status_code not in (400, 401, 403, 404, 409, 429):
raise HTTPError(status_code, response.reason)
def resolve_http_client():
"""
Resolve an appropriate HTTP client based the defined priority and user environment.
"""
if requests:
log.debug(u"Use `requests` based HTTP client.")
return RequestClient
if urlfetch and urlfetch_errors:
log.debug(u"Use `urlfetch` based HTTP client.")
return URLFetchClient
if urllib3:
log.debug(u"Use `urllib3` based HTTP client.")
return Urllib3Client
raise ImportError(
u"Datadog API client was unable to resolve a HTTP client. " u" Please install `requests` library."
)