Skip to content

Commit

Permalink
Merge pull request #148 from dfguerrerom/discovery_v2_error
Browse files Browse the repository at this point in the history
Add hint error when discovery document v1 fails
  • Loading branch information
omarryhan authored Sep 3, 2024
2 parents c60370c + e0e6d1c commit 1118d85
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 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
2 changes: 1 addition & 1 deletion tests/refresh_all_apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 1118d85

Please sign in to comment.