Skip to content
Open
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
13 changes: 10 additions & 3 deletions mailchimp_transactional/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
import requests
import json


class ApiClientError(Exception):
def __init__(self, text, status_code):
self.text = text
self.status_code = status_code


class ApiClient(object):
def __init__(self):
self.host = "https://mandrillapp.com/api/1.0"
Expand Down Expand Up @@ -61,19 +63,24 @@ def call_api(self, resource_path, method, header_params=None, body=None, **kwarg
# perform request and return response
res = self.request(method, url, body, headers)

try:
res.raise_for_status()
except requests.exceptions.HTTPError:
raise ApiClientError(text=res.text, status_code=res.status_code)

try:
if 'application/json' in res.headers.get('content-type'):
data = res.json()
else:
data = res.text
except Exception as err:
except Exception:
data = None

if data:
if (res.ok):
return data
else:
raise ApiClientError(text = data, status_code = res.status_code)
raise ApiClientError(text=data, status_code=res.status_code)
else:
return res

Expand All @@ -86,4 +93,4 @@ def request(self, method, url, body=None, headers=None, timeout=None):
else:
raise ValueError(
"http method must be `POST`"
)
)