Add `verify_credentials` method.
This commit is contained in:
parent
5b2294b699
commit
c6698ae761
|
@ -10,6 +10,7 @@ TOKEN_PATH = '/api-token-auth/'
|
||||||
API_PATHS = {
|
API_PATHS = {
|
||||||
# auth
|
# auth
|
||||||
'token-auth': '/api-token-auth/',
|
'token-auth': '/api-token-auth/',
|
||||||
|
'my-account': 'accounts/',
|
||||||
|
|
||||||
# services
|
# services
|
||||||
'domain-list': 'domains/',
|
'domain-list': 'domains/',
|
||||||
|
@ -44,7 +45,7 @@ class Orchestra(object):
|
||||||
|
|
||||||
return response.json().get("token", None)
|
return response.json().get("token", None)
|
||||||
|
|
||||||
def request(self, verb, resource):
|
def request(self, verb, resource, raise_exception=True):
|
||||||
assert verb in ["HEAD", "GET", "POST", "PATCH", "PUT", "DELETE"]
|
assert verb in ["HEAD", "GET", "POST", "PATCH", "PUT", "DELETE"]
|
||||||
url = self.build_absolute_uri(resource)
|
url = self.build_absolute_uri(resource)
|
||||||
|
|
||||||
|
@ -52,6 +53,7 @@ class Orchestra(object):
|
||||||
response = verb(url, headers={"Authorization": "Token {}".format(
|
response = verb(url, headers={"Authorization": "Token {}".format(
|
||||||
self.auth_token)}, allow_redirects=False)
|
self.auth_token)}, allow_redirects=False)
|
||||||
|
|
||||||
|
if raise_exception:
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
|
|
||||||
status = response.status_code
|
status = response.status_code
|
||||||
|
@ -62,3 +64,20 @@ class Orchestra(object):
|
||||||
def retrieve_domains(self):
|
def retrieve_domains(self):
|
||||||
status, output = self.request("GET", 'domain-list')
|
status, output = self.request("GET", 'domain-list')
|
||||||
return output
|
return output
|
||||||
|
|
||||||
|
def retreve_profile(self):
|
||||||
|
_, output = self.request("GET", 'my-account')
|
||||||
|
return output
|
||||||
|
|
||||||
|
def verify_credentials(self):
|
||||||
|
"""
|
||||||
|
Returns:
|
||||||
|
A user profile info if the
|
||||||
|
credentials are valid, None otherwise.
|
||||||
|
"""
|
||||||
|
status, output = self.request("GET", 'my-account', raise_exception=False)
|
||||||
|
|
||||||
|
if status < 400:
|
||||||
|
return output
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
Loading…
Reference in New Issue