diff --git a/lib/vsc/utils/rest.py b/lib/vsc/utils/rest.py index 78f853d6..fe01566f 100644 --- a/lib/vsc/utils/rest.py +++ b/lib/vsc/utils/rest.py @@ -67,7 +67,7 @@ class Client: USER_AGENT = 'vsc-rest-client' def __init__(self, url, username=None, password=None, token=None, token_type='Token', user_agent=None, - append_slash=False): + append_slash=False, decode=True): """ Create a Client object, this client can consume a REST api hosted at host/endpoint @@ -81,6 +81,7 @@ def __init__(self, url, username=None, password=None, token=None, token_type='To self.username = username self.url = url self.append_slash = append_slash + self.decode = decode if not user_agent: self.user_agent = self.USER_AGENT @@ -193,9 +194,12 @@ def request(self, method, url, body, headers, content_type=None): else: body = conn.read() body = body.decode('utf-8') # byte encoded response - try: - pybody = json.loads(body) - except ValueError: + if self.decode: + try: + pybody = json.loads(body) + except ValueError: + pybody = body + else: pybody = body logging.debug('reponse len: %s ', len(pybody)) return status, pybody diff --git a/setup.py b/setup.py index 956ef2af..1e90eafb 100755 --- a/setup.py +++ b/setup.py @@ -36,7 +36,7 @@ from vsc.install.shared_setup import ag, kh, jt, sdw PACKAGE = { - 'version': '3.5.10', + 'version': '3.6.0', 'author': [sdw, jt, ag, kh], 'maintainer': [sdw, jt, ag, kh], 'install_requires': [ diff --git a/test/rest.py b/test/rest.py index 398b76c3..78a84873 100644 --- a/test/rest.py +++ b/test/rest.py @@ -134,3 +134,40 @@ def test_censor_request(self): nondict_payload = [payload, 'more_payload'] payload_censored = client.censor_request(['password'], nondict_payload) self.assertEqual(payload_censored, nondict_payload) + + +class RestClientNoDecodeTest(TestCase): + """ small test for The RestClient + This should not be to much, since there is an hourly limit of requests for the github api + """ + + def setUp(self): + """setup""" + super().setUp() + self.client = RestClient('https://api.github.com', username=GITHUB_LOGIN, token=GITHUB_TOKEN, decode=False) + + def test_client(self): + """Do a test api call""" + if GITHUB_TOKEN is None: + print("Skipping test_client, since no GitHub token is available") + return + + status, body = self.client.repos[GITHUB_USER][GITHUB_REPO].contents.a_directory['a_file.txt'].get() + self.assertEqual(status, 200) + # dGhpcyBpcyBhIGxpbmUgb2YgdGV4dAo= == 'this is a line of text' in base64 encoding + self.assertEqual(body['content'].strip(), "dGhpcyBpcyBhIGxpbmUgb2YgdGV4dAo=") + + status, body = self.client.repos['hpcugent']['easybuild-framework'].pulls[1].get() + self.assertEqual(status, 200) + self.assertEqual(body['merge_commit_sha'], 'fba3e13815f3d2a9dfbd2f89f1cf678dd58bb1f1') + + def test_get_method(self): + """A quick test of a GET to the github API""" + + status, body = self.client.users['hpcugent'].get() + self.assertEqual(status, 200) + self.assertTrue('"login":"hpcugent"' in body) + self.assertTrue('"id":1515263' in body) + + +