-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(connector): [#396] Removing dependency on the Requests library
- Loading branch information
1 parent
e6cbb33
commit 255786f
Showing
5 changed files
with
149 additions
and
40 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
""" | ||
This module contains common utilities used by the connector | ||
""" | ||
from typing import Any, Dict, Optional | ||
import http.client | ||
import urllib.parse | ||
import json | ||
|
||
|
||
class Request: | ||
""" | ||
Provides a wrapper for the python http.client, | ||
to be used similar to the requests library. | ||
Parameters | ||
---------- | ||
_url: The requesting end-point URL. | ||
""" | ||
|
||
def __init__(self, _url: str,) -> None: | ||
self.url = urllib.parse.urlparse(_url) | ||
self.hostname = self.url.hostname | ||
self.path = self.url.path | ||
self.headers = {"user-agent": "node.js"} | ||
|
||
def get(self, _headers: Optional[Dict[str, Any]] = "") -> Dict[str, Any]: | ||
""" | ||
GET request to the specified end-point. | ||
Parameters | ||
---------- | ||
_headers: Any additional headers to be passed | ||
""" | ||
self.headers.update(_headers) | ||
conn = http.client.HTTPSConnection(self.hostname) | ||
conn.request(method="GET", url=self.path, headers=self.headers) | ||
response = conn.getresponse() | ||
|
||
return response | ||
|
||
def post( | ||
self, _headers: Optional[Dict[str, Any]] = "", _data: Optional[Dict[str, Any]] = "" | ||
) -> Dict[str, Any]: | ||
""" | ||
POST request to the specified end-point. | ||
Parameters | ||
---------- | ||
_headers: Any additional headers to be passed | ||
_data: Body of the request | ||
""" | ||
self.headers.update(_headers) | ||
conn = http.client.HTTPSConnection(self.hostname) | ||
conn.request( | ||
method="POST", url=self.path, headers=self.headers, body=urllib.parse.urlencode(_data) | ||
) | ||
response = conn.getresponse() | ||
|
||
return response | ||
|
||
def put( | ||
self, _headers: Optional[Dict[str, Any]] = "", _data: Optional[Dict[str, Any]] = "" | ||
) -> Dict[str, Any]: | ||
""" | ||
PUT request to the specified end-point. | ||
Parameters | ||
---------- | ||
_headers: Any additional headers to be passed | ||
_data: Body of the request | ||
""" | ||
self.headers.update(_headers) | ||
conn = http.client.HTTPSConnection(self.hostname) | ||
conn.request( | ||
method="PUT", url=self.path, headers=self.headers, body=urllib.parse.urlencode(_data) | ||
) | ||
response = conn.getresponse() | ||
|
||
return response |
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