Skip to content

Commit 0c5b966

Browse files
committed
chore(discovery): make the SDK use static discovery files
GAE runtime will soon enough be decommissionned. To allow API discovery to continue working without breaking change, make the SDK rely on static discovery files hosted on LumApps CDN.
1 parent e25c852 commit 0c5b966

File tree

2 files changed

+79
-6
lines changed

2 files changed

+79
-6
lines changed

lumapps/api/base_client.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
Tuple,
1616
Union,
1717
)
18+
from urllib.parse import urlparse
1819

1920
from httpx import Client, HTTPStatusError
2021

@@ -59,15 +60,38 @@ def fetch_access_token(
5960
raise GetTokenError(str(err)) from err
6061

6162

62-
def _get_urls(base_url: str, api_info: Dict[str, str]) -> Tuple[str, str]:
63+
def _get_discovery_urls(base_url: str, api_info: Dict[str, str]) -> Tuple[str, str]:
6364
api_name = api_info.get("name", LUMAPPS_NAME)
6465
version = api_info.get("version", "v1")
6566
prefix = "" if api_name in GOOGLE_APIS else "/_ah/api"
6667
api_url = f"{prefix}/{api_name}/{version}"
6768

6869
if api_name == LUMAPPS_NAME:
70+
api_host = urlparse(base_url).netloc.split(".")
71+
if len(api_host) < 4:
72+
raise BaseClientError(f"Invalid base URL: {base_url}")
73+
is_beta_cell = api_host[1] == "beta"
74+
cell = api_host[0]
75+
if is_beta_cell and cell not in (
76+
"go-cell-001",
77+
"go-cell-003",
78+
"ms-cell-001",
79+
):
80+
raise BaseClientError(f"Invalid LumApps cell in base URL: {cell}")
81+
82+
if cell not in (
83+
"go-cell-001",
84+
"go-cell-002",
85+
"go-cell-003",
86+
"go-cell-005",
87+
"go-cell-600",
88+
"ms-cell-001",
89+
"ms-cell-002",
90+
):
91+
raise BaseClientError(f"Invalid LumApps cell in base URL: {cell}")
6992
return (
70-
"https://sites.lumapps.com/_ah/api/discovery/v1/apis/lumsites/v1/rest",
93+
"https://storage.googleapis.com/prod-frontend-static-assets/api-discovery/"
94+
f"lumapps-discovery-{'beta-' if is_beta_cell else ''}{cell}.json",
7195
api_url,
7296
)
7397
return (
@@ -117,7 +141,9 @@ def __init__(
117141
}
118142
self._extra_http_headers = extra_http_headers or {}
119143
self.api_info = api_info
120-
self._discovery_url, self._api_url = _get_urls(self.base_url, self.api_info)
144+
self._discovery_url, self._api_url = _get_discovery_urls(
145+
self.base_url, self.api_info
146+
)
121147
self.token_getter = token_getter
122148
self.token = token
123149
self.cursor = None

tests/legacy/test_base_client.py

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
from typing import Dict, Optional
12
from json import load, loads
23
from typing import Sequence
34
from unittest.mock import PropertyMock
4-
5+
from lumapps.api import __version__
56
from httpx import HTTPStatusError
6-
from pytest import fixture, raises
7+
from pytest import fixture, raises, mark
78

8-
from lumapps.api.base_client import BaseClient
9+
from lumapps.api.base_client import BaseClient, fetch_access_token
10+
from lumapps.api.client import LumAppsClient
911
from lumapps.api.errors import BadCallError, BaseClientError
1012
from lumapps.api.utils import (
1113
FILTERS,
@@ -378,3 +380,48 @@ def get(*args, **kwargs):
378380
def test_get_new_client_as_using_dwd(cli: BaseClient):
379381
with raises(NotImplementedError):
380382
cli.get_new_client_as_using_dwd("[email protected]")
383+
384+
@mark.parametrize("cell, api_info, expected_exception", [
385+
("go-cell-002", {"base_url": "https://go-cell-002.api.lumapps.com/"}, None),
386+
("ms-cell-001", {"base_url": "https://ms-cell-001.api.lumapps.com/"}, None),
387+
("go-cell-003", {"base_url": "https://go-cell-003.beta.api.lumapps.com/"}, None),
388+
("go-cell-004", {"base_url": "https://go-cell-004.api.lumapps.com/"}, BaseClientError),
389+
("go-cell-001", {"base_url": "http://localhost/sdk"}, BaseClientError),
390+
("go-cell-001", {}, BaseClientError),
391+
])
392+
def test_create_client(cell: str, api_info: Dict[str, str], expected_exception: Optional[Exception]) -> None:
393+
# Given / When
394+
if expected_exception:
395+
with raises(expected_exception):
396+
LumAppsClient(
397+
"123",
398+
None,
399+
api_info=api_info,
400+
auth_info={"client_id": "abc", "client_secret": "789"},
401+
)
402+
return
403+
else:
404+
client = LumAppsClient(
405+
"123",
406+
None,
407+
api_info=api_info,
408+
auth_info={"client_id": "abc", "client_secret": "789"},
409+
)
410+
411+
# Then
412+
assert client._headers == {
413+
"x-lumapps-analytics": "off",
414+
"User-Agent": f"lumapps-sdk {__version__}",
415+
"authorization": "Bearer None"
416+
}
417+
assert client.base_url == api_info["base_url"].rstrip("/")
418+
if ".beta." in client.base_url:
419+
assert client._discovery_url == (
420+
"https://storage.googleapis.com/prod-frontend-static-assets/api-discovery/"
421+
f"lumapps-discovery-beta-{cell}.json"
422+
)
423+
else:
424+
assert client._discovery_url == (
425+
"https://storage.googleapis.com/prod-frontend-static-assets/api-discovery/"
426+
f"lumapps-discovery-{cell}.json"
427+
)

0 commit comments

Comments
 (0)