feat: add ability to send custom headers with requests through restadapter

This commit is contained in:
Seaswimmer 2023-12-23 11:13:54 -05:00
parent 63e83a7703
commit bd5b2bbbae
Signed by: cswimr
GPG key ID: 1EBC234EEDA901AE

View file

@ -34,12 +34,13 @@ class RestAdapter:
if not ssl and not enforced_signing: if not ssl and not enforced_signing:
disable_warnings() disable_warnings()
def _do(self, http_method: str, endpoint: str, params: dict = None, json: dict = None, files: dict = None, timeout: float = 60) -> Result: def _do(self, http_method: str, endpoint: str, headers: dict = None, params: dict = None, json: dict = None, files: dict = None, timeout: float = 60) -> Result:
"""Internal method to make a request to the Zipline server. You shouldn't use this directly. """Internal method to make a request to the Zipline server. You shouldn't use this directly.
Args: Args:
http_method (str): The [HTTP method](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods) to use http_method (str): The [HTTP method](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods) to use
endpoint (str): The endpoint to make the request to. endpoint (str): The endpoint to make the request to.
headers (dict): Python dictionary of headers to send with the request.
params (dict): Python dictionary of query parameters to send with the request. params (dict): Python dictionary of query parameters to send with the request.
json (dict): Python dictionary of JSON-serializable data to send with the request. json (dict): Python dictionary of JSON-serializable data to send with the request.
files (dict): Python dictionary of files to send with the request. files (dict): Python dictionary of files to send with the request.
@ -53,7 +54,9 @@ class RestAdapter:
PyZiplineError: Raised when an error occurs in the PyZipline library. PyZiplineError: Raised when an error occurs in the PyZipline library.
""" """
full_url = self._url + endpoint full_url = self._url + endpoint
headers = {'Authorization': self._token} if not headers:
headers = {}
headers.update({'Authorization': self._token})
try: # Perform an HTTP request, catching and re-raising any exceptions try: # Perform an HTTP request, catching and re-raising any exceptions
# will eventually refactor this to use asyncio/aiohttp instead for async operation # will eventually refactor this to use asyncio/aiohttp instead for async operation
@ -78,7 +81,7 @@ class RestAdapter:
return Result(success=is_success, status_code=response.status_code, message=data_out['error'], data=data_out) return Result(success=is_success, status_code=response.status_code, message=data_out['error'], data=data_out)
def delete(self, endpoint: str, params: dict = None, json: dict = None, timeout: float = 60) -> Result: def delete(self, endpoint: str, headers: dict = None, params: dict = None, json: dict = None, timeout: float = 60) -> Result:
"""Make a DELETE request to the Zipline server. You should almost never have to use this directly. """Make a DELETE request to the Zipline server. You should almost never have to use this directly.
Args: Args:
@ -90,9 +93,9 @@ class RestAdapter:
Returns: Returns:
Result: A Result object containing the status code, message, and data from the request. Result: A Result object containing the status code, message, and data from the request.
""" """
return self._do(http_method='DELETE', endpoint=endpoint, params=params, json=json, timeout=timeout) return self._do(http_method='DELETE', endpoint=endpoint, headers=headers, params=params, json=json, timeout=timeout)
def get(self, endpoint: str, params: dict = None, timeout: float = 60) -> Result: def get(self, endpoint: str, headers: dict = None, params: dict = None, timeout: float = 60) -> Result:
"""Make a GET request to the Zipline server. You should almost never have to use this directly. """Make a GET request to the Zipline server. You should almost never have to use this directly.
Args: Args:
@ -103,9 +106,9 @@ class RestAdapter:
Returns: Returns:
Result: A Result object containing the status code, message, and data from the request. Result: A Result object containing the status code, message, and data from the request.
""" """
return self._do(http_method='GET', endpoint=endpoint, params=params, timeout=timeout) return self._do(http_method='GET', endpoint=endpoint, headers=headers, params=params, timeout=timeout)
def patch(self, endpoint: str, params: dict = None, json: dict = None, files: dict = None, timeout: float = 60) -> Result: def patch(self, endpoint: str, headers: dict = None, params: dict = None, json: dict = None, files: dict = None, timeout: float = 60) -> Result:
"""Make a PATCH request to the Zipline server. You should almost never have to use this directly. """Make a PATCH request to the Zipline server. You should almost never have to use this directly.
Args: Args:
@ -118,13 +121,14 @@ class RestAdapter:
Returns: Returns:
Result: A Result object containing the status code, message, and data from the request. Result: A Result object containing the status code, message, and data from the request.
""" """
return self._do(http_method='PATCH', endpoint=endpoint, params=params, json=json, files=files, timeout=timeout) return self._do(http_method='PATCH', endpoint=endpoint, headers=headers, params=params, json=json, files=files, timeout=timeout)
def post(self, endpoint: str, params: dict = None, json: dict = None, files: dict = None, timeout: float = 60) -> Result: def post(self, endpoint: str, headers: dict = None, params: dict = None, json: dict = None, files: dict = None, timeout: float = 60) -> Result:
"""Make a POST request to the Zipline server. You should almost never have to use this directly. """Make a POST request to the Zipline server. You should almost never have to use this directly.
Args: Args:
endpoint (str): The endpoint to make the request to. endpoint (str): The endpoint to make the request to.
headers (dict): Python dictionary of headers to send with the request.
params (dict): Python dictionary of query parameters to send with the request. params (dict): Python dictionary of query parameters to send with the request.
json (dict): Python dictionary of JSON-serializable data to send with the request. json (dict): Python dictionary of JSON-serializable data to send with the request.
files (dict): Python dictionary of files to send with the request. files (dict): Python dictionary of files to send with the request.
@ -133,4 +137,4 @@ class RestAdapter:
Returns: Returns:
Result: A Result object containing the status code, message, and data from the request. Result: A Result object containing the status code, message, and data from the request.
""" """
return self._do(http_method='POST', endpoint=endpoint, params=params, json=json, files=files, timeout=timeout) return self._do(http_method='POST', endpoint=endpoint, headers=headers, params=params, json=json, files=files, timeout=timeout)