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 let path = ApiPath::ocs("cloud/capabilities")?;
43 self.ocs
44 .get(path, Query::new().format(OcsFormat::Json))
45 .await
46 }
47
48 /// Fetches the profile and storage quota of the currently authenticated user.
49 ///
50 /// Sends a `GET` request to `/ocs/v2.php/cloud/user?format=json`
51 /// (the v2 equivalent of `/ocs/v1.php/cloud/user`).
52 ///
53 /// See <https://docs.nextcloud.com/server/latest/developer_manual/client_apis/LoginFlow/index.html>.
54 pub async fn current_user(&self) -> Result<User, Error> {
55 let path = ApiPath::ocs("cloud/user")?;
56 self.ocs
57 .get(path, Query::new().format(OcsFormat::Json))
58 .await
59 }
60
61 /// Fetches the profile and storage quota of a user by their user identifier.
62 ///
63 /// Sends a `GET` request to `/ocs/v2.php/cloud/users/{userid}?format=json`
64 /// (the v2 equivalent of `/ocs/v1.php/cloud/users/{userid}`).
65 ///
66 /// See <https://docs.nextcloud.com/server/latest/developer_manual/client_apis/OCS/ocs-api-overview.html#user-metadata>.
67 pub async fn get_user(&self, user_id: &str) -> Result<User, Error> {
68 let path = ApiPath::ocs(format!("cloud/users/{user_id}"))?;
69 self.ocs
70 .get(path, Query::new().format(OcsFormat::Json))
71 .await
72 }
73}
74
75impl<T: Transport, A: AuthProvider> std::fmt::Display for UserClient<'_, T, A> {
76 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
77 f.write_str("UserClient")
78 }
79}