Skip to content
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

feat(api): Add the orderBy parameter to list methods #191

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,30 @@ print(client.projects.with_fetch_all().list_projects())
print(client.projects.with_fetch_all(1000).list_projects())
```

### Sorting

An optional `orderBy` parameter is used to apply sorting.

```python
from crowdin_api import CrowdinClient
from crowdin_api.sorting import Sorting, SortingOrder, SortingRule
from crowdin_api.api_resources.projects.enums import ListProjectsOrderBy

client = CrowdinClient(token='__token__')

sorting = Sorting(
[
SortingRule(ListProjectsOrderBy.ID, SortingOrder.ASC),
SortingRule(ListProjectsOrderBy.NAME),
SortingRule(ListProjectsOrderBy.CREATED_AT, SortingOrder.DESC),
]
)

print(client.projects.list_projects(orderBy=sorting))
```

Enum `SortingOrder` is also optional (ascending order applied by default).

### Extended request parameters

The `EXTENDED_REQUEST_PARAMS` parameter allows you to set additional parameters for requests. For example, you can configure proxies or certificates.
Expand Down
38 changes: 38 additions & 0 deletions crowdin_api/api_resources/glossaries/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,41 @@ class TermGender(Enum):
FEMININE = "feminine"
NEUTER = "neuter"
OTHER = "other"


class ListConceptsOrderBy(Enum):
ID = "id"
SUBJECT = "subject"
DEFINITION = "definition"
NOTE = "note"
CREATED_AT = "createdAt"
UPDATED_AT = "updatedAt"


class ListGlossariesCrowdinOrderBy(Enum):
ID = "id"
NAME = "name"
USER_ID = "userId"
CREATED_AT = "createdAt"


class ListGlossariesEnterpriseOrderBy(Enum):
ID = "id"
NAME = "name"
GROUP_ID = "groupId"
USER_ID = "userId"
CREATED_AT = "createdAt"


class ListTermsOrderBy(Enum):
ID = "id"
TEXT = "text"
DESCRIPTION = "description"
PART_OF_SPEECH = "partOfSpeech"
STATUS = "status"
TYPE = "type"
GENDER = "gender"
NOTE = "note"
LEMMA = "lemma"
CREATED_AT = "createdAt"
UPDATED_AT = "updatedAt"
16 changes: 12 additions & 4 deletions crowdin_api/api_resources/glossaries/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
LanguagesDetails,
GlossarySchemaRequest,
)
from crowdin_api.sorting import Sorting


class GlossariesResource(BaseResource):
Expand All @@ -38,6 +39,7 @@ def get_glossaries_path(self, glossaryId: Optional[int] = None):

def list_glossaries(
self,
orderBy: Optional[Sorting] = None,
groupId: Optional[int] = None,
page: Optional[int] = None,
offset: Optional[int] = None,
Expand All @@ -50,7 +52,7 @@ def list_glossaries(
https://developer.crowdin.com/api/v2/#operation/api.glossaries.getMany
"""

params = {"groupId": groupId}
params = {"orderBy": orderBy, "groupId": groupId}
params.update(self.get_page_params(page=page, offset=offset, limit=limit))

return self._get_entire_data(
Expand Down Expand Up @@ -239,6 +241,7 @@ def get_terms_path(self, glossaryId: int, termId: Optional[int] = None):
def list_terms(
self,
glossaryId: int,
orderBy: Optional[Sorting] = None,
userId: Optional[int] = None,
languageId: Optional[str] = None,
conceptId: Optional[int] = None,
Expand All @@ -254,6 +257,7 @@ def list_terms(
"""

params = {
"orderBy": orderBy,
"userId": userId,
"languageId": languageId,
"conceptId": conceptId,
Expand Down Expand Up @@ -302,7 +306,7 @@ def add_term(
"gender": gender,
"note": note,
"url": url,
"conceptId": conceptId
"conceptId": conceptId,
},
)

Expand Down Expand Up @@ -377,8 +381,9 @@ def get_concepts_path(self, glossaryId: int, conceptId: Optional[int] = None):
def list_concepts(
self,
glossaryId: int,
orderBy: Optional[Sorting] = None,
offset: Optional[int] = None,
limit: Optional[int] = None
limit: Optional[int] = None,
):
"""
List Concepts.
Expand All @@ -387,10 +392,13 @@ def list_concepts(
https://developer.crowdin.com/api/v2/#operation/api.glossaries.concepts.getMany
"""

params = {"orderBy": orderBy}
params.update(self.get_page_params(offset=offset, limit=limit))

return self._get_entire_data(
method="get",
path=self.get_concepts_path(glossaryId=glossaryId),
params=self.get_page_params(offset=offset, limit=limit),
params=params,
)

def get_concept(self, glossaryId: int, conceptId: int):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@
from crowdin_api.api_resources.enums import PatchOperation
from crowdin_api.api_resources.glossaries.enums import (
GlossaryPatchPath,
ListConceptsOrderBy,
ListGlossariesEnterpriseOrderBy,
ListTermsOrderBy,
TermPartOfSpeech,
TermPatchPath,
TermStatus,
TermType,
TermGender,
GlossaryFormat,
GlossaryExportFields,
ListGlossariesCrowdinOrderBy,
)
from crowdin_api.api_resources.glossaries.resource import GlossariesResource
from crowdin_api.requester import APIRequester
from crowdin_api.sorting import Sorting, SortingOrder, SortingRule


class TestGlossariesResource:
Expand Down Expand Up @@ -47,21 +52,60 @@ def test_get_screenshots_path(self, in_params, path, base_absolut_url):
(
{},
{
"orderBy": None,
"groupId": None,
"offset": 0,
"limit": 25,
},
),
(
{
"orderBy": Sorting(
[
SortingRule(
ListGlossariesCrowdinOrderBy.NAME, SortingOrder.DESC
)
]
),
"groupId": 1,
},
{
"orderBy": Sorting(
[
SortingRule(
ListGlossariesCrowdinOrderBy.NAME, SortingOrder.DESC
)
]
),
"groupId": 1,
"offset": 0,
"limit": 25,
},
),
(
{
"orderBy": Sorting(
[
SortingRule(
ListGlossariesEnterpriseOrderBy.NAME, SortingOrder.DESC
)
]
),
"groupId": 1,
},
{
"orderBy": Sorting(
[
SortingRule(
ListGlossariesEnterpriseOrderBy.NAME, SortingOrder.DESC
)
]
),
"groupId": 1,
"offset": 0,
"limit": 25,
},
)
),
)
@mock.patch("crowdin_api.requester.APIRequester.request")
Expand All @@ -72,8 +116,8 @@ def test_list_glossaries(self, m_request, incoming_data, request_params, base_ab
assert resource.list_glossaries(**incoming_data) == "response"
m_request.assert_called_once_with(
method="get",
params=request_params,
path=resource.get_glossaries_path(),
params=request_params,
)

@mock.patch("crowdin_api.requester.APIRequester.request")
Expand Down Expand Up @@ -269,6 +313,7 @@ def test_get_terms_path(self, in_params, path, base_absolut_url):
(
{},
{
"orderBy": None,
"userId": None,
"languageId": None,
"conceptId": None,
Expand All @@ -279,12 +324,18 @@ def test_get_terms_path(self, in_params, path, base_absolut_url):
),
(
{
"orderBy": Sorting(
[SortingRule(ListTermsOrderBy.ID, SortingOrder.DESC)]
),
"userId": 1,
"languageId": "ua",
"conceptId": 2,
"croql": "status = 'preferred'",
},
{
"orderBy": Sorting(
[SortingRule(ListTermsOrderBy.ID, SortingOrder.DESC)]
),
"userId": 1,
"languageId": "ua",
"conceptId": 2,
Expand Down Expand Up @@ -447,10 +498,27 @@ def test_get_concepts_path(self, in_params, path, base_absolut_url):
(
{},
{
"orderBy": None,
"offset": 0,
"limit": 25,
},
),
(
{
"orderBy": Sorting(
[SortingRule(ListConceptsOrderBy.ID, SortingOrder.DESC)]
),
"offset": 10,
"limit": 50,
},
{
"orderBy": Sorting(
[SortingRule(ListConceptsOrderBy.ID, SortingOrder.DESC)]
),
"offset": 10,
"limit": 50,
},
),
),
)
@mock.patch("crowdin_api.requester.APIRequester.request")
Expand Down
8 changes: 8 additions & 0 deletions crowdin_api/api_resources/groups/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,11 @@ class GroupPatchPath(Enum):
NAME = "/name"
DESCRIPTION = "/description"
PARENT_ID = "/parentId"


class ListGroupsOrderBy(Enum):
ID = "id"
NAME = "name"
DESCRIPTION = "description"
CREATED_AT = "createdAt"
UPDATED_AT = "updatedAt"
4 changes: 3 additions & 1 deletion crowdin_api/api_resources/groups/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from crowdin_api.api_resources.abstract.resources import BaseResource
from crowdin_api.api_resources.groups.types import GroupPatchRequest
from crowdin_api.sorting import Sorting


class GroupsResource(BaseResource):
Expand Down Expand Up @@ -63,6 +64,7 @@ def add_group(

def list_groups(
self,
orderBy: Optional[Sorting] = None,
parentId: Optional[int] = None,
limit: Optional[int] = None,
offset: Optional[int] = None
Expand All @@ -74,7 +76,7 @@ def list_groups(
https://developer.crowdin.com/enterprise/api/v2/#operation/api.groups.getMany
"""

params = {"parentId": parentId}
params = {"orderBy": orderBy, "parentId": parentId}
params.update(self.get_page_params(offset=offset, limit=limit))

return self._get_entire_data(
Expand Down
10 changes: 9 additions & 1 deletion crowdin_api/api_resources/groups/tests/test_groups_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

from crowdin_api.api_resources import GroupsResource
from crowdin_api.api_resources.enums import PatchOperation
from crowdin_api.api_resources.groups.enums import GroupPatchPath
from crowdin_api.api_resources.groups.enums import GroupPatchPath, ListGroupsOrderBy
from crowdin_api.requester import APIRequester
from crowdin_api.sorting import Sorting, SortingOrder, SortingRule


class TestGroupsResource:
Expand Down Expand Up @@ -51,18 +52,25 @@ def test_delete_group(self, m_request, base_absolut_url):
(
{},
{
"orderBy": None,
"parentId": None,
"limit": 25,
"offset": 0,
},
),
(
{
"orderBy": Sorting(
[SortingRule(ListGroupsOrderBy.NAME, SortingOrder.DESC)]
),
"parentId": "test",
"limit": 10,
"offset": 2,
},
{
"orderBy": Sorting(
[SortingRule(ListGroupsOrderBy.NAME, SortingOrder.DESC)]
),
"parentId": "test",
"limit": 10,
"offset": 2,
Expand Down
5 changes: 5 additions & 0 deletions crowdin_api/api_resources/labels/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@

class LabelsPatchPath(Enum):
TITLE = "/title"


class ListLabelsOrderBy(Enum):
ID = "id"
TITLE = "title"
Loading
Loading