From 842b2d2851108558178e0d600881e0805b37d46b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Luna?= Date: Mon, 2 Dec 2024 08:36:56 -0300 Subject: [PATCH 1/3] feat: add support for GraphQL endpoint -Add GraphQL method to CrowdinClient for interfacing with the GraphQL endpoint. -The implementation supports query strings and optional variables. -Includes comprehensive test coverage for request validation. Implements #180 --- crowdin_api/client.py | 12 +++++++++++- crowdin_api/tests/test_client.py | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/crowdin_api/client.py b/crowdin_api/client.py index c121322..d5693da 100644 --- a/crowdin_api/client.py +++ b/crowdin_api/client.py @@ -91,9 +91,19 @@ def get_api_requestor(self) -> APIRequester: default_headers=self.get_default_headers(), extended_params=self.EXTENDED_REQUEST_PARAMS ) - return self._api_requestor + def graphql(self, query: str, variables: Optional[Dict] = None) -> Dict: + data = { + "query": query, + "variables": variables or {} + } + return self.get_api_requestor().request( + method="post", + path="graphql", + request_data=data + ) + @property def ai(self) -> Union[api_resources.AIResource, api_resources.EnterpriseAIResource]: if self._is_enterprise_platform: diff --git a/crowdin_api/tests/test_client.py b/crowdin_api/tests/test_client.py index 836291d..2fd9e1e 100644 --- a/crowdin_api/tests/test_client.py +++ b/crowdin_api/tests/test_client.py @@ -210,6 +210,25 @@ def test_storages(self, _m_api_requestor, property_name, class_name): requester="api_requestor", project_id=1, page_size=25 ) + @mock.patch("crowdin_api.client.CrowdinClient.get_api_requestor") + def test_graphql(self, mock_get_requestor): + """Test GraphQL functionality with basic request validation.""" + client = CrowdinClient() + assert isinstance(client, CrowdinClient) + + mock_requestor = mock.Mock() + mock_get_requestor.return_value = mock_requestor + mock_requestor.request.return_value = {"data": {"test": True}} + + query = "query { test }" + client.graphql(query=query) + + mock_requestor.request.assert_called_once() + call_args = mock_requestor.request.call_args[1] + assert call_args["method"] == "post" + assert call_args["path"] == "graphql" + assert call_args["request_data"]["query"] == query + class TestCrowdinClientEnterprise: @pytest.mark.parametrize( From fc0648fcbb84cbc4739af4ca2b0e909f880c319d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Luna?= Date: Tue, 3 Dec 2024 09:46:13 -0300 Subject: [PATCH 2/3] feat: add support for GraphQL endpoint -Add GraphQL method to CrowdinClient for interfacing with the GraphQL endpoint. -The implementation supports query strings and optional variables. -Includes comprehensive test coverage for request validation. -Add documentation for GraphQL API usage in README. --- README.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/README.md b/README.md index 9185707..f39580d 100644 --- a/README.md +++ b/README.md @@ -159,6 +159,30 @@ class FirstCrowdinClient(CrowdinClient): EXTENDED_REQUEST_PARAMS = {"proxies": proxies} ``` +### GraphQL API +This library also provides possibility to use [GraphQL API](https://developer.crowdin.com/graphql-api/) (only for Crowdin Enterprise). + +```python +from crowdin_api import CrowdinClient + +client = CrowdinClient( + token='{token}', + organization='{organization}' +) + +query = """ +query { + viewer { + id + name + } +} +""" + +# Execute the GraphQL query +response = client.graphql(query=query) +``` + ## Seeking Assistance If you find any problems or would like to suggest a feature, please read the [How can I contribute](https://github.com/crowdin/crowdin-api-client-python/blob/main/CONTRIBUTING.md#how-can-i-contribute) section in our contributing guidelines. From 2cb8e92dd7b8bd5d1e24f4d12cb780395f9a3757 Mon Sep 17 00:00:00 2001 From: Andrii Bodnar Date: Tue, 3 Dec 2024 16:39:23 +0200 Subject: [PATCH 3/3] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f39580d..db91bbe 100644 --- a/README.md +++ b/README.md @@ -160,7 +160,8 @@ class FirstCrowdinClient(CrowdinClient): ``` ### GraphQL API -This library also provides possibility to use [GraphQL API](https://developer.crowdin.com/graphql-api/) (only for Crowdin Enterprise). + +This library also provides the possibility to use [GraphQL API](https://developer.crowdin.com/graphql-api/): ```python from crowdin_api import CrowdinClient