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 4 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
27 changes: 22 additions & 5 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 @@ -30,23 +30,39 @@ def get_page_params(
page: Optional[int] = None,
offset: Optional[int] = None,
limit: Optional[int] = None,
labelIds: Optional[Iterable[int]] = None,
excludeLabelIds: Optional[Iterable[int]] = None
Mahhheshh marked this conversation as resolved.
Show resolved Hide resolved
):
if page is not None and (offset is not None or limit is not None):
raise ValueError("You must set page or offset and limit.")

if (labelIds is not None and excludeLabelIds is not None):
raise ValueError(
"Cannot use both labelIds and excludeLabelIds together.")

if page:
return self._get_page_params(page=page)
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.")

params = {"offset": offset, "limit": limit}

if labelIds is not None:
params.update({"labelIds": labelIds})

if excludeLabelIds is not None:
params.update({"excludeLabelIds": excludeLabelIds})

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

def with_fetch_all(self, max_limit: Optional[int] = None):
self._max_limit = max_limit
Expand Down Expand Up @@ -90,7 +106,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
54 changes: 37 additions & 17 deletions crowdin_api/api_resources/screenshots/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def list_screenshots(
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 @@ -47,7 +49,8 @@ def list_screenshots(
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=self.get_page_params(
page=page, offset=offset, limit=limit, labelIds=labelIds, excludeLabelIds=excludeLabelIds),
andrii-bodnar marked this conversation as resolved.
Show resolved Hide resolved
)

def add_screenshot(
Expand All @@ -56,6 +59,7 @@ def add_screenshot(
name: str,
projectId: Optional[int] = None,
autoTag: Optional[bool] = None,
labelIds: Optional[Iterable[int]] = None
andrii-bodnar marked this conversation as resolved.
Show resolved Hide resolved
):
"""
Add Screenshot.
Expand All @@ -66,14 +70,19 @@ def add_screenshot(

projectId = projectId or self.get_project_id()

request_data = {
"storageId": storageId,
"name": name,
"autoTag": autoTag,
}

if labelIds is not None:
andrii-bodnar marked this conversation as resolved.
Show resolved Hide resolved
request_data.update({"labelIds": labelIds})

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

def get_screenshot(self, screenshotId: int, projectId: Optional[int] = None):
Expand All @@ -88,7 +97,8 @@ def get_screenshot(self, screenshotId: int, projectId: Optional[int] = None):

return self.requester.request(
method="get",
path=self.get_screenshots_path(projectId=projectId, screenshotId=screenshotId),
path=self.get_screenshots_path(
projectId=projectId, screenshotId=screenshotId),
andrii-bodnar marked this conversation as resolved.
Show resolved Hide resolved
)

def update_screenshot(
Expand All @@ -113,7 +123,8 @@ def update_screenshot(
"storageId": storageId,
"name": name,
},
path=self.get_screenshots_path(projectId=projectId, screenshotId=screenshotId),
path=self.get_screenshots_path(
projectId=projectId, screenshotId=screenshotId),
andrii-bodnar marked this conversation as resolved.
Show resolved Hide resolved
)

def delete_screenshot(self, screenshotId: int, projectId: Optional[int] = None):
Expand All @@ -128,7 +139,8 @@ def delete_screenshot(self, screenshotId: int, projectId: Optional[int] = None):

return self.requester.request(
method="delete",
path=self.get_screenshots_path(projectId=projectId, screenshotId=screenshotId),
path=self.get_screenshots_path(
projectId=projectId, screenshotId=screenshotId),
)

def edit_screenshot(
Expand All @@ -149,7 +161,8 @@ def edit_screenshot(
return self.requester.request(
method="patch",
request_data=data,
path=self.get_screenshots_path(projectId=projectId, screenshotId=screenshotId),
path=self.get_screenshots_path(
projectId=projectId, screenshotId=screenshotId),
)

# Tags
Expand Down Expand Up @@ -199,7 +212,8 @@ def replace_tags(

return self.requester.request(
method="put",
path=self.get_tags_path(projectId=projectId, screenshotId=screenshotId),
path=self.get_tags_path(
projectId=projectId, screenshotId=screenshotId),
request_data=data,
)

Expand All @@ -217,7 +231,8 @@ def auto_tag(

return self.requester.request(
method="put",
path=self.get_tags_path(projectId=projectId, screenshotId=screenshotId),
path=self.get_tags_path(
projectId=projectId, screenshotId=screenshotId),
request_data={"autoTag": autoTag},
)

Expand All @@ -238,7 +253,8 @@ def add_tag(

return self.requester.request(
method="post",
path=self.get_tags_path(projectId=projectId, screenshotId=screenshotId),
path=self.get_tags_path(
projectId=projectId, screenshotId=screenshotId),
request_data=data,
)

Expand All @@ -254,7 +270,8 @@ def clear_tags(self, screenshotId: int, projectId: Optional[int] = None):

return self.requester.request(
method="delete",
path=self.get_tags_path(projectId=projectId, screenshotId=screenshotId),
path=self.get_tags_path(
projectId=projectId, screenshotId=screenshotId),
)

def get_tag(self, screenshotId: int, tagId: int, projectId: Optional[int] = None):
Expand All @@ -269,7 +286,8 @@ def get_tag(self, screenshotId: int, tagId: int, projectId: Optional[int] = None

return self.requester.request(
method="get",
path=self.get_tags_path(projectId=projectId, screenshotId=screenshotId, tagId=tagId),
path=self.get_tags_path(
projectId=projectId, screenshotId=screenshotId, tagId=tagId),
)

def delete_tag(
Expand All @@ -286,7 +304,8 @@ def delete_tag(

return self.requester.request(
method="delete",
path=self.get_tags_path(projectId=projectId, screenshotId=screenshotId, tagId=tagId),
path=self.get_tags_path(
projectId=projectId, screenshotId=screenshotId, tagId=tagId),
)

def edit_tag(
Expand All @@ -308,5 +327,6 @@ def edit_tag(
return self.requester.request(
method="patch",
request_data=data,
path=self.get_tags_path(projectId=projectId, screenshotId=screenshotId, tagId=tagId),
path=self.get_tags_path(
projectId=projectId, screenshotId=screenshotId, tagId=tagId),
)
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,45 @@ 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),
)
assert resource.list_screenshots(
projectId=1, **{"offset": 0, "limit": 10}) == "response"
assert resource.list_screenshots(
projectId=1, **{"offset": 0, "limit": 10, "labelIds": [1, 2]}) == "response"
assert resource.list_screenshots(
projectId=1, **{"offset": 0, "limit": 10, "excludeLabelIds": [1, 2]}) == "response"
assert_call_list = [
mock.call(
method="get",
params={"offset": 0, "limit": 10},
path=resource.get_screenshots_path(projectId=1),
),
mock.call(
method="get",
params={"offset": 0, "limit": 10, "labelIds": [1, 2]},
path=resource.get_screenshots_path(projectId=1),
),
mock.call(
method="get",
params={"offset": 0, "limit": 10, "excludeLabelIds": [1, 2]},
path=resource.get_screenshots_path(projectId=1),
)
]
assert m_request.call_args_list == assert_call_list

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

resource = self.get_resource(base_absolut_url)
with pytest.raises(ValueError):
assert resource.list_screenshots(
projectId=1, **{"offset": 0, "limit": 10, "labelIds": [1, 2], "excludeLabelIds": [3, 2]}) == "response"
m_request.assert_called_once_with(
method="get",
params={"offset": 0, "limit": 10, "labelIds": [
1, 2], "excludeLabelIds": [3, 2]},
path=resource.get_screenshots_path(projectId=1),
)

@pytest.mark.parametrize(
"in_params, request_data",
Expand All @@ -55,8 +88,10 @@ def test_list_screenshots(self, m_request, base_absolut_url):
{"storageId": 1, "name": "name", "autoTag": None},
),
(
{"storageId": 1, "name": "name", "autoTag": True},
{"storageId": 1, "name": "name", "autoTag": True},
{"storageId": 1, "name": "name",
"autoTag": True, "labelIds": [1, 2]},
{"storageId": 1, "name": "name",
"autoTag": True, "labelIds": [1, 2]},
),
),
)
Expand All @@ -77,7 +112,8 @@ 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 +125,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 +140,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 +160,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 +172,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