openapp_sdk_core/resources/
users.rs1use 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 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 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: ¶ms,
43 ..Default::default()
44 })
45 .await
46 }
47
48 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 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 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 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 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 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}