Skip to content

Commit

Permalink
Handle errors more like requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr0grog committed Oct 4, 2024
1 parent f7c6166 commit 099154c
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions scripts/lib/zoom.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,13 @@ def __init__(self, response, message=None):
self.data = {}

self.message = message or self.data.pop('message', 'Zoom API error')
self.code = self.data.pop('code')
self.code = self.data.pop('code', 0)
super().__init__(
f'{self.message} '
f'(code={self.code}, http_status={self.response.status_code}) '
f'Check the docs for details: {ZOOM_DOCS_URL}.'
)

@classmethod
def raise_for_response(cls, response: Response) -> Response:
if response.status_code >= 400:
raise cls(response)

return response


class ZoomResponse:
response: Response
Expand All @@ -61,16 +54,28 @@ def data(self):
def text(self):
return self.response.text

def raise_for_status(self):
self.raise_for_response(self.response)

@classmethod
def is_error(cls, response: Response) -> bool:
return response.status_code >= 400

@classmethod
def raise_for_response(cls, response: Response):
if cls.is_error(response):
raise ZoomError(response)


def wrap_method_with_parsing(original):
@wraps(original)
def wrapper(*args, **kwargs):
result = original(*args, **kwargs)
if isinstance(result, Response):
ZoomError.raise_for_response(result)
return ZoomResponse(result)
else:
return result
result = ZoomResponse(result)
result.raise_for_status()

return result

return wrapper

Expand Down Expand Up @@ -119,7 +124,7 @@ def get_file(self, url: str) -> Response:
response = requests.get(url, stream=True, headers={
'Authorization': f'Bearer {self.config['token']}'
})
ZoomError.raise_for_response(response)
ZoomResponse.raise_for_response(response)
return response

def download_file(self, url: str, download_directory: str) -> str:
Expand Down

0 comments on commit 099154c

Please sign in to comment.