From cac730b5dd5a97b61d06656f77ec4280f2cd4dda Mon Sep 17 00:00:00 2001 From: Jakub Trllo <43494761+iLLiCiTiT@users.noreply.github.com> Date: Mon, 22 Jun 2026 11:18:43 +0200 Subject: [PATCH] added as_username as public function --- ayon_api/__init__.py | 2 ++ ayon_api/_api.py | 29 ++++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/ayon_api/__init__.py b/ayon_api/__init__.py index 3d5f89311..2f04c2800 100644 --- a/ayon_api/__init__.py +++ b/ayon_api/__init__.py @@ -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, @@ -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", diff --git a/ayon_api/_api.py b/ayon_api/_api.py index 728ce0e59..66ebccec9 100644 --- a/ayon_api/_api.py +++ b/ayon_api/_api.py @@ -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 @@ -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. # ------------------------------------------------