forked from aweber/AWeber-API-Python-Library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoauth.py
More file actions
104 lines (85 loc) · 3.16 KB
/
oauth.py
File metadata and controls
104 lines (85 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import json
import os
import oauth2 as oauth
import six
from aweber_api.base import APIException
try:
from urllib import urlencode
except ImportError:
from urllib.parse import urlencode
class OAuthAdapter(object):
def __init__(self, key, secret, base):
self.key = key
self.secret = secret
self.consumer = oauth.Consumer(key=self.key, secret=self.secret)
self.api_base = base
def _parse(self, response):
try:
data = json.loads(response)
if not data or data == '':
return response
return data
except:
pass
return response
def request(self, method, url, data={}, response='body'):
client = self._get_client()
url = self._expand_url(url)
body = self._prepare_request_body(method, url, data)
content_type = 'application/json'
if method == 'GET' and body is not None and body is not '':
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}
resp, content = client.request(
url, method, body=body, headers=headers)
if int(resp['status']) >= 400:
"""
API Service Errors:
Please review the Exception that is raised it should indicate
what the error is.
refer to https://labs.aweber.com/docs/troubleshooting for more
details.
"""
content = json.loads(content)
error = content.get('error', {})
error_type = error.get('type')
error_msg = error.get('message')
raise APIException(
'{0}: {1}'.format(error_type, error_msg))
if isinstance(content, six.binary_type):
content = content.decode('utf-8')
if response == 'body' and isinstance(content, six.string_types):
return self._parse(content)
if response == 'status':
return resp['status']
if response == 'headers':
return resp
return None
def _expand_url(self, url):
if not url[:4] == 'http':
return '{0}{1}'.format(self.api_base, url)
return url
def _get_client(self):
token = self.user.get_highest_priority_token()
if token:
token = oauth.Token(token, self.user.token_secret)
client = oauth.Client(self.consumer, token=token)
else:
client = oauth.Client(self.consumer)
return client
def _prepare_request_body(self, method, url, data):
if method not in ['POST', 'GET', 'PATCH'] or len(data.keys()) == 0:
return ''
if method in ['POST', 'GET']:
# WARNING: non-primative items in data must be json serialized.
for key in data:
if type(data[key]) in [dict, list]:
data[key] = json.dumps(data[key])
return urlencode(data)
if method == 'PATCH':
return json.dumps(data)