From c6698ae761af6dc9860fb2f10d28bcd560ffcc7c Mon Sep 17 00:00:00 2001 From: Santiago Lamora Date: Wed, 30 Oct 2019 14:00:12 +0100 Subject: [PATCH] Add `verify_credentials` method. --- musician/api.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/musician/api.py b/musician/api.py index 8f7bf32..9e91561 100644 --- a/musician/api.py +++ b/musician/api.py @@ -10,6 +10,7 @@ TOKEN_PATH = '/api-token-auth/' API_PATHS = { # auth 'token-auth': '/api-token-auth/', + 'my-account': 'accounts/', # services 'domain-list': 'domains/', @@ -44,7 +45,7 @@ class Orchestra(object): 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"] url = self.build_absolute_uri(resource) @@ -52,7 +53,8 @@ class Orchestra(object): response = verb(url, headers={"Authorization": "Token {}".format( self.auth_token)}, allow_redirects=False) - response.raise_for_status() + if raise_exception: + response.raise_for_status() status = response.status_code output = response.json() @@ -62,3 +64,20 @@ class Orchestra(object): def retrieve_domains(self): status, output = self.request("GET", 'domain-list') 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