Skip to content

Commit 8a6da93

Browse files
committed
support options parameter
1 parent 02492c7 commit 8a6da93

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

python_graphql_client/graphql_client.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313
class GraphqlClient:
1414
"""Class which represents the interface to make graphQL requests through."""
1515

16-
def __init__(self, endpoint: str, headers: dict = None):
16+
def __init__(self, endpoint: str, headers: dict = {}, options: dict = {}):
1717
"""Insantiate the client."""
1818
self.endpoint = endpoint
19-
self.headers = headers or {}
19+
self.headers = headers
20+
self.options = options
2021

2122
def __request_body(
2223
self, query: str, variables: dict = None, operation_name: str = None
@@ -31,15 +32,13 @@ def __request_body(
3132

3233
return json
3334

34-
def __request_headers(self, headers: dict = None) -> dict:
35-
return {**self.headers, **headers} if headers else self.headers
36-
3735
def execute(
3836
self,
3937
query: str,
4038
variables: dict = None,
4139
operation_name: str = None,
42-
headers: dict = None,
40+
headers: dict = {},
41+
options: dict = {},
4342
):
4443
"""Make synchronous request to graphQL server."""
4544
request_body = self.__request_body(
@@ -49,7 +48,8 @@ def execute(
4948
result = requests.post(
5049
self.endpoint,
5150
json=request_body,
52-
headers=self.__request_headers(headers),
51+
headers={**self.headers, **headers},
52+
**{**self.options, **options},
5353
)
5454

5555
result.raise_for_status()
@@ -60,7 +60,7 @@ async def execute_async(
6060
query: str,
6161
variables: dict = None,
6262
operation_name: str = None,
63-
headers: dict = None,
63+
headers: dict = {},
6464
):
6565
"""Make asynchronous request to graphQL server."""
6666
request_body = self.__request_body(
@@ -69,9 +69,7 @@ async def execute_async(
6969

7070
async with aiohttp.ClientSession() as session:
7171
async with session.post(
72-
self.endpoint,
73-
json=request_body,
74-
headers=self.__request_headers(headers),
72+
self.endpoint, json=request_body, headers={**self.headers, **headers},
7573
) as response:
7674
return await response.json()
7775

@@ -81,7 +79,7 @@ async def subscribe(
8179
handle: Callable,
8280
variables: dict = None,
8381
operation_name: str = None,
84-
headers: dict = None,
82+
headers: dict = {},
8583
):
8684
"""Make asynchronous request for GraphQL subscription."""
8785
connection_init_message = json.dumps({"type": "connection_init", "payload": {}})
@@ -96,7 +94,7 @@ async def subscribe(
9694
async with websockets.connect(
9795
self.endpoint,
9896
subprotocols=["graphql-ws"],
99-
extra_headers=self.__request_headers(headers),
97+
extra_headers={**self.headers, **headers},
10098
) as websocket:
10199
await websocket.send(connection_init_message)
102100
await websocket.send(request_message)

tests/test_graphql_client.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from unittest.mock import AsyncMock, MagicMock, call, patch
55

66
from aiohttp import web
7+
from requests.auth import HTTPBasicAuth
78
from requests.exceptions import HTTPError
89

910
from python_graphql_client import GraphqlClient
@@ -98,6 +99,24 @@ def test_execute_query_with_headers(self, post_mock):
9899
},
99100
)
100101

102+
@patch("python_graphql_client.graphql_client.requests.post")
103+
def test_execute_query_with_options(self, post_mock):
104+
"""Sends a graphql POST request with headers."""
105+
auth = HTTPBasicAuth("fake@example.com", "not_a_real_password")
106+
client = GraphqlClient(
107+
endpoint="http://www.test-api.com/", options={"auth": auth},
108+
)
109+
query = ""
110+
client.execute(query=query, options={"verify": False})
111+
112+
post_mock.assert_called_once_with(
113+
"http://www.test-api.com/",
114+
json={"query": query},
115+
headers={},
116+
auth=HTTPBasicAuth("fake@example.com", "not_a_real_password"),
117+
verify=False,
118+
)
119+
101120
@patch("python_graphql_client.graphql_client.requests.post")
102121
def test_execute_query_with_operation_name(self, post_mock):
103122
"""Sends a graphql POST request with the operationName key set."""
@@ -302,8 +321,4 @@ async def test_headers_passed_to_websocket_connect(self, mock_connect):
302321
extra_headers=expected_headers,
303322
)
304323

305-
mock_handle.assert_has_calls(
306-
[
307-
call({"data": {"messageAdded": "one"}}),
308-
]
309-
)
324+
mock_handle.assert_has_calls([call({"data": {"messageAdded": "one"}})])

0 commit comments

Comments
 (0)