Skip to main content

openapp_sdk_core/resources/
users.rs

1//! `Users` resource group.
2
3use std::sync::Arc;
4
5use reqwest::Method;
6
7use super::JsonValue;
8use crate::{
9    error::SdkError,
10    transport::{RequestSpec, Transport},
11};
12
13#[derive(Debug, Clone)]
14pub struct UsersClient {
15    transport: Arc<Transport>,
16}
17
18impl UsersClient {
19    pub(crate) fn new(transport: Arc<Transport>) -> Self {
20        Self { transport }
21    }
22
23    /// `POST /users` — create a user (org-admin required).
24    pub async fn create(&self, body: &JsonValue) -> Result<JsonValue, SdkError> {
25        self.transport
26            .request_json::<JsonValue, JsonValue>(RequestSpec {
27                method: Method::POST,
28                path: "/users",
29                body: Some(body),
30                ..Default::default()
31            })
32            .await
33    }
34
35    /// `GET /users/search?q=…` — fuzzy search by email / name.
36    pub async fn search(&self, query: &str) -> Result<Vec<JsonValue>, SdkError> {
37        let params = [("q", Some(query.to_string()))];
38        self.transport
39            .request_json::<(), Vec<JsonValue>>(RequestSpec {
40                method: Method::GET,
41                path: "/users/search",
42                query: &params,
43                ..Default::default()
44            })
45            .await
46    }
47
48    /// `GET /users/{id}`
49    pub async fn get(&self, id: &str) -> Result<JsonValue, SdkError> {
50        let path = format!("/users/{id}");
51        self.transport
52            .request_json::<(), JsonValue>(RequestSpec {
53                method: Method::GET,
54                path: &path,
55                ..Default::default()
56            })
57            .await
58    }
59
60    /// `PUT /users/{id}`
61    pub async fn update(&self, id: &str, body: &JsonValue) -> Result<JsonValue, SdkError> {
62        let path = format!("/users/{id}");
63        self.transport
64            .request_json::<JsonValue, JsonValue>(RequestSpec {
65                method: Method::PUT,
66                path: &path,
67                body: Some(body),
68                ..Default::default()
69            })
70            .await
71    }
72
73    /// `DELETE /users/{id}` — soft-delete.
74    pub async fn delete(&self, id: &str) -> Result<(), SdkError> {
75        let path = format!("/users/{id}");
76        self.transport
77            .request_json::<(), ()>(RequestSpec {
78                method: Method::DELETE,
79                path: &path,
80                ..Default::default()
81            })
82            .await
83    }
84
85    /// `DELETE /users/{id}/purge` — hard-delete.
86    pub async fn purge(&self, id: &str) -> Result<(), SdkError> {
87        let path = format!("/users/{id}/purge");
88        self.transport
89            .request_json::<(), ()>(RequestSpec {
90                method: Method::DELETE,
91                path: &path,
92                ..Default::default()
93            })
94            .await
95    }
96
97    /// `POST /users/{id}/roles` — add roles to a user.
98    pub async fn add_roles(&self, id: &str, body: &JsonValue) -> Result<JsonValue, SdkError> {
99        let path = format!("/users/{id}/roles");
100        self.transport
101            .request_json::<JsonValue, JsonValue>(RequestSpec {
102                method: Method::POST,
103                path: &path,
104                body: Some(body),
105                ..Default::default()
106            })
107            .await
108    }
109
110    /// `DELETE /users/{id}/roles` — remove roles from a user.
111    pub async fn remove_roles(&self, id: &str, body: &JsonValue) -> Result<JsonValue, SdkError> {
112        let path = format!("/users/{id}/roles");
113        self.transport
114            .request_json::<JsonValue, JsonValue>(RequestSpec {
115                method: Method::DELETE,
116                path: &path,
117                body: Some(body),
118                ..Default::default()
119            })
120            .await
121    }
122}