Skip to content

Commit

Permalink
Merge pull request #350 from itkovian/rest-json-no-decode
Browse files Browse the repository at this point in the history
Rest client option to not decode JSON to a python dict
  • Loading branch information
stdweird authored Nov 28, 2023
2 parents 2ae8e60 + c831a3e commit a18708d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
12 changes: 8 additions & 4 deletions lib/vsc/utils/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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': [
Expand Down
37 changes: 37 additions & 0 deletions test/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)



0 comments on commit a18708d

Please sign in to comment.