import requests class RequestError(Exception): pass class Response: def __init__(self, body, code): self.body = body self.code = code def __str__(self): return f'{self.code} => {self.body}' class Request: def __init__(self, domain: str, params: dict): assert(domain[len(domain) - 1] != '/') self.domain = domain self.params = params def _make_request(self, method: str, path: str, hope: int): # Lower driver for actuall making the request we are looking for assert(path.startswith('/')) method = method.lower() url = self.domain + path if method == 'get': resp = requests.get(url, data=self.params) return Response(resp.body, resp.status_code, hope) elif method == 'post': resp = requests.post(url, data=self.params) return Response(resp.body, resp.status_code, hope) elif method == 'delete': resp = requests.delete(url, data=self.params) return Response(resp.body, resp.status_code, hope) else: raise RequestError('Invalid method passed') def get(self, path: str, hope: int): return self._make_request('get', path, hope) def post(self, path: str, hope: int): return self._make_request('post', path, hope) def delete(self, path: str, hope: int): return self._make_request('delete', path, hope)