Skip to content

Commit eeb9508

Browse files
authored
Merge pull request #339 from ynput/enhancement/as-username-function
Public api: Added 'as_username' to public api
2 parents e52b427 + cac730b commit eeb9508

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

ayon_api/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
set_environments,
3232
get_server_api_connection,
3333
get_default_settings_variant,
34+
as_username,
3435
get_base_url,
3536
get_rest_url,
3637
get_ssl_verify,
@@ -349,6 +350,7 @@
349350
"set_environments",
350351
"get_server_api_connection",
351352
"get_default_settings_variant",
353+
"as_username",
352354
"get_base_url",
353355
"get_rest_url",
354356
"get_ssl_verify",

ayon_api/_api.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111

1212
from __future__ import annotations
1313

14+
from contextlib import contextmanager
1415
import os
1516
import socket
1617
import typing
17-
from typing import Optional, Iterable, Generator, Any
18+
from typing import Optional, Iterable, Generator, Any, ContextManager
1819

1920
import requests
2021

@@ -411,6 +412,32 @@ def get_default_settings_variant():
411412
return con.get_default_settings_variant()
412413

413414

415+
@contextmanager
416+
def as_username(
417+
username: str | None,
418+
ignore_service_error: bool = False,
419+
) -> ContextManager[None]:
420+
"""Service API will temporarily work as other user.
421+
422+
This method can be used only if service API key is logged in.
423+
424+
Args:
425+
username (str | None): Username to work as when service.
426+
ignore_service_error (bool): Ignore error when service
427+
API key is not used.
428+
429+
Raises:
430+
ValueError: When connection is not yet authenticated or api key
431+
is not service token.
432+
433+
"""
434+
con = get_server_api_connection()
435+
with con.as_username(
436+
username, ignore_service_error=ignore_service_error
437+
):
438+
yield
439+
440+
414441
# ------------------------------------------------
415442
# This content is generated automatically.
416443
# ------------------------------------------------

0 commit comments

Comments
 (0)