Skip to content

Commit

Permalink
Merge pull request #230 from JensTimmerman/cleanup
Browse files Browse the repository at this point in the history
added test + fixes
  • Loading branch information
boegel committed May 19, 2016
2 parents 3d0f9a1 + 627f01c commit 2dc7c04
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 37 deletions.
56 changes: 26 additions & 30 deletions lib/vsc/utils/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ class Client(object):

USER_AGENT = 'vsc-rest-client'

def __init__(self, url, username=None, password=None, token=None, token_type='Token', user_agent=None, append_slash=False):
def __init__(self, url, username=None, password=None, token=None, token_type='Token', user_agent=None,
append_slash=False):
"""
Create a Client object,
this client can consume a REST api hosted at host/endpoint
Expand Down Expand Up @@ -104,78 +105,73 @@ def __init__(self, url, username=None, password=None, token=None, token_type='To
elif token is not None:
self.auth_header = '%s %s' % (token_type, token)

def _append_slash_to(self, url):
"""Append slash to specified URL, if desired and needed."""
if self.append_slash and not url.endswith('/'):
url += '/'
return url

def get(self, url, headers=None, **params):
"""
Do a http get request on the given url with given headers and parameters
Parameters is a dictionary that will will be urlencoded
"""
if self.append_slash:
url += '/'
url += self.urlencode(params)
url = self._append_slash_to(url) + self.urlencode(params)
return self.request(self.GET, url, None, headers)

def head(self, url, headers=None, **params):
"""
Do a http head request on the given url with given headers and parameters
Parameters is a dictionary that will will be urlencoded
"""
if self.append_slash:
url += '/'
url += self.urlencode(params)
url = self._append_slash_to(url) + self.urlencode(params)
return self.request(self.HEAD, url, None, headers)

def delete(self, url, headers=None, **params):
def delete(self, url, headers=None, body=None, **params):
"""
Do a http delete request on the given url with given headers and parameters
Do a http delete request on the given url with given headers, body and parameters
Parameters is a dictionary that will will be urlencoded
"""
if self.append_slash:
url += '/'
url += self.urlencode(params)
return self.request(self.DELETE, url, None, headers)
url = self._append_slash_to(url) + self.urlencode(params)
return self.request(self.DELETE, url, json.dumps(body), headers, content_type='application/json')

def post(self, url, body=None, headers=None, **params):
"""
Do a http post request on the given url with given body, headers and parameters
Parameters is a dictionary that will will be urlencoded
"""
if self.append_slash:
url += '/'
url += self.urlencode(params)
headers['Content-Type'] = 'application/json'
return self.request(self.POST, url, json.dumps(body), headers)
url = self._append_slash_to(url) + self.urlencode(params)
return self.request(self.POST, url, json.dumps(body), headers, content_type='application/json')

def put(self, url, body=None, headers=None, **params):
"""
Do a http put request on the given url with given body, headers and parameters
Parameters is a dictionary that will will be urlencoded
"""
if self.append_slash:
url += '/'
url += self.urlencode(params)
headers['Content-Type'] = 'application/json'
return self.request(self.PUT, url, json.dumps(body), headers)
url = self._append_slash_to(url) + self.urlencode(params)
return self.request(self.PUT, url, json.dumps(body), headers, content_type='application/json')

def patch(self, url, body=None, headers=None, **params):
"""
Do a http patch request on the given url with given body, headers and parameters
Parameters is a dictionary that will will be urlencoded
"""
if self.append_slash:
url += '/'
url += self.urlencode(params)
headers['Content-Type'] = 'application/json'
return self.request(self.PATCH, url, json.dumps(body), headers)
url = self._append_slash_to(url) + self.urlencode(params)
return self.request(self.PATCH, url, json.dumps(body), headers, content_type='application/json')

def request(self, method, url, body, headers):
def request(self, method, url, body, headers, content_type=None):
"""Low-level networking. All HTTP-method methods call this"""
if headers is None:
headers = {}

if content_type is not None:
headers['Content-Type'] = content_type

if self.auth_header is not None:
headers['Authorization'] = self.auth_header
headers['User-Agent'] = self.user_agent
fancylogger.getLogger().debug('cli request: %s, %s, %s, %s', method, url, body, headers)
#TODO: in recent python: Context manager
# TODO: in recent python: Context manager
conn = self.get_connection(method, url, body, headers)
status = conn.code
body = conn.read()
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
VSC_INSTALL_REQ_VERSION = '0.10.1'

PACKAGE = {
'version': '2.5.0',
'version': '2.5.1',
'author': [sdw, jt, ag, kh],
'maintainer': [sdw, jt, ag, kh],
# as long as 1.0.0 is not out, vsc-base should still provide vsc.fancylogger
Expand Down
21 changes: 15 additions & 6 deletions test/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
@author: Jens Timmerman (Ghent University)
"""
import os
from urllib2 import HTTPError

from vsc.install.testing import TestCase

Expand Down Expand Up @@ -68,9 +69,17 @@ def test_client(self):
self.assertEqual(status, 200)
self.assertEqual(body['merge_commit_sha'], u'fba3e13815f3d2a9dfbd2f89f1cf678dd58bb1f1')

def suite():
""" returns all the testcases in this module """
return TestLoader().loadTestsFromTestCase(RestClientTest)

if __name__ == '__main__':
main()
def test_request_methods(self):
"""Test all request methods"""
status, body = self.client.head()
self.assertEqual(status, 200)
try:
status, body = self.client.user.emails.post(body='[email protected]')
self.assertTrue(False, 'posting to unauthorized endpoint did not trhow a http error')
except HTTPError:
pass
try:
status, body = self.client.user.emails.delete(body='[email protected]')
self.assertTrue(False, 'deleting to unauthorized endpoint did not trhow a http error')
except HTTPError:
pass

0 comments on commit 2dc7c04

Please sign in to comment.