From f78daba1b58211b77a1604c17cc5ace732690ff1 Mon Sep 17 00:00:00 2001 From: tom Date: Fri, 26 Jan 2024 11:32:54 +0300 Subject: [PATCH 1/3] add passing options(Ex: timeout, verify_ssl, ...aiohttp parameters) to execute_async method --- python_graphql_client/graphql_client.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/python_graphql_client/graphql_client.py b/python_graphql_client/graphql_client.py index 1d3d104..2037115 100644 --- a/python_graphql_client/graphql_client.py +++ b/python_graphql_client/graphql_client.py @@ -1,4 +1,5 @@ """Module containing graphQL client.""" + import json import logging from typing import Any, Callable @@ -60,6 +61,7 @@ async def execute_async( variables: dict = None, operation_name: str = None, headers: dict = {}, + **kwargs: Any, ): """Make asynchronous request to graphQL server.""" request_body = self.__request_body( @@ -71,6 +73,7 @@ async def execute_async( self.endpoint, json=request_body, headers={**self.headers, **headers}, + **{**self.options, **kwargs}, ) as response: return await response.json() From ce32e85ce929634cfdd38ce1e0dfab5178f2a0db Mon Sep 17 00:00:00 2001 From: Tom Date: Mon, 11 Nov 2024 13:38:41 +0300 Subject: [PATCH 2/3] add test with passing additional aiohttp parameters, remove potential headers key conflict in passing arguments --- python_graphql_client/graphql_client.py | 3 +-- tests/test_graphql_client.py | 28 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/python_graphql_client/graphql_client.py b/python_graphql_client/graphql_client.py index 2037115..0c199eb 100644 --- a/python_graphql_client/graphql_client.py +++ b/python_graphql_client/graphql_client.py @@ -72,8 +72,7 @@ async def execute_async( async with session.post( self.endpoint, json=request_body, - headers={**self.headers, **headers}, - **{**self.options, **kwargs}, + **{**self.options, **kwargs, "headers": {**self.headers, **headers}}, ) as response: return await response.json() diff --git a/tests/test_graphql_client.py b/tests/test_graphql_client.py index 4a44207..0bbe344 100644 --- a/tests/test_graphql_client.py +++ b/tests/test_graphql_client.py @@ -171,6 +171,34 @@ async def test_execute_basic_query(self, mock_post): "http://www.test-api.com/", json={"query": query}, headers={} ) + @patch("aiohttp.ClientSession.post") + async def test_execute_basic_query_with_aiohttp_parameters(self, mock_post): + """Sends a graphql POST request to an endpoint.""" + mock_post.return_value.__aenter__.return_value.json = AsyncMock() + client = GraphqlClient(endpoint="http://www.test-api.com/") + query = """ + { + tests { + status + } + } + """ + + await client.execute_async( + query, + timeout=10, + verify_ssl=False, + headers={"Authorization": "Bearer token"}, + ) + + mock_post.assert_called_once_with( + "http://www.test-api.com/", + json={"query": query}, + headers={"Authorization": "Bearer token"}, + timeout=10, + verify_ssl=False, + ) + @patch("aiohttp.ClientSession.post") async def test_execute_query_with_variables(self, mock_post): """Sends a graphql POST request with variables.""" From 081283af58d4dbef1afd586d4f7531679f3f02d7 Mon Sep 17 00:00:00 2001 From: Tom Date: Mon, 11 Nov 2024 13:44:46 +0300 Subject: [PATCH 3/3] remove potential json key conflict in passing arguments --- python_graphql_client/graphql_client.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/python_graphql_client/graphql_client.py b/python_graphql_client/graphql_client.py index 0c199eb..4f8388c 100644 --- a/python_graphql_client/graphql_client.py +++ b/python_graphql_client/graphql_client.py @@ -71,8 +71,12 @@ async def execute_async( async with aiohttp.ClientSession() as session: async with session.post( self.endpoint, - json=request_body, - **{**self.options, **kwargs, "headers": {**self.headers, **headers}}, + **{ + **self.options, + **kwargs, + "headers": {**self.headers, **headers}, + "json": request_body, + }, ) as response: return await response.json()