Last active
February 7, 2023 02:30
-
-
Save FernandoCelmer/d77354ae4d073b5d2ee4c196e6f72164 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from json import dumps | |
from urllib import parse | |
from http.client import HTTPSConnection | |
class Request: | |
def __init__(self, url: str = '127.0.0.1') -> None: | |
self.url = url | |
self.local = None | |
self.path = None | |
self.params = {} | |
self.headers = {} | |
self.setup() | |
def setup(self) -> None: | |
self.init_local() | |
self.init_path() | |
self.init_headers() | |
self.connection = HTTPSConnection(self.local) | |
def init_local(self) -> None: | |
self.local = parse.urlsplit(self.url).netloc | |
def init_path(self) -> None: | |
self.path = parse.urlsplit(self.url).path | |
def init_headers(self) -> None: | |
self.headers = {'Content-Type': 'application/json'} | |
def request( | |
self, | |
method: str, | |
body: dict = {}, | |
params: dict = {}, | |
headers: dict = {}): | |
"""Base Request | |
""" | |
try: | |
self.connection.request( | |
method=method, | |
body=dumps(body), | |
url=self.path + parse.urlencode(params), | |
headers=headers if headers else self.headers, | |
) | |
with self.connection.getresponse() as response: | |
return response.read() | |
except Exception: | |
raise ValueError("Failed to make the request") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment