Get Username from User ID in a Python Functions Repository

Hello! I’m hoping to get some help with a problem that I can’t seem to solve using Python functions.

Context

I’m building a page in Workshop to manage user permissions, which are stored in a Ontology object. One of the requirements for the “create permission” form is to have a dropdown to select a user. This is easily solved with the standard User Selector Widget.

The Problem

The User Selector widget outputs the selected user’s User ID (e.g., 123e4567-e89b-12d3-a456-426614174000). However, for a better user experience, I need the user’s Username.

What I’ve Tried

My first thought was to create a simple Python function that takes the User ID and returns the username using the FoundryClient:

from ontology_sdk import FoundryClient
from functions.api import function

@function()
def get_username(user_id: str) -> str:
    client = FoundryClient()
    # Attempt to get the user object by their ID
    user = client.users.get_user(user_id)
    return user.username

However, when I run this, I get the following error, which suggests that the users attribute is not part of the FoundryClient available in this context.

AttributeError: ‘FoundryClient’ object has no attribute ‘users’

The Question

I’ve seen multiple topics on the forums that solve this using Typescript functions, but I am required to use a Python Functions Repository for this project.

Is there a standard or simple way to get a user’s details (like their username) from a given User ID inside a Python function?

You can try the foundry-platform-sdk.
There the admin module has a function to get the user. I am not sure how it works though considering that this function does not work in the Ontology but in the code repository that I am working in…

from ontology_sdk import FoundryClient
import foundry_sdk

@function()
def get_current_user_id() -> str:
    ontology_sdk_client = FoundryClient()
    token = ontology_sdk_client.auth.get_token().access_token
    auth = foundry_sdk.UserTokenAuth(token)
    client = foundry_sdk.FoundryClient(auth=auth, hostname="<domain>")
    user = client.admin.User.get_current()

    return user.id

If I try this, the error message is:
raise ConnectionError(str(e)) from e foundry_sdk._errors.connection_error.ConnectionError: [Errno -2] Name or service not known

Any idea regarding this? I was already quite happy that I could get the user without putting it in as an argument of the function.

It worked for me without errors with the small modification shown below to test my use case.

import foundry_sdk
from ontology_sdk import FoundryClient

@function()
def get_current_user_id() -> str:
    ontology_sdk_client = FoundryClient()
    token = ontology_sdk_client.auth.get_token().access_token
    auth = foundry_sdk.UserTokenAuth(token)
    client = foundry_sdk.FoundryClient(auth=auth, hostname="<YOUR-DOMAIN>")
    my_id = client.admin.User.get_current().id  #modifed
    user = client.admin.User.get(my_id)  #modifed

    return user.username  #modifed

1 Like