1use serde::{
15 Deserialize,
16 Serialize,
17};
18
19use crate::{
20 CoralogixRegion,
21 RUSTC_VERSION,
22 SDK_CORRELATION_ID_HEADER_NAME,
23 SDK_LANGUAGE_HEADER_NAME,
24 SDK_RUSTC_VERSION_HEADER_NAME,
25 SDK_VERSION,
26 SDK_VERSION_HEADER_NAME,
27 auth::{
28 ApiKey,
29 AuthContext,
30 },
31};
32
33#[derive(Serialize, Deserialize)]
34#[serde(rename_all = "camelCase")]
35pub struct ScimUser {
37 pub schemas: Vec<String>,
39 pub id: Option<String>,
41 pub user_name: String,
43 pub active: bool,
45 pub name: ScimUserName,
47 pub emails: Vec<ScimUserEmail>,
49 pub groups: Vec<ScimUserGroup>,
51}
52
53#[derive(Serialize, Deserialize)]
54#[serde(rename_all = "camelCase")]
55pub struct ScimUserName {
57 pub given_name: String,
59 pub family_name: String,
61}
62
63#[derive(Serialize, Deserialize)]
64#[serde(rename_all = "camelCase")]
65pub struct ScimUserEmail {
67 pub value: String,
69 pub r#type: Option<String>,
71 pub primary: bool,
73}
74
75#[derive(Serialize, Deserialize)]
76#[serde(rename_all = "camelCase")]
77pub struct ScimUserGroup {
79 pub value: String,
81}
82
83#[derive(Serialize, Deserialize)]
85#[serde(rename_all = "camelCase")]
86pub struct ListResponse {
87 pub schemas: Vec<String>,
89 pub total_results: usize,
91 #[serde(rename = "Resources")]
93 pub resources: Vec<ScimUser>,
94}
95
96pub struct UsersClient {
98 target_url: String,
99 http_client: reqwest::Client,
100 api_key: ApiKey,
101 correlation_id: String,
102}
103
104impl UsersClient {
105 pub fn new(region: CoralogixRegion, auth_context: AuthContext) -> Self {
111 Self {
112 http_client: reqwest::Client::new(),
113 api_key: auth_context.team_level_api_key,
114 target_url: region.rest_endpoint() + "/scim/Users",
115 correlation_id: uuid::Uuid::new_v4().to_string(),
116 }
117 }
118
119 pub async fn create(&self, user: ScimUser) -> Result<ScimUser, reqwest::Error> {
121 let response = self
122 .http_client
123 .post(&self.target_url)
124 .header("Authorization", format!("Bearer {}", self.api_key.token()))
125 .header(SDK_VERSION_HEADER_NAME, SDK_VERSION)
126 .header(SDK_LANGUAGE_HEADER_NAME, "rust")
127 .header(SDK_RUSTC_VERSION_HEADER_NAME, RUSTC_VERSION)
128 .header(SDK_CORRELATION_ID_HEADER_NAME, &self.correlation_id)
129 .json(&user)
130 .send()
131 .await?;
132 response.json().await
133 }
134
135 pub async fn get(&self, id: &str) -> Result<ScimUser, reqwest::Error> {
140 let url = format!("{}/{}", self.target_url, id);
141 let response = self
142 .http_client
143 .get(&url)
144 .header("Authorization", format!("Bearer {}", self.api_key.token()))
145 .header(SDK_VERSION_HEADER_NAME, SDK_VERSION)
146 .header(SDK_LANGUAGE_HEADER_NAME, "rust")
147 .header(SDK_RUSTC_VERSION_HEADER_NAME, RUSTC_VERSION)
148 .header(SDK_CORRELATION_ID_HEADER_NAME, &self.correlation_id)
149 .send()
150 .await?;
151 response.json().await
152 }
153
154 pub async fn update(&self, user: ScimUser) -> Result<ScimUser, reqwest::Error> {
159 let url = format!("{}/{}", self.target_url, user.id.as_ref().unwrap());
160 let response = self
161 .http_client
162 .put(&url)
163 .header("Authorization", format!("Bearer {}", self.api_key.token()))
164 .header(SDK_VERSION_HEADER_NAME, SDK_VERSION)
165 .header(SDK_LANGUAGE_HEADER_NAME, "rust")
166 .header(SDK_RUSTC_VERSION_HEADER_NAME, RUSTC_VERSION)
167 .header(SDK_CORRELATION_ID_HEADER_NAME, &self.correlation_id)
168 .json(&user)
169 .send()
170 .await?;
171 response.json().await
172 }
173
174 pub async fn delete(&self, id: &str) -> Result<(), reqwest::Error> {
179 let url = format!("{}/{}", self.target_url, id);
180 let response = self
181 .http_client
182 .delete(&url)
183 .header("Authorization", format!("Bearer {}", self.api_key.token()))
184 .header(SDK_VERSION_HEADER_NAME, SDK_VERSION)
185 .header(SDK_LANGUAGE_HEADER_NAME, "rust")
186 .header(SDK_RUSTC_VERSION_HEADER_NAME, RUSTC_VERSION)
187 .header(SDK_CORRELATION_ID_HEADER_NAME, &self.correlation_id)
188 .send()
189 .await?;
190 response.error_for_status()?;
191 Ok(())
192 }
193
194 pub async fn list(&self) -> Result<Vec<ScimUser>, reqwest::Error> {
198 let response = self
199 .http_client
200 .get(&self.target_url)
201 .header("Authorization", format!("Bearer {}", self.api_key.token()))
202 .header(SDK_VERSION_HEADER_NAME, SDK_VERSION)
203 .header(SDK_LANGUAGE_HEADER_NAME, "rust")
204 .header(SDK_RUSTC_VERSION_HEADER_NAME, RUSTC_VERSION)
205 .header(SDK_CORRELATION_ID_HEADER_NAME, &self.correlation_id)
206 .send()
207 .await?;
208
209 let list_response: ListResponse = response.json().await?;
210 Ok(list_response.resources)
211 }
212}