From 768d1275042b2b9174a31c0cfc5c66d04884d605 Mon Sep 17 00:00:00 2001 From: dfguerrerom Date: Mon, 26 Aug 2024 09:51:59 -0700 Subject: [PATCH 1/2] refactor(client.discover): add hint error when discovery document v1 fails --- aiogoogle/client.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/aiogoogle/client.py b/aiogoogle/client.py index d471002..de86884 100644 --- a/aiogoogle/client.py +++ b/aiogoogle/client.py @@ -3,12 +3,13 @@ __all__ = ["Aiogoogle"] from contextvars import ContextVar -from typing import TYPE_CHECKING, Any, Optional, Type +from typing import TYPE_CHECKING, Any, Literal, Optional, Type from .resource import GoogleAPI from .auth.managers import Oauth2Manager, ApiKeyManager, OpenIdConnectManager, ServiceAccountManager from .sessions.aiohttp_session import AiohttpSession from .data import DISCOVERY_SERVICE_V1_DISCOVERY_DOC +from .excs import HTTPError if TYPE_CHECKING: from .auth.creds import ApiKey, ClientCreds, ServiceAccountCreds, UserCreds @@ -153,7 +154,7 @@ async def list_api(self, name, preferred=None, fields=None): ) return await self.as_anon(request) - async def discover(self, api_name: str, api_version: Optional[str] = None, validate: bool = False, *, disco_doc_ver: Optional[int] = None) -> GoogleAPI: + async def discover(self, api_name: str, api_version: Optional[str] = None, validate: bool = False, *, disco_doc_ver: Optional[Literal[2]] = None) -> GoogleAPI: """ Donwloads a discovery document from Google's Discovery Service V1 and sets it a ``aiogoogle.resource.GoogleAPI`` @@ -206,7 +207,16 @@ async def discover(self, api_name: str, api_version: Optional[str] = None, valid api=api_name, api_version=api_version ) - discovery_document = await self.as_anon(request) + try: + discovery_document = await self.as_anon(request) + + except Exception as e: + if isinstance(e, HTTPError): + if not disco_doc_ver: + raise ValueError( + f"Failed to fetch discovery document from v1 of the Google Discovery Service. Consider setting `disco_doc_ver=2` parmeter. Error: {e}." + ) + raise e return GoogleAPI(discovery_document, validate) From e0e6d1c9395bbb1671e846a936bbbd828e75eb04 Mon Sep 17 00:00:00 2001 From: Omar Ryhan Date: Tue, 3 Sep 2024 13:59:57 +0800 Subject: [PATCH 2/2] fix tests --- tests/refresh_all_apis.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/refresh_all_apis.py b/tests/refresh_all_apis.py index 3b1b584..74446cc 100755 --- a/tests/refresh_all_apis.py +++ b/tests/refresh_all_apis.py @@ -35,7 +35,7 @@ async def refresh_disc_docs_json(): # Refresh discovery files in tests/data for google_api, (name, version) in zip(all_discovery_documents, all_apis): - if isinstance(google_api, HTTPError): + if isinstance(google_api, (HTTPError, ValueError)): e = google_api # filter out the errored api from the final_all_apis final_all_apis = [api for api in final_all_apis if api[0] != name]