Skip to content
Merged
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
2 changes: 2 additions & 0 deletions ayon_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
set_environments,
get_server_api_connection,
get_default_settings_variant,
as_username,
get_base_url,
get_rest_url,
get_ssl_verify,
Expand Down Expand Up @@ -349,6 +350,7 @@
"set_environments",
"get_server_api_connection",
"get_default_settings_variant",
"as_username",
"get_base_url",
"get_rest_url",
"get_ssl_verify",
Expand Down
29 changes: 28 additions & 1 deletion ayon_api/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@

from __future__ import annotations

from contextlib import contextmanager
import os
import socket
import typing
from typing import Optional, Iterable, Generator, Any
from typing import Optional, Iterable, Generator, Any, ContextManager

import requests

Expand Down Expand Up @@ -411,6 +412,32 @@ def get_default_settings_variant():
return con.get_default_settings_variant()


@contextmanager
def as_username(
username: str | None,
ignore_service_error: bool = False,
) -> ContextManager[None]:
"""Service API will temporarily work as other user.

This method can be used only if service API key is logged in.

Args:
username (str | None): Username to work as when service.
ignore_service_error (bool): Ignore error when service
API key is not used.

Raises:
ValueError: When connection is not yet authenticated or api key
is not service token.

"""
con = get_server_api_connection()
with con.as_username(
username, ignore_service_error=ignore_service_error
):
yield


# ------------------------------------------------
# This content is generated automatically.
# ------------------------------------------------
Expand Down
Loading