Skip to content

Commit

Permalink
Modified python to use requests from this pull request: prisma-labs#20
Browse files Browse the repository at this point in the history
Changed to camelCase and added the specific token case
Added some comments
Updated readme with alternate calls and some more token examples
  • Loading branch information
bruce committed Jan 27, 2020
1 parent d3588c8 commit 26d185a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 36 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,28 @@ print(result)

### Authorization

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

```py
client.inject_token('very-long-and-secure-token')
client.addToken('very-long-and-secure-token')
```

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`:

```py
client.inject_token('very-long-and-secure-token','x-api-key')
client.addHeader('very-long-and-secure-token','x-api-key')


Note: there are different ways of specifying token so token maybe like these:
addHeader('Bearer {}'.format(tokenStr)) or
addHeader('token {}'.format(tokenStr)) there are others..
```

If you need more options for headers use

```py
client.inject_headers({'your_custom_header_name' : 'your_custom_header_value' ,
client.addHeader({'your_custom_header_name' : 'your_custom_header_value' ,
'your_custom_header_name_2' :'your_custom_header_value_2'})
```

Expand Down
Empty file modified example_run.py
100644 → 100755
Empty file.
54 changes: 22 additions & 32 deletions graphqlclient/client.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,30 @@
from six.moves import urllib
from requests import post
import json

class GraphQLClient:
""" A Simple GraphQL Client
"""
def __init__(self, endpoint):
self.endpoint = endpoint
self.token = None
self.headername = None
self.headersDict = None
self.headers = {
'Accept': 'application/json',
'Content-Type': 'application/json'
}

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

def inject_token(self, token, headername='Authorization'):
self.token = token
self.headername = headername

def inject_headers(self, headersDict):
self.headersDict = headersDict

def _send(self, query, variables):
data = {'query': query,
'variables': variables}
headers = {'Accept': 'application/json',
'Content-Type': 'application/json'}

if self.token is not None:
headers[self.headername] = '{}'.format(self.token)
if self.headersDict is not None:
headers.update(self.headersDict)

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

try:
response = urllib.request.urlopen(req)
return response.read().decode('utf-8')
except urllib.error.HTTPError as e:
print((e.read()))
print('')
raise e
def execute(self, query, variables=None):
""" Run query on graphql endpoint
"""
return post(self.endpoint, json=dict(query=query, variables=variables),
headers=self.headers).json()

def addHeader(self, name, value):
""" Addition headers that are needed for calls
"""
self.headers[name] = value

def addToken(self, value):
""" Addition headers that are needed for alls - such as bearer token
"""
self.headers['Authorization'] = value

0 comments on commit 26d185a

Please sign in to comment.