Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[api_tokens] add new functions for API tokens #304

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions gazu/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
except ImportError:
pass

from . import api_token
from . import asset
from . import casting
from . import context
Expand Down Expand Up @@ -110,3 +111,18 @@ def get_event_host(client=raw.default_client):

def set_event_host(url, client=raw.default_client):
raw.set_event_host(url, client=client)


def set_token(token, client=raw.default_client):
"""
Store authentication token to reuse them for all requests.

Args:
new_tokens (dict): Tokens to use for authentication.
"""
tokens = {}
if isinstance(token, dict):
tokens["access_token"] = token["access_token"]
else:
tokens["access_token"] = token
return raw.set_tokens(tokens, client=client)
131 changes: 131 additions & 0 deletions gazu/api_token.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
from . import client as raw

from .sorting import sort_by_name
from .helpers import (
normalize_list_of_models_for_links,
)
from .cache import cache

default = raw.default_client


@cache
def all_api_tokens(client=default):
"""
Returns:
list: API tokens listed in database.
"""
return sort_by_name(raw.fetch_all("api-tokens", client=client))


def new_api_token(
name,
email,
role="user",
departments=[],
description=None,
days_duration=None,
client=default,
):
"""
Create a new API token based on given parameters.
The jwt token will be in the dict returned at the key "access_token".

Args:
name (str): The name of the API token.
email (str): The email of the API token.
role (str): user, manager, admin (wich match CG artist, Supervisor
and studio manager)
departments (list): The departments for the person.
Returns:
dict: Created API token.
"""
data = {
"name": name,
"email": email,
"role": role,
"departments": normalize_list_of_models_for_links(departments),
}
if description is not None:
data["description"] = description
if days_duration is not None:
data["days_duration"] = days_duration
api_token = raw.post(
"data/api-tokens",
data,
client=client,
)
return api_token


def update_api_token(api_token, client=default):
"""
Update an API token.

Args:
api_token (dict): The API dict dict that needs to be upgraded.

Returns:
dict: The updated API token.
"""

if "departments" in api_token:
api_token["departments"] = normalize_list_of_models_for_links(
api_token["departments"]
)

return raw.put(
"data/api-tokens/%s" % (api_token["id"]),
api_token,
client=client,
)


@cache
def get_api_token(api_token_id, relations=False, client=default):
"""
Args:
api_token_id (str): An uuid identifying an API token.
relations (bool): Whether to get the relations for the given API token.

Returns:
dict: API token corresponding to given id.
"""
params = {"id": api_token_id}
if relations:
params["relations"] = "true"

return raw.fetch_first("api-tokens", params=params, client=client)


def remove_api_token(api_token, force=False, client=default):
"""
Remove given API token from database.

Args:
api_token (dict): API token to remove.
"""
path = "data/api-tokens/%s" % api_token["id"]
params = {}
if force:
params = {"force": "true"}
return raw.delete(path, params, client=client)


@cache
def get_api_token_by_name(name, client=default):
"""
Args:
name (str): The API token name.

Returns:
dict: API token matching given name.
"""

return raw.fetch_first(
"api-tokens",
{
"name": name,
},
client=client,
)