From bd5b2bbbaea000004c6c6124aad5000ac4db52f7 Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Sat, 23 Dec 2023 11:13:54 -0500 Subject: [PATCH] feat: add ability to send custom headers with requests through restadapter --- pyzipline/rest_adapter.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/pyzipline/rest_adapter.py b/pyzipline/rest_adapter.py index 04876ec..5dc40cc 100644 --- a/pyzipline/rest_adapter.py +++ b/pyzipline/rest_adapter.py @@ -34,12 +34,13 @@ class RestAdapter: if not ssl and not enforced_signing: 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. Args: 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. + headers (dict): Python dictionary of headers 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. 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. """ 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 # 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) - 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. Args: @@ -90,9 +93,9 @@ class RestAdapter: Returns: 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. Args: @@ -103,9 +106,9 @@ class RestAdapter: Returns: 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. Args: @@ -118,13 +121,14 @@ class RestAdapter: Returns: 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. Args: 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. json (dict): Python dictionary of JSON-serializable data to send with the request. files (dict): Python dictionary of files to send with the request. @@ -133,4 +137,4 @@ class RestAdapter: Returns: 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)