nextcloud_users/client/user_client.rs
1use nextcloud_ocs::{OcsClient, OcsFormat, Query};
2use nextcloud_sdk_core::{ApiPath, AuthProvider, Error, Transport};
3
4use crate::capabilities::Capabilities;
5use crate::user::User;
6
7/// Client for Nextcloud user provisioning and server capabilities.
8///
9/// Wraps an [`OcsClient`] by reference and provides high-level typed calls for:
10/// - Server capabilities discovery (`/ocs/v1.php/cloud/capabilities` or `/ocs/v2.php/cloud/capabilities`)
11/// - Current authenticated user metadata (`/ocs/v1.php/cloud/user` or `/ocs/v2.php/cloud/user`)
12/// - User profile lookup by user ID (`/ocs/v1.php/cloud/users/{userid}` or `/ocs/v2.php/cloud/users/{userid}`)
13///
14/// Official documentation:
15/// - Nextcloud OCS Overview: <https://docs.nextcloud.com/server/latest/developer_manual/client_apis/OCS/ocs-api-overview.html>
16/// - User Metadata: <https://docs.nextcloud.com/server/latest/developer_manual/client_apis/OCS/ocs-api-overview.html#user-metadata>
17/// - Capabilities API: <https://docs.nextcloud.com/server/latest/developer_manual/client_apis/OCS/ocs-api-overview.html#capabilities-api>
18/// - User Provisioning API: <https://docs.nextcloud.com/server/latest/admin_manual/configuration_user/user_provisioning_api.html>
19#[derive(Clone, Copy, Debug)]
20pub struct UserClient<'a, T: Transport, A: AuthProvider> {
21 ocs: &'a OcsClient<T, A>,
22}
23
24impl<'a, T: Transport, A: AuthProvider> UserClient<'a, T, A> {
25 /// Binds a user client to an underlying [`OcsClient`].
26 pub const fn new(ocs: &'a OcsClient<T, A>) -> Self {
27 Self { ocs }
28 }
29
30 /// Borrows the underlying [`OcsClient`].
31 pub const fn ocs(&self) -> &'a OcsClient<T, A> {
32 self.ocs
33 }
34
35 /// Fetches server capabilities.
36 ///
37 /// Sends a `GET` request to `/ocs/v2.php/cloud/capabilities?format=json`
38 /// (the v2 equivalent of `/ocs/v1.php/cloud/capabilities`).
39 ///
40 /// See <https://docs.nextcloud.com/server/latest/developer_manual/client_apis/OCS/ocs-api-overview.html#capabilities-api>.
41 pub async fn capabilities(&self) -> Result<Capabilities, Error> {
42 tracing::debug!("Fetching Nextcloud server capabilities");
43 let path = ApiPath::ocs("cloud/capabilities")?;
44 self.ocs
45 .get(path, Query::new().format(OcsFormat::Json))
46 .await
47 }
48
49 /// Fetches the profile and storage quota of the currently authenticated user.
50 ///
51 /// Sends a `GET` request to `/ocs/v2.php/cloud/user?format=json`
52 /// (the v2 equivalent of `/ocs/v1.php/cloud/user`).
53 ///
54 /// See <https://docs.nextcloud.com/server/latest/developer_manual/client_apis/LoginFlow/index.html>.
55 pub async fn current_user(&self) -> Result<User, Error> {
56 tracing::debug!("Fetching current authenticated user metadata");
57 let path = ApiPath::ocs("cloud/user")?;
58 self.ocs
59 .get(path, Query::new().format(OcsFormat::Json))
60 .await
61 }
62
63 /// Fetches the profile and storage quota of a user by their user identifier.
64 ///
65 /// Sends a `GET` request to `/ocs/v2.php/cloud/users/{userid}?format=json`
66 /// (the v2 equivalent of `/ocs/v1.php/cloud/users/{userid}`).
67 ///
68 /// See <https://docs.nextcloud.com/server/latest/developer_manual/client_apis/OCS/ocs-api-overview.html#user-metadata>.
69 pub async fn get_user(&self, user_id: &str) -> Result<User, Error> {
70 tracing::debug!(user_id, "Fetching user profile metadata");
71 let path = ApiPath::ocs(format!("cloud/users/{user_id}"))?;
72 self.ocs
73 .get(path, Query::new().format(OcsFormat::Json))
74 .await
75 }
76}
77
78impl<T: Transport, A: AuthProvider> std::fmt::Display for UserClient<'_, T, A> {
79 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
80 f.write_str("UserClient")
81 }
82}