Skip to content

Commit

Permalink
refactor(client.discover): add hint error when discovery document v1 …
Browse files Browse the repository at this point in the history
…fails
  • Loading branch information
dfguerrerom committed Aug 26, 2024
1 parent c60370c commit 768d127
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions aiogoogle/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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``
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 768d127

Please sign in to comment.