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): Screenshots APIs: add new parameters support #141

Merged
merged 6 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
11 changes: 7 additions & 4 deletions crowdin_api/api_resources/abstract/resources.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from abc import ABCMeta
from typing import Optional
from typing import Optional, Iterable
andrii-bodnar marked this conversation as resolved.
Show resolved Hide resolved

from crowdin_api.requester import APIRequester

Expand Down Expand Up @@ -39,12 +39,14 @@ def get_page_params(
else:
offset = offset or 0
if offset < 0:
raise ValueError("The offset must be greater than or equal to 0.")
raise ValueError(
"The offset must be greater than or equal to 0.")

limit = limit or self.page_size

if limit < 1:
raise ValueError("The limit must be greater than or equal to 1.")
raise ValueError(
"The limit must be greater than or equal to 1.")

return {"offset": offset, "limit": limit}

Expand Down Expand Up @@ -90,7 +92,8 @@ def _fetch_all(
while True:
params.update({"limit": limit, "offset": offset})

content = self.requester.request(method=method, path=path, params=params)
content = self.requester.request(
method=method, path=path, params=params)
data = content.get("data", [])
data and join_data.extend(data)

Expand Down
17 changes: 16 additions & 1 deletion crowdin_api/api_resources/screenshots/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ def get_screenshots_path(self, projectId: int, screenshotId: Optional[int] = Non
def list_screenshots(
self,
projectId: Optional[int] = None,
stringId: Optional[int] = None,
page: Optional[int] = None,
offset: Optional[int] = None,
limit: Optional[int] = None,
labelIds: Optional[Iterable[int]] = None,
excludeLabelIds: Optional[Iterable[int]] = None
andrii-bodnar marked this conversation as resolved.
Show resolved Hide resolved
):
"""
List Screenshots.
Expand All @@ -44,10 +47,13 @@ def list_screenshots(

projectId = projectId or self.get_project_id()

params = {"stringId": stringId, "labelIds": labelIds, "excludeLabelIds": excludeLabelIds}
params.update(self.get_page_params(page=page, offset=offset, limit=limit))

return self._get_entire_data(
method="get",
path=self.get_screenshots_path(projectId=projectId),
params=self.get_page_params(page=page, offset=offset, limit=limit),
params=params,
)

def add_screenshot(
Expand All @@ -56,6 +62,10 @@ def add_screenshot(
name: str,
projectId: Optional[int] = None,
autoTag: Optional[bool] = None,
fileId: Optional[int] = None,
branchId: Optional[int] = None,
directoryId: Optional[int] = None,
labelIds: Optional[Iterable[int]] = None,
):
"""
Add Screenshot.
Expand All @@ -66,13 +76,18 @@ def add_screenshot(

projectId = projectId or self.get_project_id()


return self.requester.request(
method="post",
path=self.get_screenshots_path(projectId=projectId),
request_data={
"storageId": storageId,
"name": name,
"autoTag": autoTag,
"fileId": fileId,
"branchId": branchId,
"directoryId": directoryId,
"labelIds": labelIds,
},
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,49 +35,59 @@ def test_get_screenshots_path(self, in_params, path, base_absolut_url):
@mock.patch("crowdin_api.requester.APIRequester.request")
def test_list_screenshots(self, m_request, base_absolut_url):
m_request.return_value = "response"

resource = self.get_resource(base_absolut_url)
assert resource.list_screenshots(projectId=1, **{"offset": 0, "limit": 10}) == "response"
m_request.assert_called_once_with(
method="get",
params={"offset": 0, "limit": 10},
path=resource.get_screenshots_path(projectId=1),
params={'stringId': None, 'labelIds': None, 'excludeLabelIds': None, 'offset': 0, 'limit': 10},
)

@pytest.mark.parametrize(
"in_params, request_data",
(
"in_params, expected_params",
[
(
{
"storageId": 1,
"name": "name",
"name": "test_screenshot",
"projectId": 1,
"autoTag": True,
"fileId": 2,
"branchId": 3,
"directoryId": 4,
"labelIds": [5, 6],
},
{
"storageId": 1,
"name": "test_screenshot",
"autoTag": True,
"fileId": 2,
"branchId": 3,
"directoryId": 4,
"labelIds": [5, 6],
},
{"storageId": 1, "name": "name", "autoTag": None},
),
(
{"storageId": 1, "name": "name", "autoTag": True},
{"storageId": 1, "name": "name", "autoTag": True},
),
),
],
)
@mock.patch("crowdin_api.requester.APIRequester.request")
def test_add_screenshot(self, m_request, in_params, request_data, base_absolut_url):
def test_add_screenshot(self, m_request, in_params, expected_params, base_absolut_url):
m_request.return_value = "response"

resource = self.get_resource(base_absolut_url)
assert resource.add_screenshot(projectId=1, **in_params) == "response"
assert resource.add_screenshot(**in_params) == "response"
m_request.assert_called_once_with(
method="post",
path=resource.get_screenshots_path(projectId=1),
request_data=request_data,
request_data=expected_params,
)

@mock.patch("crowdin_api.requester.APIRequester.request")
def test_get_screenshot(self, m_request, base_absolut_url):
m_request.return_value = "response"

resource = self.get_resource(base_absolut_url)
assert resource.get_screenshot(projectId=1, screenshotId=2) == "response"
assert resource.get_screenshot(
projectId=1, screenshotId=2) == "response"
m_request.assert_called_once_with(
method="get",
path=resource.get_screenshots_path(projectId=1, screenshotId=2),
Expand All @@ -89,7 +99,8 @@ def test_update_screenshot(self, m_request, base_absolut_url):

resource = self.get_resource(base_absolut_url)
assert (
resource.update_screenshot(projectId=1, screenshotId=2, storageId=3, name="test")
resource.update_screenshot(
projectId=1, screenshotId=2, storageId=3, name="test")
== "response"
)
m_request.assert_called_once_with(
Expand All @@ -103,7 +114,8 @@ def test_delete_screenshot(self, m_request, base_absolut_url):
m_request.return_value = "response"

resource = self.get_resource(base_absolut_url)
assert resource.delete_screenshot(projectId=1, screenshotId=2) == "response"
assert resource.delete_screenshot(
projectId=1, screenshotId=2) == "response"
m_request.assert_called_once_with(
method="delete",
path=resource.get_screenshots_path(projectId=1, screenshotId=2),
Expand All @@ -122,7 +134,8 @@ def test_edit_screenshot(self, m_request, base_absolut_url):
]

resource = self.get_resource(base_absolut_url)
assert resource.edit_screenshot(projectId=1, screenshotId=2, data=data) == "response"
assert resource.edit_screenshot(
projectId=1, screenshotId=2, data=data) == "response"
m_request.assert_called_once_with(
method="patch",
request_data=data,
Expand All @@ -133,7 +146,8 @@ def test_edit_screenshot(self, m_request, base_absolut_url):
@pytest.mark.parametrize(
"in_params, path",
(
({"projectId": 1, "screenshotId": 2}, "projects/1/screenshots/2/tags"),
({"projectId": 1, "screenshotId": 2},
"projects/1/screenshots/2/tags"),
(
{"projectId": 1, "screenshotId": 2, "tagId": 3},
"projects/1/screenshots/2/tags/3",
Expand Down
2 changes: 1 addition & 1 deletion crowdin_api/api_resources/screenshots/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


class ScreenshotPatchRequest(TypedDict):
value: Any
value: str
op: Union[PatchOperation, str]
path: ScreenshotPatchPath

Expand Down
Loading