import requests class Client(): """ A generic API client. """ def __init__(self, url, verify=True, auth=None): """ Constructs a new Client object. """ self.url = url self.verify = verify self.auth = auth def request(self, scheme, url, data=None, params=None): """ Low-level request interface to client. Takes a HTTP request scheme (lower case!), a URL to request (relative), and optionally data to add to the request. Either returns the JSON body of the request or raises a HttpException. """ url = self.url + url headers = { "User-Agent": "Autogenerated API wrapper (generated by api_builder.py)", "Content-Type": "application/json", } # this is a nice little hack to make the API nicer # we pass the scheme as string, but have it as attributes in requests fn = requests.__getattribute__(scheme) res = fn(url, headers=headers, json=data, params=params, verify=self.verify, auth=self.auth) res.raise_for_status() if not res.content: return None try: return res.json() except ValueError: return res.content def get(self, url, data=None, params=None): """ Low-level GET request interface to client. Takes a URL to request (relative), and optionally data to add to the request. Either returns the JSON body of the request or raises a HttpException. """ return self.request("get", url, data, params) def put(self, url, data=None, params=None): """ Low-level PUT request interface to client. Takes a URL to request (relative), and optionally data to add to the request. Either returns the JSON body of the request or raises a HttpException. """ return self.request("put", url, data, params) def post(self, url, data=None, params=None): """ Low-level POST request interface to client. Takes a URL to request (relative), and optionally data to add to the request. Either returns the JSON body of the request or raises a HttpException. """ return self.request("post", url, data, params) def patch(self, url, data=None, params=None): """ Low-level PATCH request interface to client. Takes a URL to request (relative), and optionally data to add to the request. Either returns the JSON body of the request or raises a HttpException. """ return self.request("patch", url, data, params) def delete(self, url, data=None, params=None): """ Low-level DELETE request interface to client. Takes a URL to request (relative), and optionally data to add to the request. Either returns the JSON body of the request or raises a HttpException. """ return self.request("delete", url, data, params)