|
24 | 24 | import pkgutil
|
25 | 25 | import time
|
26 | 26 | import requests
|
| 27 | +from requests.adapters import HTTPAdapter, Retry |
27 | 28 |
|
28 | 29 | from ._compat import (
|
29 | 30 | MutableMapping, iteritems, string_types, urlencode,
|
@@ -228,6 +229,15 @@ def __init__(self, api, interface=None, endpoint=None, method=None, nested=False
|
228 | 229 | self.endpoint = endpoint
|
229 | 230 | self.method = method
|
230 | 231 | self.nested = nested
|
| 232 | + self.session = requests.Session() |
| 233 | + adapter = HTTPAdapter(max_retries=Retry( |
| 234 | + total=3, |
| 235 | + connect=3, |
| 236 | + allowed_methods=["HEAD", "GET", "POST", "PATCH", "PUT", "OPTIONS"] |
| 237 | + )) |
| 238 | + self.session.mount("https://", adapter) |
| 239 | + self.session.mount("http://", adapter) |
| 240 | + |
231 | 241 |
|
232 | 242 | def __getattr__(self, attr):
|
233 | 243 | if attr in getattr(self, '__dict__'):
|
@@ -304,7 +314,7 @@ def validate_kwarg(key, target):
|
304 | 314 |
|
305 | 315 | # TODO: Use HTTP "method" from interfaces.json
|
306 | 316 | path = '%s%s.%s' % (self.api.host, self.method, self.endpoint)
|
307 |
| - response = requests.post(path, data=body, headers=headers, timeout=self.api.timeout) |
| 317 | + response = self.session.post(path, data=body, headers=headers, timeout=self.api.timeout) |
308 | 318 |
|
309 | 319 | # Make sure we got a 2xx response indicating success
|
310 | 320 | if not response.status_code >= 200 or not response.status_code < 300:
|
|
0 commit comments