From b10a5f3474b83addff636b4a7e7362e1c1900daa Mon Sep 17 00:00:00 2001 From: Sunil Shah Date: Tue, 12 Sep 2017 19:25:47 -0700 Subject: [PATCH 1/3] Fixed to support python 3 urllib2 doesn't exist within python3. The proposed changes should be compatible with python2 and 3, tested on 2.7.13 and 3.6.0. --- graphqlclient/client.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/graphqlclient/client.py b/graphqlclient/client.py index 4ab34b7..998c125 100644 --- a/graphqlclient/client.py +++ b/graphqlclient/client.py @@ -1,4 +1,4 @@ -import urllib2 +import urllib import json class GraphQLClient: @@ -21,12 +21,12 @@ def _send(self, query, variables): if self.token is not None: headers['Authorization'] = 'Bearer %s' % self.token - req = urllib2.Request(self.endpoint, json.dumps(data), headers) + req = urllib.request(self.endpoint, json.dumps(data), headers) try: - response = urllib2.urlopen(req) + response = urllib.request.urlopen(req) return response.read() - except urllib2.HTTPError, e: - print(e.read()) + except urllib.error.HTTPError as e: + print((e.read())) print('') raise e From 37a7706197b37c7f17355b2330c4cd5bf3e068d6 Mon Sep 17 00:00:00 2001 From: Sunil Shah Date: Tue, 12 Sep 2017 21:25:59 -0700 Subject: [PATCH 2/3] Python 2 and 3 support OK this works in python 2 and python 3. However in python 2 it returns a unicode type and in python 3 a string class. It is also dependent on six.moves, so a dependency for six will need to be added. Using six is the easiest way to deal with urllib2 changes form Python2 to Python3. I can use the requests library if you guys prefer. I tested this against my own Gcool endpoint. These IDs will be deleted so I don't mind them in the public view. Python 3 returned: {"data":{"allDevices":[{"id":"cj65hnfrr34u00135z8l3glfq"},{"id":"cj65jm5kr3n6101350sfhk58t"},{"id":"cj65mdwpx0art0152rydd3yzy"},{"id":"cj65mg1sy0aum01529ro6x9h6"},{"id":"cj65mg6s30aut0152irb5j1th"}]}} The type is Python 2 returned: {"data":{"allDevices":[{"id":"cj65hnfrr34u00135z8l3glfq"},{"id":"cj65jm5kr3n6101350sfhk58t"},{"id":"cj65mdwpx0art0152rydd3yzy"},{"id":"cj65mg1sy0aum01529ro6x9h6"},{"id":"cj65mg6s30aut0152irb5j1th"}]}} The type is --- graphqlclient/client.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/graphqlclient/client.py b/graphqlclient/client.py index 998c125..d78734a 100644 --- a/graphqlclient/client.py +++ b/graphqlclient/client.py @@ -1,4 +1,4 @@ -import urllib +from six.moves import urllib import json class GraphQLClient: @@ -19,13 +19,13 @@ def _send(self, query, variables): 'Content-Type': 'application/json'} if self.token is not None: - headers['Authorization'] = 'Bearer %s' % self.token + headers['Authorization'] = 'Bearer {}'.format(self.token) - req = urllib.request(self.endpoint, json.dumps(data), headers) + req = urllib.request.Request(self.endpoint, json.dumps(data).encode('utf-8'), headers) try: response = urllib.request.urlopen(req) - return response.read() + return response.read().decode('utf-8') except urllib.error.HTTPError as e: print((e.read())) print('') From bba7ebdb391564337500fcca111ff5c17f12325c Mon Sep 17 00:00:00 2001 From: Sunil Shah Date: Tue, 12 Sep 2017 21:34:06 -0700 Subject: [PATCH 3/3] added dependency on six --- setup.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/setup.py b/setup.py index 09e4109..1438a59 100644 --- a/setup.py +++ b/setup.py @@ -8,4 +8,7 @@ author_email='hello@graph.cool', license='MIT', packages=['graphqlclient'], + install_requires=[ + 'six', + ], zip_safe=False)