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
This commit is contained in:
shockrah 2021-01-20 16:56:54 -08:00
parent c61c57c1b8
commit fc74a3dbc7
4 changed files with 53 additions and 49 deletions

View File

@ -0,0 +1 @@
from . import web

View File

@ -1,55 +1,7 @@
import time
import subprocess, os, sys
import json, 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(path[len(path) - 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)
from web import http
class Test:

View File

@ -0,0 +1 @@
from . import http

View File

@ -0,0 +1,50 @@
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)