Add support for all requests methods

This commit is contained in:
Maël Kervella 2018-05-26 20:43:46 +00:00
parent 8409433e86
commit 7bcd0924b8

View file

@ -121,7 +121,31 @@ class Re2oAPIClient:
self._save_token_to_file()
return self.token['token']
def get(self, url, headers={}, params={}, *args, **kwargs):
def _request(self, method, url, headers={}, params={}, *args, **kwargs):
headers.update({
'Authorization': 'Token {}'.format(self.get_token())
})
if not 'format' in params.keys():
params.update({'format': 'json'})
response = getattr(requests, method)(
url, headers=headers, params=params, *args, **kwargs
)
response.raise_for_status()
return response.json()
def delete(self, *args, **kwargs):
"""
DELETE requests on a given URL that acts like `requests.delete` except
that authentication to the API is automatically done and JSON response
is decoded
:param url: (str) URL of the requests
:param: See `requests.post` params
:returns: (dict) The JSON-decoded result of the request
"""
return self._request('delete', *args, **kwargs)
def get(self, *args, **kwargs):
"""
GET requests on a given URL that acts like `requests.get` except that
authentication to the API is automatically done and JSON response is
@ -131,18 +155,11 @@ class Re2oAPIClient:
:param: See `requests.get` params
:returns: (dict) The JSON-decoded result of the request
"""
headers.update({
'Authorization': 'Token {}'.format(self.get_token())
})
if not 'format' in params.keys():
params.update({'format': 'json'})
response = requests.get(url, headers=headers, params=params, *args, **kwargs)
response.raise_for_status()
return response.json()
return self._request('get', *args, **kwargs)
def post(self, url, headers={}, params={}, *args, **kwargs):
def patch(self, *args, **kwargs):
"""
POST requests on a given URL that acts like `requests.past` except
PATCH requests on a given URL that acts like `requests.patch` except
that authentication to the API is automatically done and JSON response
is decoded
@ -150,14 +167,31 @@ class Re2oAPIClient:
:param: See `requests.post` params
:returns: (dict) The JSON-decoded result of the request
"""
headers.update({
'Authorization': 'Token {}'.format(self.get_token())
})
if not 'format' in params.keys():
params.update({'format': 'json'})
response = requests.post(url, headers=headers, params=params, *args, **kwargs)
response.raise_for_status()
return response.json()
return self._request('patch', *args, **kwargs)
def post(self, *args, **kwargs):
"""
POST requests on a given URL that acts like `requests.post` except
that authentication to the API is automatically done and JSON response
is decoded
:param url: (str) URL of the requests
:param: See `requests.post` params
:returns: (dict) The JSON-decoded result of the request
"""
return self._request('post', *args, **kwargs)
def put(self, *args, **kwargs):
"""
PUT requests on a given URL that acts like `requests.put` except
that authentication to the API is automatically done and JSON response
is decoded
:param url: (str) URL of the requests
:param: See `requests.post` params
:returns: (dict) The JSON-decoded result of the request
"""
return self._request('put', *args, **kwargs)
def get_url_for(self, name, **kwargs):
"""
@ -230,7 +264,8 @@ class Re2oAPIClient:
def __dir__(self):
ret = ['use_tls', 'token_file', 'hostname', 'token',
'need_renew_token', 'get_token', 'get', 'post', 'get_url_for']
'need_renew_token', 'get_token', 'delete', 'get', 'patch',
'post', 'put', 'get_url_for']
for name in endpoints.get_names():
if name.endswith('-list'):
ret.append('list_%s' % name[:-len('-list')])