From 1414f61fb652057f57ad87e207cf8971779e7c9a Mon Sep 17 00:00:00 2001 From: Dale Seo Date: Sun, 28 Feb 2021 17:07:21 -0500 Subject: [PATCH 01/11] removes global logging --- python_graphql_client/graphql_client.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/python_graphql_client/graphql_client.py b/python_graphql_client/graphql_client.py index 8d39676..16bf1f4 100644 --- a/python_graphql_client/graphql_client.py +++ b/python_graphql_client/graphql_client.py @@ -7,8 +7,6 @@ import requests import websockets -logging.basicConfig(level=logging.INFO, format="%(levelname)s:%(message)s") - class GraphqlClient: """Class which represents the interface to make graphQL requests through.""" From 4de0ad0ef9e3bdc20b9248eb3f7b461372d8ffc8 Mon Sep 17 00:00:00 2001 From: Dale Seo Date: Sun, 28 Feb 2021 19:46:17 -0500 Subject: [PATCH 02/11] stop using root logger --- python_graphql_client/graphql_client.py | 5 +++-- tests/test_graphql_client.py | 8 +++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/python_graphql_client/graphql_client.py b/python_graphql_client/graphql_client.py index 16bf1f4..1d3d104 100644 --- a/python_graphql_client/graphql_client.py +++ b/python_graphql_client/graphql_client.py @@ -13,6 +13,7 @@ class GraphqlClient: def __init__(self, endpoint: str, headers: dict = {}, **kwargs: Any): """Insantiate the client.""" + self.logger = logging.getLogger(__name__) self.endpoint = endpoint self.headers = headers self.options = kwargs @@ -104,8 +105,8 @@ async def subscribe( async for response_message in websocket: response_body = json.loads(response_message) if response_body["type"] == "connection_ack": - logging.info("the server accepted the connection") + self.logger.info("the server accepted the connection") elif response_body["type"] == "ka": - logging.info("the server sent a keep alive message") + self.logger.info("the server sent a keep alive message") else: handle(response_body["payload"]) diff --git a/tests/test_graphql_client.py b/tests/test_graphql_client.py index 8771d16..4a44207 100644 --- a/tests/test_graphql_client.py +++ b/tests/test_graphql_client.py @@ -269,9 +269,9 @@ async def test_subscribe(self, mock_connect): ] ) - @patch("logging.info") + @patch("logging.getLogger") @patch("websockets.connect") - async def test_does_not_crash_with_keep_alive(self, mock_connect, mock_info): + async def test_does_not_crash_with_keep_alive(self, mock_connect, mock_get_logger): """Subsribe a GraphQL subscription.""" mock_websocket = mock_connect.return_value.__aenter__.return_value mock_websocket.send = AsyncMock() @@ -288,7 +288,9 @@ async def test_does_not_crash_with_keep_alive(self, mock_connect, mock_info): await client.subscribe(query=query, handle=MagicMock()) - mock_info.assert_has_calls([call("the server sent a keep alive message")]) + mock_get_logger.return_value.info.assert_has_calls( + [call("the server sent a keep alive message")] + ) @patch("websockets.connect") async def test_headers_passed_to_websocket_connect(self, mock_connect): From 245afe39f6da2aa74a45e35e328e59399035f5d5 Mon Sep 17 00:00:00 2001 From: Dale Seo Date: Thu, 4 Mar 2021 16:26:02 -0500 Subject: [PATCH 03/11] bump version to 0.4.3 --- CHANGELOG.md | 5 +++++ setup.py | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8811f2b..039a3cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,9 @@ +## v0.4.3 + +- Stoping Using Root Logger with One-off Logging Setup #44 + ## v0.4.2 + - chore(deps): reduce strictness of dependencies #41 ## v0.4.1 diff --git a/setup.py b/setup.py index edc8c2d..fcd8f1e 100644 --- a/setup.py +++ b/setup.py @@ -10,7 +10,7 @@ setup( name="python_graphql_client", - version="0.4.2", + version="0.4.3", description="Python GraphQL Client", long_description=long_description, long_description_content_type="text/markdown", From 42e0cede1b6efcb489740cd0b5fe72fedc47e62f Mon Sep 17 00:00:00 2001 From: Ben Johnson Date: Tue, 5 Oct 2021 15:24:24 -0400 Subject: [PATCH 04/11] Update README to document custom headers --- README.md | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 948282a..4c21aa9 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ pip install python-graphql-client ## Usage -- Query/Mutation +### Queries & Mutations ```py from python_graphql_client import GraphqlClient @@ -47,7 +47,7 @@ data = asyncio.run(client.execute_async(query=query, variables=variables)) print(data) # => {'data': {'country': {'code': 'CA', 'name': 'Canada'}}} ``` -- Subscription +### Subscriptions ```py from python_graphql_client import GraphqlClient @@ -103,6 +103,14 @@ auth = HTTPBasicAuth('fake@example.com', 'not_a_real_password') client = GraphqlClient(endpoint="wss://www.your-api.com/graphql", auth=auth) ``` +### Custom Headers +```py +from python_graphql_client import GraphqlClient + +headers = { "Authorization": "Token SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV" } +client = GraphqlClient(endpoint="wss://www.your-api.com/graphql", headers=headers) +``` + ## Roadmap To start we'll try and use a Github project board for listing current work and updating priorities of upcoming features. From 20c4ad2e18e3885973811ac1fa16121990fd4e34 Mon Sep 17 00:00:00 2001 From: Justin Krinke Date: Wed, 29 Nov 2023 08:48:05 -0500 Subject: [PATCH 05/11] updating pre-commit --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 6b173e9..d9f256e 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -4,7 +4,7 @@ repos: hooks: - id: black language_version: python3 - - repo: https://gitlab.com/pycqa/flake8 + - repo: https://github.com/pycqa/flake8 rev: "3.8.4" hooks: - id: flake8 From ce45f220a8acfe51846af9c799720f4a0ebffc54 Mon Sep 17 00:00:00 2001 From: Justin Krinke Date: Wed, 29 Nov 2023 08:55:02 -0500 Subject: [PATCH 06/11] updating ci action --- .github/workflows/python-ci-checks.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/python-ci-checks.yml b/.github/workflows/python-ci-checks.yml index 9b3e869..c494a84 100644 --- a/.github/workflows/python-ci-checks.yml +++ b/.github/workflows/python-ci-checks.yml @@ -6,7 +6,7 @@ jobs: pre-commit: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - uses: actions/setup-python@v2 + - uses: actions/checkout@v3 + - uses: actions/setup-python@v3 - run: pip install -e . - uses: pre-commit/action@v2.0.0 From 4e6882353a5adc2a0579b49df1593dfc271f9c29 Mon Sep 17 00:00:00 2001 From: Justin Krinke Date: Wed, 29 Nov 2023 08:57:03 -0500 Subject: [PATCH 07/11] updating ci action --- .github/workflows/python-ci-checks.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/python-ci-checks.yml b/.github/workflows/python-ci-checks.yml index c494a84..63a2e94 100644 --- a/.github/workflows/python-ci-checks.yml +++ b/.github/workflows/python-ci-checks.yml @@ -9,4 +9,4 @@ jobs: - uses: actions/checkout@v3 - uses: actions/setup-python@v3 - run: pip install -e . - - uses: pre-commit/action@v2.0.0 + - uses: pre-commit/action@v3.0.0 From 86a751e0b7c86c871dc1eecf636c5d79be47cead Mon Sep 17 00:00:00 2001 From: Justin Krinke Date: Wed, 29 Nov 2023 09:01:09 -0500 Subject: [PATCH 08/11] updating ci action --- .pre-commit-config.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d9f256e..a1f8850 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,11 +1,11 @@ repos: - repo: https://github.com/psf/black - rev: 20.8b1 + rev: "23.11.0" hooks: - id: black language_version: python3 - repo: https://github.com/pycqa/flake8 - rev: "3.8.4" + rev: "6.1.0" hooks: - id: flake8 additional_dependencies: [flake8-docstrings, flake8-isort] From f78daba1b58211b77a1604c17cc5ace732690ff1 Mon Sep 17 00:00:00 2001 From: tom Date: Fri, 26 Jan 2024 11:32:54 +0300 Subject: [PATCH 09/11] 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 10/11] 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 11/11] 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()