1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//! User API endpoints.
use crate::types::{FavoriteItem, User};
use crate::{Client, Error};
/// API for user operations.
pub struct UsersApi<'a> {
client: &'a Client,
}
impl<'a> UsersApi<'a> {
/// Create a new users API instance.
pub fn new(client: &'a Client) -> Self {
Self { client }
}
/// Get the currently authenticated user.
///
/// # Example
///
/// ```rust,no_run
/// # use asanaclient::Client;
/// # async fn example() -> Result<(), asanaclient::Error> {
/// let client = Client::from_env()?;
/// let me = client.users().me().await?;
/// println!("Logged in as: {}", me.name);
/// # Ok(())
/// # }
/// ```
pub async fn me(&self) -> Result<User, Error> {
self.client.get("/users/me", &[]).await
}
/// Get the favorites for the current user in a workspace.
///
/// Returns all favorited items (projects, portfolios, etc.) for the
/// authenticated user in the specified workspace.
///
/// # Example
///
/// ```rust,no_run
/// # use asanaclient::Client;
/// # async fn example() -> Result<(), asanaclient::Error> {
/// let client = Client::from_env()?;
/// let favorites = client.users().favorites("12345").await?;
/// for item in favorites {
/// println!("{}: {} ({})", item.gid, item.name.unwrap_or_default(), item.resource_type);
/// }
/// # Ok(())
/// # }
/// ```
pub async fn favorites(&self, workspace_gid: &str) -> Result<Vec<FavoriteItem>, Error> {
let (projects, portfolios) = tokio::try_join!(
self.favorite_projects(workspace_gid),
self.favorite_portfolios(workspace_gid)
)?;
let mut all = projects;
all.extend(portfolios);
Ok(all)
}
/// Get only favorited projects for the current user in a workspace.
pub async fn favorite_projects(&self, workspace_gid: &str) -> Result<Vec<FavoriteItem>, Error> {
let path = "/users/me/favorites";
let query = [("workspace", workspace_gid), ("resource_type", "project")];
self.client.get_all(path, &query).await
}
/// Get only favorited portfolios for the current user in a workspace.
pub async fn favorite_portfolios(
&self,
workspace_gid: &str,
) -> Result<Vec<FavoriteItem>, Error> {
let path = "/users/me/favorites";
let query = [("workspace", workspace_gid), ("resource_type", "portfolio")];
self.client.get_all(path, &query).await
}
}
impl Client {
/// Access the users API.
pub fn users(&self) -> UsersApi<'_> {
UsersApi::new(self)
}
}