Skip to content

Commit aaa67f3

Browse files
authored
Merge pull request #8 from AcrossTheCloud/master
Allow the authorization header name to vary
2 parents 45059d8 + 494c3af commit aaa67f3

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,15 @@ print(result)
3232

3333
Authorization tokens can be added to the request using the client's `inject_token` method:
3434

35-
```
35+
```py
3636
client.inject_token('very-long-and-secure-token')
3737
```
38+
which defaults to http header name 'Authorization'. An alternative http header name for the token can be set by passing in the alternative header name, e.g. for 'x-api-key':
39+
40+
```py
41+
client.inject_token('very-long-and-secure-token','x-api-key')
42+
```
43+
3844

3945
## License
4046

graphqlclient/client.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ class GraphQLClient:
55
def __init__(self, endpoint):
66
self.endpoint = endpoint
77
self.token = None
8+
self.headername = None
89

910
def execute(self, query, variables=None):
1011
return self._send(query, variables)
1112

12-
def inject_token(self, token):
13+
def inject_token(self, token, headername='Authorization'):
1314
self.token = token
15+
self.headername = headername
1416

1517
def _send(self, query, variables):
1618
data = {'query': query,
@@ -19,7 +21,7 @@ def _send(self, query, variables):
1921
'Content-Type': 'application/json'}
2022

2123
if self.token is not None:
22-
headers['Authorization'] = '{}'.format(self.token)
24+
headers[self.headername] = '{}'.format(self.token)
2325

2426
req = urllib.request.Request(self.endpoint, json.dumps(data).encode('utf-8'), headers)
2527

0 commit comments

Comments
 (0)