From df9c0c23619ae3739fca7e8f8305703d588b6195 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Wed, 27 Sep 2023 13:59:30 +0200 Subject: [PATCH] add api call for validate with token --- musician/api.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/musician/api.py b/musician/api.py index 6b60c0c..4c7f70e 100644 --- a/musician/api.py +++ b/musician/api.py @@ -14,6 +14,7 @@ TOKEN_PATH = '/api-token-auth/' API_PATHS = { # auth 'token-auth': '/api-token-auth/', + 'token-auth-v2': '/api-token-auth-v2/', 'my-account': 'accounts/', # services @@ -37,14 +38,17 @@ API_PATHS = { class Orchestra(object): - def __init__(self, *args, username=None, password=None, **kwargs): + def __init__(self, *args, username=None, password=None, token=None, **kwargs): self.base_url = kwargs.pop('base_url', settings.API_BASE_URL) self.username = username self.session = requests.Session() self.auth_token = kwargs.pop("auth_token", None) if self.auth_token is None: - self.auth_token = self.authenticate(self.username, password) + if token: + self.auth_token = self.authenticate_token(token) + else: + self.auth_token = self.authenticate(self.username, password) def build_absolute_uri(self, path_name): path = API_PATHS.get(path_name, None) @@ -63,6 +67,15 @@ class Orchestra(object): return response.json().get("token", None) + def authenticate_token(self, token): + url = self.build_absolute_uri('token-auth-v2') + response = self.session.post( + url, + data={"token": token}, + ) + + return response.json().get("token", None) + def request(self, verb, resource=None, url=None, data=None, render_as="json", querystring=None, raise_exception=True): assert verb in ["HEAD", "GET", "POST", "PATCH", "PUT", "DELETE"] if resource is not None: