Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for user-specified timeout and retry options in the client. #67

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions sailthru/sailthru_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,21 @@ class SailthruClient(object):
client = SailthruCLient(api_key, api_secret)
"""

def __init__(self, api_key, secret, api_url=None):
def __init__(self, api_key, secret,
api_url=None, timeout=None, retries=None):
"""Create a new client.

@param api_key: the key used to identify this app to sailthru
@param secret: the secret used to authenticate this user with sailthru
@param api_url: the protocol+host to send API requests to, or None for the default
@param timeout: how long to wait for HTTP requests, or None for the default
@param retries: how many times to retry on transient HTTP errors, or None for the default.
"""
self.api_key = api_key
self.secret = secret
self.api_url = api_url if api_url else 'https://api.sailthru.com'
self.api_url = api_url if api_url is not None else 'https://api.sailthru.com'
self.timeout = timeout if timeout is not None else 10
self.retries = retries if retries is not None else 0
self.last_rate_limit_info = {}

def send(self, template, email, _vars=None, options=None, schedule_time=None, limit=None):
Expand Down Expand Up @@ -762,7 +773,8 @@ def _api_request(self, action, data, request_type):
def _http_request(self, action, data, method, file_data=None):
url = self.api_url + '/' + action
file_data = file_data or {}
response = sailthru_http_request(url, data, method, file_data)
response = sailthru_http_request(url, data, method, file_data,
timeout=self.timeout, retries=self.retries)
if (action in self.last_rate_limit_info):
self.last_rate_limit_info[action][method] = response.get_rate_limit_headers()
else:
Expand All @@ -788,4 +800,4 @@ def get_last_rate_limit_info(self, action, method):
if (action in self.last_rate_limit_info and method in self.last_rate_limit_info[action]):
return self.last_rate_limit_info[action][method]

return None
return None
25 changes: 23 additions & 2 deletions sailthru/sailthru_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ def flatten(hash_table, brackets=True):
return f
return flatten(hash_table, False)

def sailthru_http_request(url, data, method, file_data=None):
def sailthru_http_request(url, data, method, file_data=None,
timeout=10, retries=0):
"""
Perform an HTTP GET / POST / DELETE request
"""
Expand All @@ -37,7 +38,27 @@ def sailthru_http_request(url, data, method, file_data=None):

try:
headers = {'User-Agent': 'Sailthru API Python Client %s; Python Version: %s' % ('2.3.3', platform.python_version())}
response = requests.request(method, url, params=params, data=data, files=file_data, headers=headers, timeout=10)
if retries > 0:
session = requests.Session()
# We retry on connection errors and all 5xx errors. We do
# not retry on read errors since for POST requests that
# happens after the POST has finished, and POSTs are not
# necessarily safe to re-do.
retry = requests.packages.urllib3.Retry(
retries,
read=0,
method_whitelist=False,
status_forcelist={500, 502, 503, 504},
raise_on_status=False)
session.mount(url, requests.adapters.HTTPAdapter(max_retries=retry))
else:
session = requests
response = session.request(method, url,
params=params,
data=data,
files=file_data,
headers=headers,
timeout=timeout)
return SailthruResponse(response)
except requests.HTTPError as e:
raise SailthruClientError(str(e))
Expand Down