add api call for validate with token

This commit is contained in:
Cayo Puigdefabregas 2023-09-27 13:59:30 +02:00
parent 8d3033c0ca
commit df9c0c2361
1 changed files with 15 additions and 2 deletions

View File

@ -14,6 +14,7 @@ TOKEN_PATH = '/api-token-auth/'
API_PATHS = { API_PATHS = {
# auth # auth
'token-auth': '/api-token-auth/', 'token-auth': '/api-token-auth/',
'token-auth-v2': '/api-token-auth-v2/',
'my-account': 'accounts/', 'my-account': 'accounts/',
# services # services
@ -37,14 +38,17 @@ API_PATHS = {
class Orchestra(object): 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.base_url = kwargs.pop('base_url', settings.API_BASE_URL)
self.username = username self.username = username
self.session = requests.Session() self.session = requests.Session()
self.auth_token = kwargs.pop("auth_token", None) self.auth_token = kwargs.pop("auth_token", None)
if self.auth_token is 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): def build_absolute_uri(self, path_name):
path = API_PATHS.get(path_name, None) path = API_PATHS.get(path_name, None)
@ -63,6 +67,15 @@ class Orchestra(object):
return response.json().get("token", None) 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): 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"] assert verb in ["HEAD", "GET", "POST", "PATCH", "PUT", "DELETE"]
if resource is not None: if resource is not None: