diff --git a/aweber_api/oauth.py b/aweber_api/oauth.py index 74bc572..1fe3003 100644 --- a/aweber_api/oauth.py +++ b/aweber_api/oauth.py @@ -1,9 +1,10 @@ +from urllib import urlencode +import json import os + import oauth2 as oauth -import json -from urllib import urlencode -import aweber_api +from aweber_api.base import APIException class OAuthAdapter(object): @@ -29,21 +30,20 @@ def request(self, method, url, data={}, response='body'): url = self._expand_url(url) body = self._prepare_request_body(method, url, data) - # need a test for the next 4 lines below content_type = 'application/json' if method == 'GET' and body is not None and body is not '': - # todo: need a better way to do this! if '?' in url: url = '{0}&{1}'.format(url, body) else: url = '{0}?{1}'.format(url, body) + if method == 'POST': content_type = 'application/x-www-form-urlencoded' - headers = {'Content-Type' : content_type} + headers = {'Content-Type': content_type} + resp, content = client.request( + url, method, body=body, headers=headers) - resp, content = client.request(url, method, body=body, - headers=headers) if int(resp['status']) >= 400: """ API Service Errors: @@ -58,7 +58,7 @@ def request(self, method, url, data={}, response='body'): error = content.get('error', {}) error_type = error.get('type') error_msg = error.get('message') - raise aweber_api.base.APIException( + raise APIException( '{0}: {1}'.format(error_type, error_msg)) if response == 'body' and isinstance(content, str): @@ -71,7 +71,7 @@ def request(self, method, url, data={}, response='body'): def _expand_url(self, url): if not url[:4] == 'http': - return '%s%s' % (self.api_base, url) + return '{0}{1}'.format(self.api_base, url) return url def _get_client(self): @@ -81,11 +81,11 @@ def _get_client(self): client = oauth.Client(self.consumer, token=token) else: client = oauth.Client(self.consumer) + client.ca_certs = os.path.join(os.path.dirname(__file__), 'cacert.crt') return client def _prepare_request_body(self, method, url, data): - # might need a test for the changes to this method if method not in ['POST', 'GET', 'PATCH'] or len(data.keys()) == 0: return '' if method in ['POST', 'GET']: