! First new colorng mechanism is added for better human readability
+ Turning _make_requests into a lower level driver which now creates Response types with its own self.url now + Request.make is now smarter and uses its own state to dispatch the proper request Emits a respective Response object as well
This commit is contained in:
parent
d31b02089e
commit
5446d70d84
@ -1,50 +1,89 @@
|
||||
import sys
|
||||
import requests
|
||||
import time
|
||||
|
||||
class RequestError(Exception):
|
||||
pass
|
||||
|
||||
class Response:
|
||||
def __init__(self, body, code):
|
||||
self.body = body
|
||||
self.code = code
|
||||
'''
|
||||
Response is wrapper for reading + extracting information we care about
|
||||
Primarily created by Requests that get `make`'d.
|
||||
'''
|
||||
def __init__(self, url: str, body: str, code: int, expected: int, out=sys.stdout, color=True):
|
||||
|
||||
self.body = body #typically a string before parsing anything
|
||||
self.code = code #u16
|
||||
self.expected = expected #u16
|
||||
|
||||
self.out = out # file handle to write to normally sys.stdout
|
||||
self.color = color # bool telling if log should color anything (on by default)
|
||||
|
||||
def __color(self, cc, string):
|
||||
nc = '\033[0m'
|
||||
return f'{cc}{string}{nc}'
|
||||
|
||||
def _color_failing(self, string):
|
||||
red = '\033[1;31m'
|
||||
return self._color(red, string)
|
||||
|
||||
|
||||
def _color_passing(self, string):
|
||||
green = '\033[1;32m'
|
||||
return self._color(green, string)
|
||||
|
||||
def log(self):
|
||||
if self.code != self.expected:
|
||||
msg = f'Failed: {self.method.upper()} {self.url} -> got {self.code} expected {self.expected}'
|
||||
if self.color:
|
||||
msg = self._color_failing(msg)
|
||||
|
||||
print(msg, file=self.out)
|
||||
else:
|
||||
msg = f'Passing: {self.method} {self.url}'
|
||||
if self.color:
|
||||
msg = self._color_passing(msg)
|
||||
|
||||
print(msg, file=self.out)
|
||||
|
||||
def __str__(self):
|
||||
'''
|
||||
Returns: str(Response) -> `code => response.bdoy`
|
||||
'''
|
||||
return f'{self.code} => {self.body}'
|
||||
|
||||
class Request:
|
||||
def __init__(self, domain: str, params: dict):
|
||||
def __init__(self, method: str, url: str, params: dict):
|
||||
assert(domain[len(domain) - 1] != '/')
|
||||
assert(method in ['get', 'post', 'delete'])
|
||||
|
||||
self.domain = domain
|
||||
self.method = method
|
||||
self.url = url
|
||||
self.params = params
|
||||
|
||||
def _make_request(self, method: str, path: str, hope: int):
|
||||
def _make_request(self, method: 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)
|
||||
resp = requests.get(self.url, data=self.params)
|
||||
return Response(self.url, resp.body, resp.status_code, hope)
|
||||
elif method == 'post':
|
||||
resp = requests.post(url, data=self.params)
|
||||
return Response(resp.body, resp.status_code, hope)
|
||||
resp = requests.post(self.url, data=self.params)
|
||||
return Response(self.url, resp.body, resp.status_code, hope)
|
||||
elif method == 'delete':
|
||||
resp = requests.delete(url, data=self.params)
|
||||
return Response(resp.body, resp.status_code, hope)
|
||||
resp = requests.delete(self.url, data=self.params)
|
||||
return Response(self.url, resp.body, resp.status_code, hope)
|
||||
|
||||
else:
|
||||
raise RequestError('Invalid method passed')
|
||||
|
||||
|
||||
def make(self, hope: int) -> Response:
|
||||
'''
|
||||
@param hope: int -> status code we hope to get back
|
||||
@return Response -> Wrapper around server http response
|
||||
'''
|
||||
return self._make_request(self.method, hope)
|
||||
|
||||
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)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user