tftio-asana-cli 3.1.0

An interface to the Asana API
Documentation
//! High level user operations built on the core API client.

use crate::api::api_get;
use crate::{
    api::{ApiClient, ApiError},
    models::{User, UserListParams},
};
use futures_util::{StreamExt, pin_mut};

/// List users in a workspace.
///
/// # Errors
/// Returns [`ApiError`] if the API request fails or network errors occur.
pub async fn list_users(client: &ApiClient, params: UserListParams) -> Result<Vec<User>, ApiError> {
    let endpoint = format!("/workspaces/{}/users", params.workspace_gid);
    let stream = client.paginate_with_limit::<User>(&endpoint, vec![], params.limit);
    pin_mut!(stream);

    let mut users = Vec::new();
    while let Some(page) = stream.next().await {
        let mut page = page?;
        users.append(&mut page);
    }

    Ok(users)
}

/// Get a single user by GID.
///
/// # Errors
/// Returns [`ApiError`] if the API request fails or network errors occur.
pub async fn get_user(client: &ApiClient, gid: &str) -> Result<User, ApiError> {
    api_get(client, &format!("/users/{gid}")).await
}

/// Get the current authenticated user.
///
/// # Errors
/// Returns [`ApiError`] if the API request fails or network errors occur.
pub async fn get_current_user(client: &ApiClient) -> Result<User, ApiError> {
    api_get(client, "/users/me").await
}