-
Notifications
You must be signed in to change notification settings - Fork 0
MPT-12845 Add Commerce subscriptions #25
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,87 @@ | ||
| from mpt_api_client.http import ( | ||
| AsyncCreateMixin, | ||
| AsyncDeleteMixin, | ||
| AsyncService, | ||
| CreateMixin, | ||
| DeleteMixin, | ||
| Service, | ||
| ) | ||
| from mpt_api_client.models import Model, ResourceData | ||
|
|
||
|
|
||
| class Subscription(Model): | ||
| """Subscription resource.""" | ||
|
|
||
|
|
||
| class SubscriptionsServiceConfig: | ||
| """Subscription service config.""" | ||
|
|
||
| _endpoint = "/public/v1/commerce/subscriptions" | ||
| _model_class = Subscription | ||
| _collection_key = "data" | ||
|
|
||
|
|
||
| class SubscriptionsService( # noqa: WPS215 | ||
| CreateMixin[Subscription], | ||
| DeleteMixin, | ||
| Service[Subscription], | ||
| SubscriptionsServiceConfig, | ||
| ): | ||
| """Subscription service.""" | ||
|
|
||
| def render(self, resource_id: str) -> str: | ||
| """Render subscription template. | ||
|
|
||
| Args: | ||
| resource_id: Subscription resource ID | ||
|
|
||
| Returns: | ||
| Order template text in markdown format. | ||
| """ | ||
| response = self._resource_do_request(resource_id, "GET", "render") | ||
| return response.text | ||
|
|
||
| def terminate(self, resource_id: str, resource_data: ResourceData) -> Subscription: | ||
| """Terminate subscription. | ||
|
|
||
| Args: | ||
| resource_id: Order resource ID | ||
| resource_data: Order resource data | ||
|
|
||
| Returns: | ||
| Subscription template text in markdown format. | ||
| """ | ||
| return self._resource_action(resource_id, "POST", "terminate", json=resource_data) | ||
|
|
||
|
|
||
| class AsyncSubscriptionsService( # noqa: WPS215 | ||
| AsyncCreateMixin[Subscription], | ||
| AsyncDeleteMixin, | ||
| AsyncService[Subscription], | ||
| SubscriptionsServiceConfig, | ||
| ): | ||
| """Async Subscription service.""" | ||
|
|
||
| async def render(self, resource_id: str) -> str: | ||
| """Render subscription template. | ||
|
|
||
| Args: | ||
| resource_id: Subscription resource ID | ||
|
|
||
| Returns: | ||
| Order template text in markdown format. | ||
| """ | ||
| response = await self._resource_do_request(resource_id, "GET", "render") | ||
| return response.text | ||
|
|
||
| async def terminate(self, resource_id: str, resource_data: ResourceData) -> Subscription: | ||
| """Terminate subscription. | ||
|
|
||
| Args: | ||
| resource_id: Order resource ID | ||
| resource_data: Order resource data | ||
|
|
||
| Returns: | ||
| Subscription template text in markdown format. | ||
| """ | ||
| return await self._resource_action(resource_id, "POST", "terminate", json=resource_data) |
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,88 @@ | ||
| import httpx | ||
| import pytest | ||
| import respx | ||
|
|
||
| from mpt_api_client.resources.commerce.subscriptions import ( | ||
| AsyncSubscriptionsService, | ||
| SubscriptionsService, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def subscriptions_service(http_client): | ||
| return SubscriptionsService(http_client=http_client) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def async_subscriptions_service(async_http_client): | ||
| return AsyncSubscriptionsService(http_client=async_http_client) | ||
|
|
||
|
|
||
| async def test_async_terminate(async_subscriptions_service): | ||
| subscription_expected = {"id": "SUB-123", "status": "Terminated", "name": "Terminated SUB-123"} | ||
| with respx.mock: | ||
| respx.post( | ||
| "https://api.example.com/public/v1/commerce/subscriptions/SUB-123/terminate" | ||
| ).mock( | ||
| return_value=httpx.Response( | ||
| status_code=httpx.codes.OK, | ||
| json=subscription_expected, | ||
| ) | ||
| ) | ||
|
|
||
| subscription_updated = await async_subscriptions_service.terminate( | ||
| "SUB-123", {"name": "Terminated SUB-123"} | ||
| ) | ||
|
|
||
| assert subscription_updated.to_dict() == subscription_expected | ||
|
|
||
|
|
||
| async def test_async_render(async_subscriptions_service): | ||
| template_content = "# Subscription Template\n\nThis is a markdown template." | ||
| with respx.mock: | ||
| respx.get("https://api.example.com/public/v1/commerce/subscriptions/SUB-123/render").mock( | ||
| return_value=httpx.Response( | ||
| status_code=httpx.codes.OK, | ||
| headers={"content-type": "text/markdown"}, | ||
| content=template_content, | ||
| ) | ||
| ) | ||
|
|
||
| template = await async_subscriptions_service.render("SUB-123") | ||
|
|
||
| assert template == template_content | ||
|
|
||
|
|
||
| def test_terminate(subscriptions_service): | ||
| subscription_expected = {"id": "SUB-123", "status": "Terminated", "name": "Terminated SUB-123"} | ||
| with respx.mock: | ||
| respx.post( | ||
| "https://api.example.com/public/v1/commerce/subscriptions/SUB-123/terminate" | ||
| ).mock( | ||
| return_value=httpx.Response( | ||
| status_code=httpx.codes.OK, | ||
| json=subscription_expected, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment about the comma here
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ruff seems to add it if the line does not fit inside a single line in this case |
||
| ) | ||
| ) | ||
|
|
||
| subscription_updated = subscriptions_service.terminate( | ||
| "SUB-123", {"name": "Terminated SUB-123"} | ||
| ) | ||
|
|
||
| assert subscription_updated.to_dict() == subscription_expected | ||
|
|
||
|
|
||
| def test_render(subscriptions_service): | ||
| template_content = "# Subscription Template\n\nThis is a markdown template." | ||
| with respx.mock: | ||
| respx.get("https://api.example.com/public/v1/commerce/subscriptions/SUB-123/render").mock( | ||
| return_value=httpx.Response( | ||
| status_code=httpx.codes.OK, | ||
| headers={"content-type": "text/markdown"}, | ||
| content=template_content, | ||
| ) | ||
| ) | ||
|
|
||
| template = subscriptions_service.render("SUB-123") | ||
|
|
||
| assert template == template_content | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove the comma to avoid splitting the line unnecessarily
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ruff seems to add it if the line does not fit inside a single line in this case