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: Application api support added #138

Merged
merged 8 commits into from
Oct 30, 2023
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
2 changes: 2 additions & 0 deletions crowdin_api/api_resources/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .application.resource import ApplicationResource
from .bundles.resource import BundlesResource
from .dictionaries.resource import DictionariesResource
from .distributions.resource import DistributionsResource
Expand Down Expand Up @@ -25,6 +26,7 @@
from .workflows.resource import WorkflowsResource

__all__ = [
"ApplicationResource",
"BundlesResource",
"DictionariesResource",
"DistributionsResource",
Expand Down
1 change: 1 addition & 0 deletions crowdin_api/api_resources/application/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pdoc__ = {'tests': False}
106 changes: 106 additions & 0 deletions crowdin_api/api_resources/application/resource.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
from crowdin_api.parser import dumps

from crowdin_api.api_resources.abstract.resources import BaseResource


class ApplicationResource(BaseResource):
"""
Crowdin Apps are web applications that can be integrated with Crowdin to extend its functionality.

Use the API to manage the necessary app data.

Link to documentation:
https://developer.crowdin.com/api/v2/#tag/Applications

Link to documentation for enterprise:
https://developer.crowdin.com/enterprise/api/v2/#tag/Applications
"""

def get_application_path(self, applicationIdentifier: str, path: str):
return f"applications/{applicationIdentifier}/api/{path}"

def get_application_data(self, applicationIdentifier: str, path: str):
"""
Get Application Data.

Link to documentation:
https://developer.crowdin.com/api/v2/#operation/api.applications.api.get

Link to documentation for enterprise:
https://developer.crowdin.com/enterprise/api/v2/#operation/api.applications.api.get
"""

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

def update_application_data(self, applicationIdentifier: str, path: str, data: dict):
"""
Update or Restore Application Data.

Link to documentation:
https://developer.crowdin.com/api/v2/#operation/api.applications.api.put

Link to documentation for enterprise:
https://developer.crowdin.com/enterprise/api/v2/#operation/api.applications.api.put
"""

json_data = dumps(data)
return self.requester.request(
method="put",
path=self.get_application_path(applicationIdentifier, path),
request_data=json_data,
)

def add_application_data(self, applicationIdentifier: str, path: str, data: dict):
"""
Add Application Data.

Link to documentation:
https://developer.crowdin.com/api/v2/#operation/api.applications.api.post

Link to documentation for enterprise:
https://developer.crowdin.com/enterprise/api/v2/#operation/api.applications.api.post
"""

json_data = dumps(data)
return self.requester.request(
method="post",
path=self.get_application_path(applicationIdentifier, path),
request_data=json_data,
)

def delete_application_data(self, applicationIdentifier: str, path: str):
"""
Delete Application Data.

Link to documentation:
https://developer.crowdin.com/api/v2/#operation/api.applications.api.delete

Link to documentation for enterprise:
https://developer.crowdin.com/enterprise/api/v2/#operation/api.applications.api.delete
"""

return self.requester.request(
method='delete',
path=self.get_application_path(applicationIdentifier, path)
)

def edit_application_data(self, applicationIdentifier: str, path: str, data: dict):
"""
Edit Application Data.

Link to documentation:
https://developer.crowdin.com/api/v2/#operation/api.applications.api.patch

Link to documentation for enterprise:
https://developer.crowdin.com/enterprise/api/v2/#operation/api.applications.api.patch
"""

json_data = dumps(data)
return self.requester.request(
method="patch",
path=self.get_application_path(applicationIdentifier, path),
request_data=json_data,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
from unittest import mock


import pytest
from crowdin_api.api_resources.application.resource import ApplicationResource
from crowdin_api.requester import APIRequester


class TestApplicationResource:
resource_class = ApplicationResource

def get_resource(self, base_absolut_url):
return self.resource_class(requester=APIRequester(base_url=base_absolut_url))

@pytest.mark.parametrize(
"in_params, path",
(
({"applicationIdentifier": "abc", "path": "test"}, "applications/abc/api/test"),
),
)
def test_get_applications_path(self, in_params, path, base_absolut_url):
resource = self.get_resource(base_absolut_url)
assert resource.get_application_path(**in_params) == path

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

resource = self.get_resource(base_absolut_url)
assert resource.get_application_data(applicationIdentifier="abc", path="test") == "response"
m_request.assert_called_once_with(
method="get",
path=resource.get_application_path(applicationIdentifier="abc", path="test"),
)

@mock.patch("crowdin_api.requester.APIRequester.request")
def test_add_application_data(self, m_request, base_absolut_url):
m_request.return_value = "response"
resource = self.get_resource(base_absolut_url)
# # assert resource.add_application_data(**in_params, request_data) == "response"
dicts = {'key2': 2}
assert resource.add_application_data("abc", "test", dicts) == "response"
m_request.assert_called_once_with(
method="post",
path=resource.get_application_path(applicationIdentifier="abc", path="test"),
request_data='{"key2": 2}'
)

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

resource = self.get_resource(base_absolut_url)
assert resource.delete_application_data(applicationIdentifier="abc", path="test") == "response"
m_request.assert_called_once_with(
method="delete",
path=resource.get_application_path(applicationIdentifier="abc", path="test"),
)

@mock.patch("crowdin_api.requester.APIRequester.request")
def test_edit_application_data(self, m_request, base_absolut_url):
m_request.return_value = "response"
data = {"key2": 2}
resource = self.get_resource(base_absolut_url)
assert resource.edit_application_data(applicationIdentifier="abc", path="test", data=data) == "response"
m_request.assert_called_once_with(
method="patch",
path=resource.get_application_path(applicationIdentifier="abc", path="test"),
request_data='{"key2": 2}',
)

@mock.patch("crowdin_api.requester.APIRequester.request")
def test_update_application_data(self, m_request, base_absolut_url):
m_request.return_value = "response"
data = {"key2": 2}
resource = self.get_resource(base_absolut_url)
assert resource.update_application_data(applicationIdentifier="abc", path="test", data=data) == "response"
m_request.assert_called_once_with(
method="put",
path=resource.get_application_path(applicationIdentifier="abc", path="test"),
request_data='{"key2": 2}',
)
6 changes: 6 additions & 0 deletions crowdin_api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ def get_api_requestor(self) -> APIRequester:

return self._api_requestor

@property
def applications(self) -> api_resources.ApplicationResource:
return api_resources.ApplicationResource(
requester=self.get_api_requestor(), page_size=self.PAGE_SIZE
)

@property
def bundles(self) -> api_resources.BundlesResource:
return api_resources.BundlesResource(
Expand Down