-
Notifications
You must be signed in to change notification settings - Fork 40
feat: add Style Guides API support #251
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
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 @@ | ||
| __pdoc__ = {"tests": False} |
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,17 @@ | ||
| from enum import Enum | ||
|
|
||
|
|
||
| class ListStyleGuidesOrderBy(Enum): | ||
| ID = "id" | ||
| NAME = "name" | ||
| USER_ID = "userId" | ||
| CREATED_AT = "createdAt" | ||
|
|
||
|
|
||
| class StyleGuidePatchPath(Enum): | ||
| NAME = "/name" | ||
| AI_INSTRUCTIONS = "/aiInstructions" | ||
| LANGUAGE_IDS = "/languageIds" | ||
| PROJECT_IDS = "/projectIds" | ||
| IS_SHARED = "/isShared" | ||
| STORAGE_ID = "/storageId" |
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,101 @@ | ||
| from typing import Iterable, Optional | ||
|
|
||
| from crowdin_api.api_resources.abstract.resources import BaseResource | ||
| from crowdin_api.api_resources.style_guides.types import ( | ||
| AddStyleGuideRequest, | ||
| StyleGuidePatchRequest, | ||
| ) | ||
| from crowdin_api.sorting import Sorting | ||
|
|
||
|
|
||
| class StyleGuidesResource(BaseResource): | ||
| """ | ||
| Resource for Style Guides. | ||
|
|
||
| Link to documentation: | ||
| https://support.crowdin.com/developer/api/v2/#tag/Style-Guides | ||
| """ | ||
|
|
||
| def get_style_guides_path(self, style_guide_id: Optional[int] = None): | ||
| if style_guide_id is not None: | ||
| return f"style-guides/{style_guide_id}" | ||
| return "style-guides" | ||
|
|
||
| def list_style_guides( | ||
| self, | ||
| order_by: Optional[Sorting] = None, | ||
| user_id: Optional[int] = None, | ||
| limit: Optional[int] = None, | ||
| offset: Optional[int] = None, | ||
| ): | ||
| """ | ||
| List Style Guides | ||
|
|
||
| Link to documentation: | ||
| https://support.crowdin.com/developer/api/v2/#operation/api.style-guides.getMany | ||
| """ | ||
| params = { | ||
| "orderBy": order_by, | ||
| "userId": user_id, | ||
| } | ||
| params.update(self.get_page_params(limit=limit, offset=offset)) | ||
|
|
||
| return self._get_entire_data( | ||
| method="get", | ||
| path=self.get_style_guides_path(), | ||
| params=params, | ||
| ) | ||
|
|
||
| def add_style_guide(self, request_data: AddStyleGuideRequest): | ||
| """ | ||
| Create Style Guide | ||
|
|
||
| Link to documentation: | ||
| https://support.crowdin.com/developer/api/v2/#operation/api.style-guides.post | ||
| """ | ||
| return self.requester.request( | ||
| method="post", | ||
| path=self.get_style_guides_path(), | ||
| request_data=request_data, | ||
| ) | ||
|
|
||
| def get_style_guide(self, style_guide_id: int): | ||
| """ | ||
| Get Style Guide | ||
|
|
||
| Link to documentation: | ||
| https://support.crowdin.com/developer/api/v2/#operation/api.style-guides.get | ||
| """ | ||
| return self.requester.request( | ||
| method="get", | ||
| path=self.get_style_guides_path(style_guide_id), | ||
| ) | ||
|
|
||
| def delete_style_guide(self, style_guide_id: int): | ||
| """ | ||
| Delete Style Guide | ||
|
|
||
| Link to documentation: | ||
| https://support.crowdin.com/developer/api/v2/#operation/api.style-guides.delete | ||
| """ | ||
| return self.requester.request( | ||
| method="delete", | ||
| path=self.get_style_guides_path(style_guide_id), | ||
| ) | ||
|
|
||
| def edit_style_guide( | ||
| self, | ||
| style_guide_id: int, | ||
| request_data: Iterable[StyleGuidePatchRequest], | ||
| ): | ||
| """ | ||
| Edit Style Guide | ||
|
|
||
| Link to documentation: | ||
| https://support.crowdin.com/developer/api/v2/#operation/api.style-guides.patch | ||
| """ | ||
| return self.requester.request( | ||
| method="patch", | ||
| path=self.get_style_guides_path(style_guide_id), | ||
| request_data=request_data, | ||
| ) |
166 changes: 166 additions & 0 deletions
166
crowdin_api/api_resources/style_guides/tests/test_style_guides_resources.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,166 @@ | ||
| from unittest import mock | ||
|
|
||
| import pytest | ||
| from crowdin_api.api_resources.enums import PatchOperation | ||
| from crowdin_api.api_resources.style_guides.enums import ( | ||
| ListStyleGuidesOrderBy, | ||
| StyleGuidePatchPath, | ||
| ) | ||
| from crowdin_api.api_resources.style_guides.resource import StyleGuidesResource | ||
| from crowdin_api.api_resources.style_guides.types import AddStyleGuideRequest | ||
| from crowdin_api.requester import APIRequester | ||
| from crowdin_api.sorting import Sorting, SortingOrder, SortingRule | ||
|
|
||
|
|
||
| class TestStyleGuidesResource: | ||
| resource_class = StyleGuidesResource | ||
|
|
||
| def get_resource(self, base_absolut_url): | ||
| return self.resource_class(requester=APIRequester(base_url=base_absolut_url)) | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "in_params, path", | ||
| ( | ||
| ({}, "style-guides"), | ||
| ({"style_guide_id": 2}, "style-guides/2"), | ||
| ), | ||
| ) | ||
| def test_get_style_guides_path(self, in_params, path, base_absolut_url): | ||
| resource = self.get_resource(base_absolut_url) | ||
| assert resource.get_style_guides_path(**in_params) == path | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "incoming_data, request_params", | ||
| ( | ||
| ( | ||
| {}, | ||
| { | ||
| "orderBy": None, | ||
| "userId": None, | ||
| "limit": 25, | ||
| "offset": 0, | ||
| }, | ||
| ), | ||
| ( | ||
| { | ||
| "order_by": Sorting( | ||
| [SortingRule(ListStyleGuidesOrderBy.CREATED_AT, SortingOrder.DESC)] | ||
| ), | ||
| "user_id": 2, | ||
| "limit": 25, | ||
| "offset": 0, | ||
| }, | ||
| { | ||
| "orderBy": Sorting( | ||
| [SortingRule(ListStyleGuidesOrderBy.CREATED_AT, SortingOrder.DESC)] | ||
| ), | ||
| "userId": 2, | ||
| "limit": 25, | ||
| "offset": 0, | ||
| }, | ||
| ), | ||
| ), | ||
| ) | ||
| @mock.patch("crowdin_api.requester.APIRequester.request") | ||
| def test_list_style_guides(self, m_request, incoming_data, request_params, base_absolut_url): | ||
| m_request.return_value = "response" | ||
|
|
||
| resource = self.get_resource(base_absolut_url) | ||
| assert resource.list_style_guides(**incoming_data) == "response" | ||
|
|
||
| m_request.assert_called_once_with( | ||
| method="get", | ||
| path="style-guides", | ||
| params=request_params, | ||
| ) | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "incoming_data, request_data", | ||
| ( | ||
| ( | ||
| AddStyleGuideRequest( | ||
| name="Be My Eyes iOS's Style Guide", | ||
| storageId=1, | ||
| ), | ||
| { | ||
| "name": "Be My Eyes iOS's Style Guide", | ||
| "storageId": 1, | ||
| }, | ||
| ), | ||
| ( | ||
| AddStyleGuideRequest( | ||
| name="Be My Eyes iOS's Style Guide", | ||
| storageId=1, | ||
| aiInstructions="Rules to be used by AI models", | ||
| languageIds=["uk", "fr", "de"], | ||
| projectIds=[1, 2, 3], | ||
| isShared=False, | ||
| ), | ||
| { | ||
| "name": "Be My Eyes iOS's Style Guide", | ||
| "storageId": 1, | ||
| "aiInstructions": "Rules to be used by AI models", | ||
| "languageIds": ["uk", "fr", "de"], | ||
| "projectIds": [1, 2, 3], | ||
| "isShared": False, | ||
| }, | ||
| ), | ||
| ), | ||
| ) | ||
| @mock.patch("crowdin_api.requester.APIRequester.request") | ||
| def test_add_style_guide(self, m_request, incoming_data, request_data, base_absolut_url): | ||
| m_request.return_value = "response" | ||
|
|
||
| resource = self.get_resource(base_absolut_url) | ||
| assert resource.add_style_guide(incoming_data) == "response" | ||
|
|
||
| m_request.assert_called_once_with( | ||
| method="post", | ||
| path="style-guides", | ||
| request_data=request_data, | ||
| ) | ||
|
|
||
| @mock.patch("crowdin_api.requester.APIRequester.request") | ||
| def test_get_style_guide(self, m_request, base_absolut_url): | ||
| m_request.return_value = "response" | ||
|
|
||
| resource = self.get_resource(base_absolut_url) | ||
| assert resource.get_style_guide(style_guide_id=2) == "response" | ||
|
|
||
| m_request.assert_called_once_with( | ||
| method="get", | ||
| path="style-guides/2", | ||
| ) | ||
|
|
||
| @mock.patch("crowdin_api.requester.APIRequester.request") | ||
| def test_delete_style_guide(self, m_request, base_absolut_url): | ||
| m_request.return_value = "response" | ||
|
|
||
| resource = self.get_resource(base_absolut_url) | ||
| assert resource.delete_style_guide(style_guide_id=2) == "response" | ||
|
|
||
| m_request.assert_called_once_with( | ||
| method="delete", | ||
| path="style-guides/2", | ||
| ) | ||
|
|
||
| @mock.patch("crowdin_api.requester.APIRequester.request") | ||
| def test_edit_style_guide(self, m_request, base_absolut_url): | ||
| m_request.return_value = "response" | ||
|
|
||
| request_data = [ | ||
| { | ||
| "op": PatchOperation.REPLACE, | ||
| "path": StyleGuidePatchPath.NAME, | ||
| "value": "Be My Eyes iOS's Style Guide", | ||
| } | ||
| ] | ||
|
|
||
| resource = self.get_resource(base_absolut_url) | ||
| assert resource.edit_style_guide(style_guide_id=2, request_data=request_data) == "response" | ||
|
|
||
| m_request.assert_called_once_with( | ||
| method="patch", | ||
| path="style-guides/2", | ||
| request_data=request_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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| from typing import Any, Iterable, Optional | ||
|
|
||
| from crowdin_api.api_resources.enums import PatchOperation | ||
| from crowdin_api.api_resources.style_guides.enums import StyleGuidePatchPath | ||
| from crowdin_api.typing import TypedDict | ||
|
|
||
|
|
||
| class AddStyleGuideRequest(TypedDict): | ||
| name: str | ||
| storageId: int | ||
| aiInstructions: Optional[str] | ||
| languageIds: Optional[Iterable[str]] | ||
| projectIds: Optional[Iterable[int]] | ||
| isShared: Optional[bool] | ||
|
|
||
|
|
||
| class StyleGuidePatchRequest(TypedDict): | ||
| op: PatchOperation | ||
| path: StyleGuidePatchPath | ||
| value: Any |
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.