public
Last active

An example of a pattern I use when using requests to wrap an api.

  • Download Gist
ProviderAPIWrapper.py
Python
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
import requests
import json
 
class AbstractDataProvider():
"""A list of methods that data providers should implement or extend."""
 
def __init__(self, base_url, username=None, password=None):
self.base_url = base_url.rstrip('/')
self.username = username
self.password = password
def request(self, request_type, path, params=None, data=None, files=None, response_type=None):
"""
Handles requests for each api call to <api-provider>.
Since responses are standard from this provider, also return a standard response.
"""
#if you need to manually set headers or other parameters, set them here
#example: headers = {'content-type': 'application/json'}
#you can also dump your data format below under the data arguement. In this example, we're using json.
url = '{}/{}'.format(self.base_url, path)
r = requests.request(request_type, url, params=params, data=json.dumps(data), files=files, auth=self.get_credentials(), headers=headers)
if response_type == 'json':
return r.json()
else:
pass
 
class MyDataProvider(AbstractDataProvider):
def __init__(self, base_url):
self.base_url = base_url
def get_provider_thingy1(self):
return self.request('get', 'getproviderthingy1.do', response_type='json')
def get_provider_thingy2(self, myParams):
return self.request('get', 'getproviderthing2.do', {'myParam': myParam}, response_type='json')
 
def post_provider_thingy(self, data):
return self.request('put', 'postproviderthing.do', data=data)
 
d = MyDataProvider('http://example.com/')
d.get_provider_thingy2('param')

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.