diff options
author | Frederick Muriuki Muriithi | 2024-01-29 06:48:06 +0300 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2024-01-30 07:03:12 +0300 |
commit | 0bc0bd0673f8c167558b62645cbba652f329ab08 (patch) | |
tree | e27cc3c6c58c48ecb3ab211edc279d65fee75645 /gn2/wqflask/oauth2 | |
parent | 7e378f32807dc42fc2d87d6697f05a08f96423ed (diff) | |
download | genenetwork2-0bc0bd0673f8c167558b62645cbba652f329ab08.tar.gz |
Create framework for error handling and handle connection errors
Diffstat (limited to 'gn2/wqflask/oauth2')
-rw-r--r-- | gn2/wqflask/oauth2/client.py | 27 |
1 files changed, 18 insertions, 9 deletions
diff --git a/gn2/wqflask/oauth2/client.py b/gn2/wqflask/oauth2/client.py index c6a3110b..ed4dbbbf 100644 --- a/gn2/wqflask/oauth2/client.py +++ b/gn2/wqflask/oauth2/client.py @@ -1,4 +1,5 @@ """Common oauth2 client utilities.""" +import uuid import json import requests from typing import Any, Optional @@ -11,6 +12,7 @@ from authlib.integrations.requests_client import OAuth2Session from gn2.wqflask.oauth2 import session from gn2.wqflask.oauth2.checks import user_logged_in +from gn2.wqflask.external_errors import ExternalRequestError SCOPE = ("profile group role resource register-client user masquerade " "introspect migrate-data") @@ -76,10 +78,14 @@ def oauth2_post( def no_token_get(uri_path: str, **kwargs) -> Either: from gn2.utility.tools import AUTH_SERVER_URL - resp = requests.get(urljoin(AUTH_SERVER_URL, uri_path), **kwargs) - if resp.status_code == 200: - return Right(resp.json()) - return Left(resp) + uri = urljoin(AUTH_SERVER_URL, uri_path) + try: + resp = requests.get(uri, **kwargs) + if resp.status_code == 200: + return Right(resp.json()) + return Left(resp) + except requests.exceptions.RequestException as exc: + raise ExternalRequestError(uri, exc) from exc def no_token_post(uri_path: str, **kwargs) -> Either: from gn2.utility.tools import ( @@ -99,11 +105,14 @@ def no_token_post(uri_path: str, **kwargs) -> Either: }, ("data" if bool(data) else "json"): request_data } - resp = requests.post(urljoin(AUTH_SERVER_URL, uri_path), - **new_kwargs) - if resp.status_code == 200: - return Right(resp.json()) - return Left(resp) + try: + resp = requests.post(urljoin(AUTH_SERVER_URL, uri_path), + **new_kwargs) + if resp.status_code == 200: + return Right(resp.json()) + return Left(resp) + except requests.exceptions.RequestException as exc: + raise ExternalRequestError(uri, exc) from exc def post(uri_path: str, **kwargs) -> Either: """ |