Commit 38016ff4 authored by Georges Racinet's avatar Georges Racinet 🦑
Browse files

Heptapod: general helpers for API requests

These allow notably to send the requests as a given `User`.

Up to now, we had such methods only for projects, and more general
requests were handcrafted and generally running as `root`.

--HG--
branch : heptapod-0-14
parent 773abd8c5ee9
......@@ -288,6 +288,41 @@ class Heptapod:
assert resp.status_code == 200
self.settings = resp.json()
def api_request(self, method, user=None, subpath='', **kwargs):
"""Perform a simple API HTTP request
`method` is the HTTP method to use, same as in `requests.request`.
The full URL is made of the API URL of the instance, together with
the given subpath (example 'snippets/42').
Appropriate authentication headers are added on the fly.
:param user: the :class:`User` to run the request as. If not specified,
the request is sent as the root user.
All other kwargs are passed to `requests.request()`
"""
headers = kwargs.pop('headers', {})
token = self.owner_token if user is None else user.token
headers['Private-Token'] = token
return requests.request(method,
'/'.join((self.api_url, subpath)),
headers=headers,
**kwargs)
def api_get(self, **kwargs):
return self.api_request('GET', **kwargs)
def api_post(self, **kwargs):
return self.api_request('POST', **kwargs)
def api_put(self, **kwargs):
return self.api_request('PUT', **kwargs)
def api_delete(self, **kwargs):
return self.api_request('DELETE', **kwargs)
def load_ssh_keys(self):
"""Load client-side information to use SSH keys
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment