-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathexampleClient.py
executable file
·337 lines (290 loc) · 10.5 KB
/
exampleClient.py
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/usr/bin/env python
import requests
from requests.auth import HTTPBasicAuth, HTTPDigestAuth
from requests_oauthlib import OAuth1
from math import floor
import yaml
import logging
import os
import sys
from enum import Enum
# Set URL for the client
URL = 'https://api.example.com'
# Set up basic logger
logger = logging.getLogger('example.exampleClient')
# Setup stdout logger
soh = logging.StreamHandler(sys.stdout)
# Can optionally set logging levels per handler
# soh.setLevel(logging.WARN)
logger.addHandler(soh)
# File handler for logging to a file
# fh = logging.FileHandler('apiWrapper.log')
# fh.setLevel(logging.DEBUG)
# logger.addHandler(fh)
# Get log level from env vars
log_level = os.environ.get('LOG_LEVEL', 'INFO').upper()
if os.environ.get('DEBUG'):
if log_level:
logger.warn("Overriding LOG_LEVEL setting with DEBUG")
log_level = 'DEBUG'
try:
logger.setLevel(log_level)
except ValueError:
logger.setLevel(logging.INFO)
logger.warn("Variable LOG_LEVEL not valid - Setting Log Level to INFO")
class AuthenticationError(Exception):
pass
AuthType = Enum('AuthType', 'HTTPBASICAUTH HTTPDIGESTAUTH OAUTH1 OAUTH2 NONE')
class Example(object):
def __init__(
self,
user=None,
password=None,
client_app_key=None,
client_app_secret=None,
user_oauth_token=None,
user_oauth_token_secret=None,
api_app_key=None,
auth_type=None
):
'''Set up client for API communications
This is where you'll need to specify all the authentication and
required headers
Preference will be given towards passed in variables, otherwise
environment variables will be used
Config file is supported but discouraged since it's a common
source of credential leaks
'''
# Setup Host here
self.url = URL
# Setup Session object for all future API calls
self.session = requests.Session()
# Setup authentication
# If interested in using a config file instead of env vars, load with
# self._load_key_yml(config_key, path)
# Feel free to clear out auth methods not implemented by the API
if not auth_type:
auth_type = AuthType[os.getenv('AUTH_TYPE', default='NONE')]
if (auth_type == AuthType.HTTPBASICAUTH or
auth_type == AuthType.HTTPDIGESTAUTH):
if not user:
user = os.getenv('CLIENT_USER')
if not password:
password = os.getenv('CLIENT_PASSWORD')
if auth_type == AuthType.HTTPBASICAUTH:
self.session.auth = HTTPBasicAuth(user, password)
else:
self.session.auth = HTTPDigestAuth(user, password)
if auth_type == AuthType.OAUTH1:
if not client_app_key:
client_app_key = os.getenv('CLIENT_APP_KEY')
if not client_app_secret:
client_app_secret = os.getenv('CLIENT_APP_SECRET')
if not user_oauth_token:
user_oauth_token = os.getenv('USER_OAUTH_TOKEN')
if not user_oauth_token_secret:
user_oauth_token_secret = os.getenv('USER_OAUTH_TOKEN_SECRET')
self.session.auth = OAuth1(
client_app_key,
client_app_secret,
user_oauth_token,
user_oauth_token_secret
)
if auth_type == AuthType.OAUTH2:
# Feel free to create a PR if you want to contribute
raise NotImplementedError("OAuth2 currently not supported")
# Some APIs require an API key in a header in addition to or instead
# of standard authentication methods
if not api_app_key:
api_app_key = os.getenv('API_APP_KEY')
self.session.headers.update({'App-Key': api_app_key})
# Setup any additional headers required by the API
# This sometimes includes additional account info
account_owner = os.getenv('EXAMPLE_ACCOUNT_OWNER')
if account_owner:
self.session.headers.update({'account-email': account_owner})
logger.info('Authenticating...')
if self._authenticate():
logger.info('Authentication Successful!')
else:
logger.info('Authentication Failed!')
raise AuthenticationError('Authentication Failed!')
def _load_key_yml(self, config_key, path):
'''Example function for loading config values from a yml file
'''
with open(path) as stream:
yaml_data = yaml.safe_load(stream)
return yaml_data[config_key]
def _authenticate(self):
'''Authenticate by making simple request
Some APIs will offer a simple auth validation endpoint, some
won't.
I like to make the simplest authenticated request when
instantiating the client just to make sure the auth works
'''
resp_json = self._make_request('/api/v1/servertime', 'GET')
try:
pass
except AuthenticationError as e:
raise e
print(resp_json)
if resp_json:
return True
else:
return False
def _make_request(self, endpoint, method, query_params=None, body=None):
'''Handles all requests to Example API
'''
url = self.url + endpoint
req = requests.Request(method, url, params=query_params, json=body)
prepped = self.session.prepare_request(req)
# Log request prior to sending
self._pprint_request(prepped)
# Actually make request to endpoint
r = self.session.send(prepped)
# Log response immediately upon return
self._pprint_response(r)
# Handle all response codes as elegantly as needed in a single spot
if r.status_code == requests.codes.ok:
try:
resp_json = r.json()
logger.debug('Response: {}'.format(resp_json))
return resp_json
except ValueError:
return r.text
elif r.status_code == 401:
logger.info("Authentication Unsuccessful!")
try:
resp_json = r.json()
logger.debug('Details: ' + str(resp_json))
raise AuthenticationError(resp_json)
except ValueError:
raise
# TODO handle rate limiting gracefully
# Raises HTTP error if status_code is 4XX or 5XX
elif r.status_code >= 400:
logger.error('Received a ' + str(r.status_code) + ' error!')
try:
logger.debug('Details: ' + str(r.json()))
except ValueError:
pass
r.raise_for_status()
def _pprint_request(self, prepped):
'''
method endpoint HTTP/version
Host: host
header_key: header_value
body
'''
method = prepped.method
url = prepped.path_url
# TODO retrieve HTTP version
headers = '\n'.join('{}: {}'.format(k, v) for k, v in
prepped.headers.items())
# Print body if present or empty string if not
body = prepped.body or ""
logger.info("Requesting {} to {}".format(method, url))
logger.debug(
'{}\n{} {} HTTP/1.1\n{}\n\n{}'.format(
'-----------REQUEST-----------',
method,
url,
headers,
body
)
)
def _pprint_response(self, r):
'''
HTTP/version status_code status_text
header_key: header_value
body
'''
# Not using requests_toolbelt.dump because I want to be able to
# print the request before submitting and response after
# ref: https://stackoverflow.com/a/35392830/8418673
httpv0, httpv1 = list(str(r.raw.version))
httpv = 'HTTP/{}.{}'.format(httpv0, httpv1)
status_code = r.status_code
status_text = r.reason
headers = '\n'.join('{}: {}'.format(k, v) for k, v in
r.headers.items())
body = r.text or ""
# Convert timedelta to milliseconds
elapsed = floor(r.elapsed.total_seconds() * 1000)
logger.info(
"Response {} {} received in {}ms".format(
status_code,
status_text,
elapsed
)
)
logger.debug(
'{}\n{} {} {}\n{}\n\n{}'.format(
'-----------RESPONSE-----------',
httpv,
status_code,
status_text,
headers,
body
)
)
def _datetime_to_epoch(self, dt):
# floor needed because datetime.timestamp() returns
# >>> datetime.datetime.now().timestamp()
# 1550686321.920955 - <epoch seconds>.<epoch microseconds>
return floor(dt.timestamp())
def make_request(
self,
endpoint,
method="GET",
query_params=None,
body=None
):
return self._make_request(endpoint, method, query_params, body)
def get_users(
self,
tags=[],
offset=0,
limit=20
):
'''Get list of users in Example
:tags: list of tags to filter users on
'''
endpoint = '/api/v1/users'
params = {}
if tags:
params['tags'] = ','.join(tags)
else:
tags = None
params['offset'] = offset
params['limit'] = limit
params['include_tags'] = True
return self._make_request(endpoint, 'GET', query_params=params)
def get_user(self, user_id):
"""return user object
"""
endpoint = '/api/v1/users/{}'.format(user_id)
return self._make_request(endpoint, 'GET')
def create_user(
self,
name,
email,
**kwargs
):
'''Create new user in Example
'''
endpoint = '/api/v1/users'
params = {}
params['name'] = name
params['email'] = email
# Allows an arbitrary number of keyword arguments to this method
# to be converted into query_params
for key, value in kwargs.items():
params[key] = value
return self._make_request(endpoint, 'POST', query_params=params)
if __name__ == "__main__":
client = Example()
users = client.get_users()
logger.info("Get users details:\n{}".format(users))
resp = client.create_user("John", "[email protected]")
logger.info("Create John details:\n{}".format(resp))