Skip to content

Commit 3c9d47e

Browse files
authored
Merge pull request #10 from prodigyeducation/take-headers
Allow overriding headers when making requests
2 parents 3d8c93b + 47addbd commit 3c9d47e

File tree

3 files changed

+47
-13
lines changed

3 files changed

+47
-13
lines changed

docs/CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ git clone git@github.com:prodigyeducation/python-graphql-client.git
2828
After that you'll need to install the required python dependencies as well as development dependencies for linting the project.
2929

3030
```bash
31-
pip install -e .["dev"]
31+
pip install -e ."[dev]"
3232
```
3333

3434
Finally, you should make sure `pre-commit` has setup the git hooks on your machine by running the following command. This will run the automated checks every time you commit.

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": "456", "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": "456",
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": "456", "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": "456",
201+
"New": "foo",
202+
},
187203
)
188204

189205
@unittest_run_loop

0 commit comments

Comments
 (0)