I got the go ahead to make an API wrapper for an undocumented web API. I'm hoping to get some feedback on my code and design choice.
I've been going back and forth and haven't been able to really feel satisfied with any of my design choices.
There's a lot of placeholders, but I think the base of what I am going for should make sense. At this point it would be a matter of adding the endpoints and do the error catching if I were to continue with it as-is.
Tips, alternative design choices, really anything is welcome, and I appreciate it all.
from uuid import uuid4
from time import strftime, gmtime
from hashlib import md5
import requests
class Wrapper:
endpoints = ['sign_in', 'sign_up']
def __init__(self):
# Details
self.prv_key = '' # private_key
self.pub_key = '' # token
self.member_id = 0
self.device_id = uuid4()
# 'X-Device' header
self.device_header = \
'{0},{1},{2}:{2}'.format('android', self.device_id, '845')
@property
def auth_token(self):
auth_token = ','.join((
# MD5 of auth and time stamp
md5(self.prv_key + self.pub_key).hexdigest(),
md5(strftime('%Y-%m-%dT%H:%M,mobile', gmtime())).hexdigest(),
# Member ID can't be int(0)
str(self.member_id or '')
))
return auth_token
def request(self, url, method, **kwargs):
# Default headers
kwargs['headers'] = {
'X-AuthToken': self.auth_token,
'X-Device': self.device_header,
'Accept': 'application/json',
'Accept-Language': 'en-US',
'User-Agent': 'okhttp/3.6.0'
}
# Proxy settings
kwargs['proxies'] = {
'https': '192.168.178.27:8085', # TODO: DEBUG
'http': '192.168.178.27:8085' # TODO: DEBUG
}
# SSL verification
kwargs['verify'] = False # TODO: DEBUG
r = requests.request(method, url, **kwargs)
# TODO: CATCH ERRORS
# TODO: CORRECT RESPONSE
response = r.content
return response
def sign_in(self):
return self.request('http://httpbin.org/get', 'GET') # TODO: DEBUG
def sign_up(self):
return ''
if __name__ == '__main__':
test = Wrapper()
print test.sign_in()