Skip to content

Commit 011bc89

Browse files
Merge pull request disqus#68 from markjm/master
Add default retry to phab APIs
2 parents 8762652 + af4f8e9 commit 011bc89

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

phabricator/__init__.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import pkgutil
2525
import time
2626
import requests
27+
from requests.adapters import HTTPAdapter, Retry
2728

2829
from ._compat import (
2930
MutableMapping, iteritems, string_types, urlencode,
@@ -228,6 +229,15 @@ def __init__(self, api, interface=None, endpoint=None, method=None, nested=False
228229
self.endpoint = endpoint
229230
self.method = method
230231
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+
231241

232242
def __getattr__(self, attr):
233243
if attr in getattr(self, '__dict__'):
@@ -304,7 +314,7 @@ def validate_kwarg(key, target):
304314

305315
# TODO: Use HTTP "method" from interfaces.json
306316
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)
308318

309319
# Make sure we got a 2xx response indicating success
310320
if not response.status_code >= 200 or not response.status_code < 300:

0 commit comments

Comments
 (0)