diff --git a/geosyspy/utils/http_client.py b/geosyspy/utils/http_client.py index 70f0f0c..4e832bd 100644 --- a/geosyspy/utils/http_client.py +++ b/geosyspy/utils/http_client.py @@ -1,9 +1,13 @@ """ http client class""" + +import json + from oauthlib.oauth2 import TokenExpiredError from requests_oauthlib import OAuth2Session from . import oauth2_client + def renew_access_token(func): """Decorator used to wrap the Geosys class's http methods. @@ -22,6 +26,7 @@ def wrapper(self, *args, **kwargs): return wrapper + class HttpClient: """A class for making HTTP requests with OAuth2 authentication. @@ -32,15 +37,16 @@ class HttpClient: get_access_token(): Returns the access token. """ + def __init__( - self, - client_id: str, - client_secret: str, - username: str, - password: str, - enum_env: str, - enum_region: str, - bearer_token: str = None + self, + client_id: str, + client_secret: str, + username: str, + password: str, + enum_env: str, + enum_region: str, + bearer_token: str = None, ): self.__client_oauth = oauth2_client.Oauth2Api( client_id=client_id, @@ -49,15 +55,16 @@ def __init__( username=username, enum_env=enum_env, enum_region=enum_region, - bearer_token=bearer_token + bearer_token=bearer_token, ) self.access_token = self.__client_oauth.token - - self.__client = OAuth2Session(self.__client_oauth.client_id, - token=self.__client_oauth.token) + + self.__client = OAuth2Session( + self.__client_oauth.client_id, token=self.__client_oauth.token + ) @renew_access_token - def get(self, url_endpoint: str, headers=None, verify_ssl = True): + def get(self, url_endpoint: str, headers=None, verify_ssl=True): """Gets the url_endpopint. Args: @@ -71,7 +78,7 @@ def get(self, url_endpoint: str, headers=None, verify_ssl = True): return self.__client.get(url_endpoint, headers=headers, verify=verify_ssl) @renew_access_token - def post(self, url_endpoint: str, payload: dict, headers=None, verify_ssl = True): + def post(self, url_endpoint: str, payload: dict, headers=None, verify_ssl=True): """Posts payload to the url_endpoint. Args: @@ -83,10 +90,12 @@ def post(self, url_endpoint: str, payload: dict, headers=None, verify_ssl = True """ if headers is None: headers = {} - return self.__client.post(url_endpoint, json=payload, headers=headers, verify=verify_ssl) + return self.__client.post( + url_endpoint, json=payload, headers=headers, verify=verify_ssl + ) @renew_access_token - def patch(self, url_endpoint: str, payload: dict, verify_ssl = True): + def patch(self, url_endpoint: str, payload: dict, verify_ssl=True): """Patchs payload to the url_endpoint. Args: @@ -96,7 +105,11 @@ def patch(self, url_endpoint: str, payload: dict, verify_ssl = True): Returns: A response object. """ - return self.__client.patch(url_endpoint, json=payload, verify=verify_ssl) + headers = {"Content-Type": "application/json"} + json_payload = json.dumps(payload, allow_nan=True) + return self.__client.patch( + url_endpoint, data=json_payload, verify=verify_ssl, headers=headers + ) def get_access_token(self): """Returns the access token.