-
Notifications
You must be signed in to change notification settings - Fork 0
MPT-19908: add /integration/installations service #281
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
Merged
Merged
Changes from all commits
Commits
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,69 @@ | ||
| from mpt_api_client.http import AsyncService, Service | ||
| from mpt_api_client.http.mixins import ( | ||
| AsyncCollectionMixin, | ||
| AsyncManagedResourceMixin, | ||
| CollectionMixin, | ||
| ManagedResourceMixin, | ||
| ) | ||
| from mpt_api_client.models import Model | ||
| from mpt_api_client.models.model import BaseModel | ||
| from mpt_api_client.resources.integration.mixins import ( | ||
| AsyncInstallationMixin, | ||
| InstallationMixin, | ||
| ) | ||
|
|
||
|
|
||
| class Installation(Model): | ||
| """Installation resource. | ||
|
|
||
| Attributes: | ||
| name: Installation name. | ||
| revision: Revision number. | ||
| account: Reference to the account. | ||
| extension: Reference to the extension. | ||
| status: Installation status (Invited, Installed, Uninstalled, Expired). | ||
| configuration: Installation configuration data. | ||
| invitation: Invitation details. | ||
| modules: Modules included in the installation. | ||
| terms: Accepted terms for this installation. | ||
| audit: Audit information (created, updated, invited, installed, expired, uninstalled). | ||
| """ | ||
|
|
||
| name: str | None | ||
| revision: int | None | ||
| account: BaseModel | None | ||
| extension: BaseModel | None | ||
| status: str | None | ||
| configuration: BaseModel | None | ||
| invitation: BaseModel | None | ||
| modules: list[BaseModel] | None | ||
| terms: list[BaseModel] | None | ||
| audit: BaseModel | None | ||
|
|
||
|
|
||
| class InstallationsServiceConfig: | ||
| """Installations service configuration.""" | ||
|
|
||
| _endpoint = "/public/v1/integration/installations" | ||
| _model_class = Installation | ||
| _collection_key = "data" | ||
|
|
||
|
|
||
| class InstallationsService( | ||
| InstallationMixin[Installation], | ||
| ManagedResourceMixin[Installation], | ||
| CollectionMixin[Installation], | ||
| Service[Installation], | ||
| InstallationsServiceConfig, | ||
| ): | ||
| """Sync service for the /public/v1/integration/installations endpoint.""" | ||
|
|
||
|
|
||
| class AsyncInstallationsService( | ||
| AsyncInstallationMixin[Installation], | ||
| AsyncManagedResourceMixin[Installation], | ||
| AsyncCollectionMixin[Installation], | ||
| AsyncService[Installation], | ||
| InstallationsServiceConfig, | ||
| ): | ||
| """Async service for the /public/v1/integration/installations endpoint.""" |
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
33 changes: 33 additions & 0 deletions
33
mpt_api_client/resources/integration/mixins/installation_mixin.py
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,33 @@ | ||
| from mpt_api_client.models import ResourceData | ||
|
|
||
|
|
||
| class InstallationMixin[Model]: | ||
| """Mixin that adds the installation redeem action.""" | ||
|
|
||
| def redeem(self, resource_id: str, resource_data: ResourceData | None = None) -> Model: | ||
| """Redeem an installation invitation. | ||
|
|
||
| Args: | ||
| resource_id: Installation ID. | ||
| resource_data: Redeem payload, for example ``{"code": "...", "modules": [...]}``. | ||
|
|
||
| Returns: | ||
| Updated installation. | ||
| """ | ||
| return self._resource(resource_id).post("redeem", json=resource_data) # type: ignore[attr-defined, no-any-return] | ||
|
|
||
|
|
||
| class AsyncInstallationMixin[Model]: | ||
| """Async mixin that adds the installation redeem action.""" | ||
|
|
||
| async def redeem(self, resource_id: str, resource_data: ResourceData | None = None) -> Model: | ||
| """Redeem an installation invitation. | ||
|
|
||
| Args: | ||
| resource_id: Installation ID. | ||
| resource_data: Redeem payload, for example ``{"code": "...", "modules": [...]}``. | ||
|
|
||
| Returns: | ||
| Updated installation. | ||
| """ | ||
| return await self._resource(resource_id).post("redeem", json=resource_data) # type: ignore[attr-defined, no-any-return] |
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,6 @@ | ||
| import pytest | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def extension_id(e2e_config): | ||
| return e2e_config["integration.extension.id"] |
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
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
Empty file.
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,93 @@ | ||
| import pytest | ||
|
|
||
| from mpt_api_client.exceptions import MPTAPIError | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def installations_service(mpt_vendor): | ||
| return mpt_vendor.integration.installations | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def async_installations_service(async_mpt_vendor): | ||
| return async_mpt_vendor.integration.installations | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def installation_modules(): | ||
| return [ | ||
| {"id": "MOD-0478"}, | ||
| {"id": "MOD-1239"}, | ||
| {"id": "MOD-1756"}, | ||
| {"id": "MOD-4525"}, | ||
| {"id": "MOD-8352"}, | ||
| {"id": "MOD-8743"}, | ||
| {"id": "MOD-9042"}, | ||
| ] | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def installation_data(extension_id, installation_modules): | ||
| return { | ||
| "extension": {"id": extension_id}, | ||
| "modules": installation_modules, | ||
| } | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def invite_data(extension_id): | ||
| return { | ||
| "extension": {"id": extension_id}, | ||
| "invitation": { | ||
| "message": "E2E testing - Delete", | ||
| "validity": "7d", | ||
| }, | ||
| } | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def created_installation(installations_service, installation_data): | ||
| installation = installations_service.create(installation_data) | ||
|
|
||
| yield installation | ||
|
|
||
| try: | ||
| installations_service.delete(installation.id) | ||
| except MPTAPIError as error: | ||
| print(f"TEARDOWN - Unable to delete installation {installation.id}: {error.title}") # noqa: WPS421 | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def created_installation_invite(installations_service, invite_data): | ||
| invite = installations_service.create(invite_data) | ||
|
|
||
| yield invite | ||
|
|
||
| try: | ||
| installations_service.delete(invite.id) | ||
| except MPTAPIError as error: | ||
| print(f"TEARDOWN - Unable to delete installation {invite.id}: {error.title}") # noqa: WPS421 | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| async def async_created_installation(async_installations_service, installation_data): | ||
| installation = await async_installations_service.create(installation_data) | ||
|
|
||
| yield installation | ||
|
|
||
| try: | ||
| await async_installations_service.delete(installation.id) | ||
| except MPTAPIError as error: | ||
| print(f"TEARDOWN - Unable to delete installation {installation.id}: {error.title}") # noqa: WPS421 | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| async def async_created_installation_invite(async_installations_service, invite_data): | ||
| invite = await async_installations_service.create(invite_data) | ||
|
|
||
| yield invite | ||
|
|
||
| try: | ||
| await async_installations_service.delete(invite.id) | ||
| except MPTAPIError as error: | ||
| print(f"TEARDOWN - Unable to delete installation {invite.id}: {error.title}") # noqa: WPS421 |
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,30 @@ | ||
| import base64 | ||
| import json | ||
|
|
||
|
|
||
| def _decode_base64_json(candidate: str) -> dict[str, str] | None: | ||
| normalized_candidate = candidate.strip() | ||
| if not normalized_candidate: | ||
| return None | ||
|
|
||
| padding = "=" * (-len(normalized_candidate) % 4) | ||
|
|
||
| try: # noqa: WPS229 | ||
| decoded_bytes = base64.urlsafe_b64decode(normalized_candidate + padding) | ||
| decoded_payload = json.loads(decoded_bytes.decode("utf-8")) | ||
| except (ValueError, json.JSONDecodeError): | ||
| return None | ||
|
|
||
| if isinstance(decoded_payload, dict) and decoded_payload.get("code"): | ||
| return decoded_payload | ||
|
|
||
| return None | ||
|
|
||
|
|
||
| def decode_invitation_payload(invitation_url: str) -> dict[str, str]: | ||
| """Decode an invitation payload from the API invitation URL field.""" | ||
| decoded_payload = _decode_base64_json(invitation_url) | ||
| if decoded_payload: | ||
| return decoded_payload | ||
|
|
||
| raise AssertionError(f"Unable to decode invitation payload from URL: {invitation_url}") |
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.