-
Notifications
You must be signed in to change notification settings - Fork 6
[CDF-27549] Add AppsAPI client for the App Hosting API #2986
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Magssch
wants to merge
21
commits into
main
Choose a base branch
from
cdf-27549-custom-apps-part2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+277
−17
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
0cc6380
Add custom app data model, ACLs, and YAML schema
Magssch 58492c2
external_id->app_external_id to match version api
Magssch 5740a99
Refactoring
Magssch 4df33dc
Fix CI
Magssch 62a5192
Remove as_update
Magssch 28d0db9
Add AppsAPI client for the App Hosting API
Magssch 2cb5b9c
Replace transition_lifecycle and set_alias with combined update_version
Magssch 67d77d0
Fix lint
Magssch d32d0b3
Merge branch 'main' into cdf-27549-custom-apps-part1
Magssch 7417ca9
Merge branch 'cdf-27549-custom-apps-part1' into cdf-27549-custom-apps…
Magssch 1d1aaab
Cleanup
Magssch ae88737
Remove unused AppsAPI methods: list_app_versions, retrieve, delete
Magssch 91a8346
Add AppsAPI unit tests
Magssch 1735ad8
Fix lint
Magssch 807d97d
address review comment
Magssch 7d2e955
Merge branch 'main' into cdf-27549-custom-apps-part1
Magssch a28ab56
Merge branch 'cdf-27549-custom-apps-part1' into cdf-27549-custom-apps…
Magssch a3e89c3
Merge branch 'main' into cdf-27549-custom-apps-part2
Magssch 00da852
Merge branch 'main' into cdf-27549-custom-apps-part2
Magssch 133dcc3
WIP
Magssch 1468f62
Merge branch 'main' into cdf-27549-custom-apps-part2
Magssch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| """AppsAPI: Custom apps deployed via the CDF App Hosting API.""" | ||
|
|
||
| import json | ||
| from collections.abc import Iterable, Sequence | ||
| from pathlib import Path | ||
|
|
||
| from cognite_toolkit._cdf_tk.client.http_client import HTTPClient, RequestMessage | ||
| from cognite_toolkit._cdf_tk.client.http_client._data_classes import FailedResponse, SuccessResponse | ||
| from cognite_toolkit._cdf_tk.client.http_client._exception import ToolkitAPIError | ||
| from cognite_toolkit._cdf_tk.client.identifiers import AppVersionId | ||
| from cognite_toolkit._cdf_tk.client.resource_classes.app import AppRequest, AppResponse | ||
|
|
||
|
|
||
| class AppsAPI: | ||
| """Client for the CDF App Hosting API (POST /apphosting/...).""" | ||
|
|
||
| def __init__(self, http_client: HTTPClient) -> None: | ||
| self._http_client = http_client | ||
|
|
||
| def _url(self, path: str) -> str: | ||
| return self._http_client.config.create_api_url(path) | ||
|
|
||
| def ensure_app(self, item: AppRequest) -> None: | ||
| """POST /apphosting/apps — create the app if it does not exist; 409 = already exists (idempotent).""" | ||
| request = RequestMessage( | ||
| endpoint_url=self._url("/apphosting/apps"), | ||
| method="POST", | ||
| body_content={"items": [item.dump()]}, | ||
| ) | ||
| result = self._http_client.request_single_retries(request) | ||
| if isinstance(result, SuccessResponse) or (isinstance(result, FailedResponse) and result.status_code == 409): | ||
| return | ||
| result.get_success_or_raise(request) | ||
|
|
||
| def upload_version( | ||
| self, | ||
| external_id: str, | ||
| version: str, | ||
| entrypoint: str, | ||
| zip_path: Path, | ||
| ) -> None: | ||
| """POST /apphosting/apps/{externalId}/versions — multipart upload of the zipped app.""" | ||
| result = self._http_client.request_raw_retries( | ||
| method="POST", | ||
| url=self._url(f"/apphosting/apps/{external_id}/versions"), | ||
| files={"file": ("app.zip", zip_path, "application/zip")}, | ||
| data={"version": version, "entryPath": entrypoint}, | ||
| add_auth=True, | ||
| ) | ||
| # 409 means this exact version already exists — treat as success (idempotent). | ||
| if isinstance(result, SuccessResponse) or (isinstance(result, FailedResponse) and result.status_code == 409): | ||
| return | ||
| raise ToolkitAPIError(message=result.body, code=result.status_code) | ||
|
|
||
| def update_version(self, external_id: str, version: str, update: dict) -> None: | ||
|
Magssch marked this conversation as resolved.
|
||
| """POST /apphosting/apps/{externalId}/versions/update — apply one or more field updates to a version.""" | ||
| request = RequestMessage( | ||
| endpoint_url=self._url(f"/apphosting/apps/{external_id}/versions/update"), | ||
| method="POST", | ||
| body_content={"items": [{"version": version, "update": update}]}, | ||
| ) | ||
| self._http_client.request_single_retries(request).get_success_or_raise(request) | ||
|
|
||
| def retrieve_version(self, external_id: str, version: str, ignore_unknown_ids: bool = False) -> AppResponse | None: | ||
| """Retrieve version metadata + app-level name/description in two calls.""" | ||
| version_request = RequestMessage( | ||
| endpoint_url=self._url(f"/apphosting/apps/{external_id}/versions/{version}"), | ||
| method="GET", | ||
| ) | ||
| version_result = self._http_client.request_single_retries(version_request) | ||
| if not isinstance(version_result, SuccessResponse): | ||
| if ( | ||
| isinstance(version_result, FailedResponse) | ||
| and version_result.status_code in (400, 404) | ||
| and ignore_unknown_ids | ||
| ): | ||
| return None | ||
| version_result.get_success_or_raise(version_request) | ||
| return None | ||
|
|
||
| version_data = json.loads(version_result.body) | ||
|
|
||
| app_request = RequestMessage( | ||
| endpoint_url=self._url(f"/apphosting/apps/{external_id}"), | ||
| method="GET", | ||
| ) | ||
| app_result = self._http_client.request_single_retries(app_request) | ||
| app_data = json.loads(app_result.body) if isinstance(app_result, SuccessResponse) else {} | ||
|
|
||
| return AppResponse( | ||
| external_id=version_data.get("appExternalId", external_id), | ||
| version=version_data.get("version", version), | ||
| name=app_data.get("name", ""), | ||
| description=app_data.get("description"), | ||
| lifecycle_state=version_data.get("lifecycleState", "DRAFT"), | ||
| alias=version_data.get("alias"), | ||
| entrypoint=version_data.get("entrypoint", "index.html"), | ||
| ) | ||
|
|
||
| def iterate(self, limit: int | None = 100) -> Iterable[list[AppResponse]]: | ||
| """POST /apphosting/versions/list — paginated list of all versions across all apps.""" | ||
| cursor: str | None = None | ||
| page_limit = min(limit, 1000) if limit is not None else 1000 | ||
| fetched = 0 | ||
| while True: | ||
| body: dict = {"limit": page_limit} | ||
| if cursor: | ||
| body["cursor"] = cursor | ||
| request = RequestMessage( | ||
| endpoint_url=self._url("/apphosting/versions/list"), | ||
| method="POST", | ||
| body_content=body, | ||
| ) | ||
| result = self._http_client.request_single_retries(request) | ||
| if not isinstance(result, SuccessResponse): | ||
| result.get_success_or_raise(request) | ||
| break | ||
|
|
||
| data = json.loads(result.body) | ||
| page_items = [ | ||
| AppResponse( | ||
| external_id=item["appExternalId"], | ||
| version=item["version"], | ||
| name="", | ||
| description=None, | ||
| lifecycle_state=item.get("lifecycleState", "DRAFT"), | ||
| alias=item.get("alias"), | ||
| entrypoint=item.get("entrypoint", "index.html"), | ||
| ) | ||
| for item in data.get("items", []) | ||
| ] | ||
| if page_items: | ||
| yield page_items | ||
| fetched += len(page_items) | ||
|
|
||
| cursor = data.get("nextCursor") | ||
| if not cursor or (limit is not None and fetched >= limit): | ||
| break | ||
|
|
||
| def delete_version(self, external_id: str, versions: Sequence[AppVersionId]) -> None: | ||
| """POST /apphosting/apps/{externalId}/versions/delete — delete specific versions of an app.""" | ||
| if not versions: | ||
| return | ||
| request = RequestMessage( | ||
| endpoint_url=self._url(f"/apphosting/apps/{external_id}/versions/delete"), | ||
| method="POST", | ||
| body_content={"items": [{"version": v.version} for v in versions]}, | ||
| ) | ||
| self._http_client.request_single_retries(request).get_success_or_raise(request) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.