proiect finalizat
This commit is contained in:
@@ -1,3 +1,9 @@
|
||||
from .request_builder import RequestBuilder
|
||||
|
||||
request_builder = RequestBuilder()
|
||||
|
||||
"""
|
||||
The 'http' package contains the tools for making HTTP requests to the backend. This package contains:
|
||||
- request_builder: class through which the request is build
|
||||
- request_handler: object resulted from the builder and can be used to make requests.
|
||||
"""
|
||||
|
||||
@@ -2,6 +2,18 @@ from .request_handler import _HttpRequestHandler
|
||||
|
||||
|
||||
class RequestBuilder:
|
||||
"""
|
||||
A Builder for HTTP Request class
|
||||
|
||||
Attributes:
|
||||
__url (str): URL at which the request is made
|
||||
__headers (dict):
|
||||
__body (dict):
|
||||
__params (dict): Query params
|
||||
__method (str): HTTP method (GET, POST, ...)
|
||||
"""
|
||||
|
||||
|
||||
def __init__(self):
|
||||
self.__url = ""
|
||||
self.__headers = {}
|
||||
@@ -9,26 +21,27 @@ class RequestBuilder:
|
||||
self.__params = None
|
||||
self.__method = "GET"
|
||||
|
||||
def set_url(self, url=str):
|
||||
def set_url(self, url):
|
||||
self.__url = url
|
||||
return self
|
||||
|
||||
def set_headers(self, headers=dict):
|
||||
def set_headers(self, headers):
|
||||
self.__headers = headers
|
||||
return self
|
||||
|
||||
def set_body(self, body=dict):
|
||||
def set_body(self, body):
|
||||
self.__body = body
|
||||
return self
|
||||
|
||||
def set_params(self, params=dict):
|
||||
def set_params(self, params):
|
||||
self.__params = params
|
||||
return self
|
||||
|
||||
def set_method(self, method=str):
|
||||
def set_method(self, method):
|
||||
self.__method = method
|
||||
return self
|
||||
|
||||
def build(self):
|
||||
"""Based on the set parameters it returns a Request Handler object"""
|
||||
return _HttpRequestHandler(url=self.__url, method=self.__method, body=self.__body,
|
||||
params=self.__params, headers=self.__headers)
|
||||
|
||||
@@ -3,16 +3,26 @@ import requests
|
||||
|
||||
|
||||
class _HttpRequestHandler:
|
||||
"""
|
||||
A Builder for HTTP Request class
|
||||
|
||||
Attributes:
|
||||
url (str): URL at which the request is made
|
||||
headers (dict):
|
||||
body (dict):
|
||||
params (dict): Query params
|
||||
method (str): HTTP method (GET, POST, ...)
|
||||
"""
|
||||
|
||||
def __init__(self, url, method="GET", params=None, headers=None, body=None):
|
||||
self.url = url
|
||||
self.method = method
|
||||
self.headers = headers
|
||||
print(body)
|
||||
self.body = json.dumps(body)
|
||||
self.params = params
|
||||
|
||||
def make_request(self):
|
||||
print(self.body)
|
||||
"""Make HTTP request and returns the answer"""
|
||||
response = requests.request(method=self.method, url=self.url,
|
||||
params=self.params, data=self.body, headers=self.headers)
|
||||
return response
|
||||
|
||||
Reference in New Issue
Block a user