Skip to content

Commit

Permalink
Merge pull request #6 from boegel/cleanup
Browse files Browse the repository at this point in the history
flesh out append_slash
  • Loading branch information
Jens Timmerman committed May 19, 2016
2 parents 10fdab8 + f86871e commit 627f01c
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions lib/vsc/utils/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,64 +105,58 @@ 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 and not url.endswith('/'):
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, body=None, **params):
"""
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)
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)
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)
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)
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, content_type=None):
Expand Down

0 comments on commit 627f01c

Please sign in to comment.