Skip to content

Commit 73729e1

Browse files
committed
Allow overriding headers when making requests
1 parent 3d8c93b commit 73729e1

File tree

2 files changed

+46
-12
lines changed

2 files changed

+46
-12
lines changed

python_graphql_client/graphql_client.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,34 @@ def __request_body(
2525

2626
return json
2727

28-
def execute(self, query: str, variables: dict = None, operation_name: str = None):
28+
def __request_headers(self, headers: dict = None) -> dict:
29+
return {**self.headers, **headers} if headers else self.headers
30+
31+
def execute(
32+
self,
33+
query: str,
34+
variables: dict = None,
35+
operation_name: str = None,
36+
headers: dict = None,
37+
):
2938
"""Make synchronous request to graphQL server."""
3039
request_body = self.__request_body(
3140
query=query, variables=variables, operation_name=operation_name
3241
)
3342

34-
result = requests.post(self.endpoint, json=request_body, headers=self.headers)
43+
result = requests.post(
44+
self.endpoint, json=request_body, headers=self.__request_headers(headers),
45+
)
46+
3547
result.raise_for_status()
3648
return result.json()
3749

3850
async def execute_async(
39-
self, query: str, variables: dict = None, operation_name: str = None
51+
self,
52+
query: str,
53+
variables: dict = None,
54+
operation_name: str = None,
55+
headers: dict = None,
4056
):
4157
"""Make asynchronous request to graphQL server."""
4258
request_body = self.__request_body(
@@ -45,6 +61,8 @@ async def execute_async(
4561

4662
async with aiohttp.ClientSession() as session:
4763
async with session.post(
48-
self.endpoint, json=request_body, headers=self.headers
64+
self.endpoint,
65+
json=request_body,
66+
headers=self.__request_headers(headers),
4967
) as response:
5068
return await response.json()

tests/test_graphql_client.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,21 @@ def test_raises_http_errors_as_exceptions(self, post_mock):
8383
@patch("python_graphql_client.graphql_client.requests.post")
8484
def test_execute_query_with_headers(self, post_mock):
8585
"""Sends a graphql POST request with headers."""
86-
headers = {"Content-Type": "application/json"}
87-
client = GraphqlClient(endpoint="http://www.test-api.com/", headers=headers)
86+
client = GraphqlClient(
87+
endpoint="http://www.test-api.com/",
88+
headers={"Content-Type": "application/json", "Existing": "123",},
89+
)
8890
query = ""
89-
client.execute(query)
91+
client.execute(query=query, headers={"Existing": "123", "New": "foo"})
9092

9193
post_mock.assert_called_once_with(
92-
"http://www.test-api.com/", json={"query": query}, headers=headers
94+
"http://www.test-api.com/",
95+
json={"query": query},
96+
headers={
97+
"Content-Type": "application/json",
98+
"Existing": "123",
99+
"New": "foo",
100+
},
93101
)
94102

95103
@patch("python_graphql_client.graphql_client.requests.post")
@@ -176,14 +184,22 @@ async def test_execute_query_with_variables(self, mock_post):
176184
async def test_execute_query_with_headers(self, mock_post):
177185
"""Sends a graphql POST request with headers."""
178186
mock_post.return_value.__aenter__.return_value.json = CoroutineMock()
179-
headers = {"Content-Type": "application/json"}
180-
client = GraphqlClient(endpoint="http://www.test-api.com/", headers=headers)
187+
client = GraphqlClient(
188+
endpoint="http://www.test-api.com/",
189+
headers={"Content-Type": "application/json", "Existing": "123",},
190+
)
181191
query = ""
182192

183-
await client.execute_async(query)
193+
await client.execute_async("", headers={"Existing": "123", "New": "foo"})
184194

185195
mock_post.assert_called_once_with(
186-
"http://www.test-api.com/", json={"query": query}, headers=headers
196+
"http://www.test-api.com/",
197+
json={"query": query},
198+
headers={
199+
"Content-Type": "application/json",
200+
"Existing": "123",
201+
"New": "foo",
202+
},
187203
)
188204

189205
@unittest_run_loop

0 commit comments

Comments
 (0)