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:
parent
c61c57c1b8
commit
fc74a3dbc7
1
server-api/client-tests/__init__.py
Normal file
1
server-api/client-tests/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
from . import web
|
@ -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:
|
||||
|
1
server-api/client-tests/web/__init__.py
Normal file
1
server-api/client-tests/web/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
from . import http
|
50
server-api/client-tests/web/http.py
Normal file
50
server-api/client-tests/web/http.py
Normal 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)
|
||||
|
Loading…
Reference in New Issue
Block a user