Improved the loggin output from response/request tyeps
This commit is contained in:
parent
1a7b7f0478
commit
dfd4c18402
@ -11,16 +11,20 @@ class Response:
|
|||||||
Response is wrapper for reading + extracting information we care about
|
Response is wrapper for reading + extracting information we care about
|
||||||
Primarily created by Requests that get `make`'d.
|
Primarily created by Requests that get `make`'d.
|
||||||
'''
|
'''
|
||||||
def __init__(self, url: str, body: str, code: int, expected: int, out=sys.stdout, color=True):
|
def __init__(self, method: str, url: str, body: str, code: int, expected: int, out=sys.stdout, color=True,
|
||||||
|
truncate_long_body=True):
|
||||||
|
|
||||||
|
self.method = method
|
||||||
|
self.url = url
|
||||||
self.body = body #typically a string before parsing anything
|
self.body = body #typically a string before parsing anything
|
||||||
self.code = code #u16
|
self.code = code #u16
|
||||||
self.expected = expected #u16
|
self.expected = expected #u16
|
||||||
|
|
||||||
self.out = out # file handle to write to normally sys.stdout
|
self.out = out # file handle to write to normally sys.stdout
|
||||||
self.color = color # bool telling if log should color anything (on by default)
|
self.color = color # bool telling if log should color anything (on by default)
|
||||||
|
self.truncate_long_body = truncate_long_body
|
||||||
|
|
||||||
def __color(self, cc, string):
|
def _color(self, cc, string):
|
||||||
nc = '\033[0m'
|
nc = '\033[0m'
|
||||||
return f'{cc}{string}{nc}'
|
return f'{cc}{string}{nc}'
|
||||||
|
|
||||||
@ -33,19 +37,38 @@ class Response:
|
|||||||
green = '\033[1;32m'
|
green = '\033[1;32m'
|
||||||
return self._color(green, string)
|
return self._color(green, string)
|
||||||
|
|
||||||
|
def __write_msg(self, s):
|
||||||
|
# mega dumb wrapper to reduce visual noise i think
|
||||||
|
if len(s) != 0: print(s, file=self.out)
|
||||||
|
|
||||||
|
def _log_body(self):
|
||||||
|
if self.truncate_long_body:
|
||||||
|
if len(self.body) > 80:
|
||||||
|
msg = self.body
|
||||||
|
while len(msg) > 80:
|
||||||
|
msg = msg[:len(msg)//2] + msg[len(msg)//2+1:]
|
||||||
|
msg = '.....'.join([msg[:40], msg[44]])
|
||||||
|
|
||||||
|
self.__write_msg(msg)
|
||||||
|
else:
|
||||||
|
self.__write_msg(f'\t{self.body}')
|
||||||
|
else:
|
||||||
|
self.__write_msg(f'\t{self.body}')
|
||||||
|
|
||||||
def log(self):
|
def log(self):
|
||||||
if self.code != self.expected:
|
if self.code != self.expected:
|
||||||
msg = f'Failed: {self.method.upper()} {self.url} -> got {self.code} expected {self.expected}'
|
msg = f'Failed {self.method.upper()} {self.url}\t{self.code} expected {self.expected}'
|
||||||
if self.color:
|
if self.color:
|
||||||
msg = self._color_failing(msg)
|
msg = self._color_failing(msg)
|
||||||
|
|
||||||
print(msg, file=self.out)
|
self.__write_msg(msg)
|
||||||
|
self._log_body(f'{self.body}')
|
||||||
else:
|
else:
|
||||||
msg = f'Passing: {self.method} {self.url}'
|
msg = f'Passing: {self.method} {self.url}'
|
||||||
if self.color:
|
if self.color:
|
||||||
msg = self._color_passing(msg)
|
msg = self._color_passing(msg)
|
||||||
|
|
||||||
print(msg, file=self.out)
|
self.__write_msg(msg)
|
||||||
|
|
||||||
def json(self):
|
def json(self):
|
||||||
'''
|
'''
|
||||||
@ -79,17 +102,19 @@ class Request:
|
|||||||
params = json.dumps(self.params)
|
params = json.dumps(self.params)
|
||||||
if method == 'get':
|
if method == 'get':
|
||||||
resp = requests.get(self.url, data=params)
|
resp = requests.get(self.url, data=params)
|
||||||
return Response(self.url, resp.text, resp.status_code, hope)
|
return Response('get', self.url, resp.text, resp.status_code, hope)
|
||||||
elif method == 'post':
|
elif method == 'post':
|
||||||
resp = requests.post(self.url, data=params)
|
resp = requests.post(self.url, data=params)
|
||||||
return Response(self.url, resp.text, resp.status_code, hope)
|
return Response('post', self.url, resp.text, resp.status_code, hope)
|
||||||
elif method == 'delete':
|
elif method == 'delete':
|
||||||
resp = requests.delete(self.url, data=params)
|
resp = requests.delete(self.url, data=params)
|
||||||
return Response(self.url, resp.text, resp.status_code, hope)
|
return Response('delete', self.url, resp.text, resp.status_code, hope)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise RequestError('Invalid method passed')
|
raise RequestError('Invalid method passed')
|
||||||
|
|
||||||
|
return resp
|
||||||
|
|
||||||
|
|
||||||
def make(self, hope: int) -> Response:
|
def make(self, hope: int) -> Response:
|
||||||
'''
|
'''
|
||||||
|
Loading…
Reference in New Issue
Block a user