forked from prisma-labs/python-graphql-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modified python to use requests from this pull request: prisma-labs#20
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
Showing
3 changed files
with
31 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |