freechat/server-api/client-tests/web/http.py
shockrah fc74a3dbc7 Moving http logic to its own module under a new 'web' module
More interfaces for the testing client will be built to better analyze responses in next patches
2021-01-20 16:56:54 -08:00

51 lines
1.4 KiB
Python

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)