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) 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]