diff --git a/src/sumo/wrapper/_blob_client.py b/src/sumo/wrapper/_blob_client.py new file mode 100644 index 0000000..b551f0c --- /dev/null +++ b/src/sumo/wrapper/_blob_client.py @@ -0,0 +1,21 @@ +import requests +from ._request_error import raise_request_error_exception + +class BlobClient: + + def upload_blob(self, blob, url): + headers = { + "Content-Type": "application/octet-stream", + "Content-Length": str(len(blob)), + "x-ms-blob-type": "BlockBlob", + } + + try: + response = requests.put(url, data=blob, headers=headers) + except requests.exceptions.ProxyError as err: + raise_request_error_exception(503, err) + + if not response.ok: + raise_request_error_exception(response.status_code, response.text) + + return response \ No newline at end of file diff --git a/src/sumo/wrapper/_request_error.py b/src/sumo/wrapper/_request_error.py index e039094..694dc67 100644 --- a/src/sumo/wrapper/_request_error.py +++ b/src/sumo/wrapper/_request_error.py @@ -29,3 +29,15 @@ def __init__(self, code, message): def __str__(self): return f'Fatal Request Error with status code {self.code} and text {self.message}.' + +def raise_request_error_exception(code, message): + """ + Raise the proper authentication error according to the code received from sumo. + """ + + if 503 <= code <= 504 or code == 404 or code == 500: + raise TransientError(code, message) + elif 401 <= code <= 403: + raise AuthenticationError(code, message) + else: + raise PermanentError(code, message) \ No newline at end of file diff --git a/src/sumo/wrapper/_sumo_client.py b/src/sumo/wrapper/_sumo_client.py index 40f9fda..d8ee7bd 100644 --- a/src/sumo/wrapper/_sumo_client.py +++ b/src/sumo/wrapper/_sumo_client.py @@ -4,7 +4,8 @@ from .config import APP_REGISTRATION, TENANT_ID from ._new_auth import NewAuth -from ._request_error import AuthenticationError, TransientError, PermanentError +from ._request_error import raise_request_error_exception +from ._blob_client import BlobClient class SumoClient: def __init__( @@ -19,6 +20,7 @@ def __init__( self.access_token = None self.access_token_expires = None self.refresh_token = None + self._blob_client = BlobClient() if token: payload = self.__decode_token(token) @@ -43,6 +45,11 @@ def __init__( self.base_url = f"https://main-sumo-{env}.radix.equinor.com/api/v1" + @property + def blob_client(self): + return self._blob_client + + def __decode_token(self, token): try: payload = jwt.decode(token, options={"verify_signature": False}) @@ -85,7 +92,7 @@ def get(self, path, **params): ) if not response.ok: - self._raise_request_error_exception( + raise_request_error_exception( response.status_code, response.text) if "/blob" in path: @@ -116,10 +123,10 @@ def post(self, path, blob=None, json=None): headers=headers ) except requests.exceptions.ProxyError as err: - self._raise_request_error_exception(503, err) + raise_request_error_exception(503, err) if not response.ok: - self._raise_request_error_exception( + raise_request_error_exception( response.status_code, response.text) return response @@ -147,10 +154,10 @@ def put(self, path, blob=None, json=None): headers=headers ) except requests.exceptions.ProxyError as err: - self. _raise_request_error_exception(503, err) + raise_request_error_exception(503, err) if not response.ok: - self._raise_request_error_exception( + raise_request_error_exception( response.status_code, response.text) return response @@ -166,19 +173,7 @@ def delete(self, path): response = requests.delete(f'{self.base_url}{path}', headers=headers) if not response.ok: - self._raise_request_error_exception( + raise_request_error_exception( response.status_code, response.text) return response.json() - - def _raise_request_error_exception(self, code, message): - """ - Raise the proper authentication error according to the code received from sumo. - """ - - if 503 <= code <= 504 or code == 404 or code == 500: - raise TransientError(code, message) - elif 401 <= code <= 403: - raise AuthenticationError(code, message) - else: - raise PermanentError(code, message)