imaging_api.services.users

Attributes

XNAT_URL

Functions

get_xnat_users(→ list[imaging_api.routers.schemas.User])

Gets all users from XNAT.

to_create_imaging_user(...)

Converts central hub user (mainly email) to an XNAT CreateUser object.

get_user_profile_by(→ imaging_api.routers.schemas.User)

Fetches a user profile from XNAT using the key and value.

user_exists(→ bool)

Checks if a user exists in XNAT.

create_user_from_central_hub_user(...)

Convert central hub user to XNAT CreateUser request object, and create user on XNAT.

create_user(→ imaging_api.routers.schemas.User)

Core logic to create an XNAT user. Returns the newly created user profile.

add_user_to_project(→ imaging_api.routers.schemas.User)

Adds a user to a project in XNAT.

Module Contents

imaging_api.services.users.XNAT_URL = 'http://xnat-web:8080'
imaging_api.services.users.get_xnat_users(headers: dict[str, str]) list[imaging_api.routers.schemas.User]

Gets all users from XNAT.

Parameters:

headers (dict[str, str]) – XNAT authentication headers.

Returns:

List of XNAT users.

Return type:

list[imaging_api.routers.schemas.User]

Raises:

Exception – If XNAT returns a non-200 response.

imaging_api.services.users.to_create_imaging_user(user: imaging_api.routers.schemas.CentralHubUser, headers: dict[str, str]) imaging_api.routers.schemas.CreateUser

Converts central hub user (mainly email) to an XNAT CreateUser object.

XNAT usernames are unique, so we have to create a unique XNAT username from the email address. We first grab the part of the email before ‘@’. If the username already exists, we append a number suffix to make it unique.

Parameters:
Returns:

XNAT user creation request object.

Return type:

imaging_api.routers.schemas.CreateUser

Raises:

Exception – If fetching existing XNAT users fails.

imaging_api.services.users.get_user_profile_by(key: str, value: str, headers: dict[str, str]) imaging_api.routers.schemas.User

Fetches a user profile from XNAT using the key and value.

Parameters:
  • key (str) – The key to search by. Can be ‘username’ or ‘email’.

  • value (str) – The value to search for.

  • headers (dict[str, str]) – XNAT authentication headers.

Returns:

The user profile.

Return type:

imaging_api.routers.schemas.User

Raises:
imaging_api.services.users.user_exists(username: str, headers: dict[str, str]) bool

Checks if a user exists in XNAT.

In XNAT, username is unique, so it can be used to identify the user: “ERROR: The username {username} is already in use. Please enter a different username value and try again.”

Parameters:
  • username (str) – The username to check.

  • headers (dict[str, str]) – XNAT authentication headers.

Returns:

True if the user exists, False otherwise.

Return type:

bool

imaging_api.services.users.create_user_from_central_hub_user(central_hub_user: imaging_api.routers.schemas.CentralHubUser, headers: dict[str, str]) tuple[imaging_api.routers.schemas.CreatedUser, imaging_api.routers.schemas.User]

Convert central hub user to XNAT CreateUser request object, and create user on XNAT.

Parameters:
Returns:

The created user and the user profile.

Return type:

tuple[imaging_api.routers.schemas.CreatedUser, imaging_api.routers.schemas.User]

imaging_api.services.users.create_user(user: imaging_api.routers.schemas.CreateUser, headers: dict[str, str]) imaging_api.routers.schemas.User

Core logic to create an XNAT user. Returns the newly created user profile.

Parameters:
Returns:

The created user profile.

Return type:

imaging_api.routers.schemas.User

Raises:
  • AlreadyExistsError – If a user with the same username already exists on XNAT (HTTP 409).

  • Exception – If XNAT returns any other non-201 response.

imaging_api.services.users.add_user_to_project(user: imaging_api.routers.schemas.User, project_id: str, headers: dict[str, str]) imaging_api.routers.schemas.User

Adds a user to a project in XNAT.

Parameters:
Returns:

The user profile.

Return type:

imaging_api.routers.schemas.User

Raises: