Skip to content

Commit d017407

Browse files
author
Kyle Hamlin
committed
add headers param to base request methods and stats_list
1 parent 6df44e1 commit d017407

2 files changed

Lines changed: 21 additions & 15 deletions

File tree

sailthru/sailthru_client.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class SailthruClient(object):
5252
from sailthru import SailthruClient
5353
api_key = "your-api-key"
5454
api_secret = "api-secret"
55-
client = SailthruCLient(api_key, api_secret)
55+
client = SailthruClient(api_key, api_secret)
5656
"""
5757

5858
def __init__(self, api_key, secret, api_url=None):
@@ -513,7 +513,7 @@ def get_purchase(self, purchase_id, purchase_key='sid'):
513513
'purchase_key': purchase_key}
514514
return self.api_get('purchase', data)
515515

516-
def stats_list(self, list=None, date=None):
516+
def stats_list(self, list=None, date=None, headers=None):
517517
"""
518518
Retrieve information about your subscriber counts on a particular list, on a particular day.
519519
http://docs.sailthru.com/api/stat
@@ -523,7 +523,7 @@ def stats_list(self, list=None, date=None):
523523
data['list'] = list
524524
if date is not None:
525525
data['date'] = date
526-
return self._stats(data)
526+
return self._stats(data, headers)
527527

528528
def stats_blast(self, blast_id=None, start_date=None, end_date=None, options=None):
529529
"""
@@ -556,11 +556,11 @@ def stats_send(self, template, start_date, end_date, options=None):
556556
data['stat'] = 'send'
557557
return self._stats(data)
558558

559-
def _stats(self, data):
559+
def _stats(self, data, headers=None):
560560
"""
561561
Make Stats API Request
562562
"""
563-
return self.api_get('stats', data)
563+
return self.api_get('stats', data, headers)
564564

565565
def receive_verify_post(self, post_params):
566566
"""
@@ -696,13 +696,14 @@ def check_for_valid_postback_actions(self, required_keys, post_params):
696696
return False
697697
return True
698698

699-
def api_get(self, action, data):
699+
def api_get(self, action, data, headers=None):
700700
"""
701701
Perform an HTTP GET request, using the shared-secret auth hash.
702702
@param action: API action call
703703
@param data: dictionary values
704704
"""
705-
return self._api_request(action, data, 'GET')
705+
#{'action': action, 'data': data, 'request_type': 'GET', 'headers': headers}
706+
return self._api_request(action, data, 'GET', headers)
706707

707708
def api_post(self, action, data, binary_data_param=None):
708709
"""
@@ -718,7 +719,7 @@ def api_post(self, action, data, binary_data_param=None):
718719

719720
def api_post_multipart(self, action, data, binary_data_param):
720721
"""
721-
Perform an HTTP Multipart POST request, using the shared-secret auth hash.
722+
Perform an HTTP Multipart POST request, using the shared-secret auth hash.
722723
@param action: API action call
723724
@param data: dictionary values
724725
@param: binary_data_params: array of multipart keys
@@ -748,7 +749,7 @@ def api_delete(self, action, data):
748749
"""
749750
return self._api_request(action, data, 'DELETE')
750751

751-
def _api_request(self, action, data, request_type):
752+
def _api_request(self, action, data, request_type, headers=None):
752753
"""
753754
Make Request to Sailthru API with given data and api key, format and signature hash
754755
"""
@@ -757,12 +758,12 @@ def _api_request(self, action, data, request_type):
757758
else:
758759
file_data = None
759760

760-
return self._http_request(action, self._prepare_json_payload(data), request_type, file_data)
761+
return self._http_request(action, self._prepare_json_payload(data), request_type, file_data, headers)
761762

762-
def _http_request(self, action, data, method, file_data=None):
763+
def _http_request(self, action, data, method, file_data=None, headers=None):
763764
url = self.api_url + '/' + action
764765
file_data = file_data or {}
765-
response = sailthru_http_request(url, data, method, file_data)
766+
response = sailthru_http_request(url, data, method, file_data, headers)
766767
if (action in self.last_rate_limit_info):
767768
self.last_rate_limit_info[action][method] = response.get_rate_limit_headers()
768769
else:

sailthru/sailthru_http.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,21 @@ def flatten(hash_table, brackets=True):
2727
return f
2828
return flatten(hash_table, False)
2929

30-
def sailthru_http_request(url, data, method, file_data=None):
30+
def sailthru_http_request(url, data, method, file_data=None, headers=None):
3131
"""
3232
Perform an HTTP GET / POST / DELETE request
3333
"""
3434
data = flatten_nested_hash(data)
3535
method = method.upper()
3636
params, data = (None, data) if method == 'POST' else (data, None)
37-
37+
sailthru_headers = {'User-Agent': 'Sailthru API Python Client %s; Python Version: %s' % ('2.3.3', platform.python_version())}
38+
if headers and isinstance(headers, dict):
39+
for key, value in sailthru_headers.items():
40+
headers[key] = value
41+
else:
42+
headers = sailthru_headers
3843
try:
39-
headers = {'User-Agent': 'Sailthru API Python Client %s; Python Version: %s' % ('2.3.3', platform.python_version())}
44+
print(headers)
4045
response = requests.request(method, url, params=params, data=data, files=file_data, headers=headers, timeout=10)
4146
return SailthruResponse(response)
4247
except requests.HTTPError as e:

0 commit comments

Comments
 (0)