Skip to content

Commit

Permalink
Merge pull request #67 from equinor/219935-callsumoapi-to-sumoclient
Browse files Browse the repository at this point in the history
Added blob_client for uploading blob
  • Loading branch information
thezultimate authored May 31, 2022
2 parents f6e6114 + f098f8a commit 5922f9a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 19 deletions.
21 changes: 21 additions & 0 deletions src/sumo/wrapper/_blob_client.py
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions src/sumo/wrapper/_request_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
33 changes: 14 additions & 19 deletions src/sumo/wrapper/_sumo_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__(
Expand All @@ -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)
Expand All @@ -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})
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)

0 comments on commit 5922f9a

Please sign in to comment.