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 Aug 31, 2024
1 parent ad1a70f commit db8cdba
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions scripts/lib/zoom.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@ def __init__(self, response, message=None):
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,24 @@ def data(self):
def text(self):
return self.response.text

def raise_for_status(self):
if ZoomResponse.is_error(self.response):
raise ZoomError(self.response)

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


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 @@ -116,10 +117,10 @@ def get_file(self, url: str) -> Response:
# not explicitly private, either. Use `config[]` syntax instead of
# `config.get()` so we get an exception if things have changed and
# this data is no longer available.
response = requests.get(url, stream=True, headers={
response = ZoomResponse(requests.get(url, stream=True, headers={
'Authorization': f'Bearer {self.config['token']}'
})
ZoomError.raise_for_response(response)
}))
response.raise_for_status()
return response

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

0 comments on commit db8cdba

Please sign in to comment.