Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 7 additions & 28 deletions graphqlclient/client.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,13 @@
from six.moves import urllib
import json

class GraphQLClient:
def __init__(self, endpoint):
self.endpoint = endpoint
self.token = None
self.headername = 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 _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)

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

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 add_header(self, name, value):
self.headers[name] = value