nile_client_rs/apis/
users_api.rs

1/*
2 * Nile API
3 *
4 * Making SaaS chill.
5 *
6 * The version of the OpenAPI document: 0.1.0-fdd7cd5
7 * Contact: support@thenile.dev
8 * Generated by: https://openapi-generator.tech
9 */
10
11use reqwest;
12
13use super::{configuration, Error};
14use crate::apis::ResponseContent;
15use serde::{Deserialize, Serialize};
16
17/// struct for typed errors of method [`create_developer_owned_user`]
18#[derive(Debug, Clone, Serialize, Deserialize)]
19#[serde(untagged)]
20pub enum CreateDeveloperOwnedUserError {
21    UnknownValue(serde_json::Value),
22}
23
24/// struct for typed errors of method [`create_user`]
25#[derive(Debug, Clone, Serialize, Deserialize)]
26#[serde(untagged)]
27pub enum CreateUserError {
28    UnknownValue(serde_json::Value),
29}
30
31/// struct for typed errors of method [`delete_user`]
32#[derive(Debug, Clone, Serialize, Deserialize)]
33#[serde(untagged)]
34pub enum DeleteUserError {
35    UnknownValue(serde_json::Value),
36}
37
38/// struct for typed errors of method [`get_user`]
39#[derive(Debug, Clone, Serialize, Deserialize)]
40#[serde(untagged)]
41pub enum GetUserError {
42    UnknownValue(serde_json::Value),
43}
44
45/// struct for typed errors of method [`list_users`]
46#[derive(Debug, Clone, Serialize, Deserialize)]
47#[serde(untagged)]
48pub enum ListUsersError {
49    UnknownValue(serde_json::Value),
50}
51
52/// struct for typed errors of method [`login_user`]
53#[derive(Debug, Clone, Serialize, Deserialize)]
54#[serde(untagged)]
55pub enum LoginUserError {
56    Status401(crate::models::Error),
57    UnknownValue(serde_json::Value),
58}
59
60/// struct for typed errors of method [`me`]
61#[derive(Debug, Clone, Serialize, Deserialize)]
62#[serde(untagged)]
63pub enum MeError {
64    UnknownValue(serde_json::Value),
65}
66
67/// struct for typed errors of method [`token`]
68#[derive(Debug, Clone, Serialize, Deserialize)]
69#[serde(untagged)]
70pub enum TokenError {
71    Status401(crate::models::Error),
72    UnknownValue(serde_json::Value),
73}
74
75/// struct for typed errors of method [`update_user`]
76#[derive(Debug, Clone, Serialize, Deserialize)]
77#[serde(untagged)]
78pub enum UpdateUserError {
79    UnknownValue(serde_json::Value),
80}
81
82/// struct for typed errors of method [`validate_user`]
83#[derive(Debug, Clone, Serialize, Deserialize)]
84#[serde(untagged)]
85pub enum ValidateUserError {
86    Status400(crate::models::Error),
87    UnknownValue(serde_json::Value),
88}
89
90pub async fn create_developer_owned_user(
91    configuration: &configuration::Configuration,
92    workspace: &str,
93    create_developer_owned_user_request: crate::models::CreateDeveloperOwnedUserRequest,
94) -> Result<crate::models::User, Error<CreateDeveloperOwnedUserError>> {
95    let local_var_configuration = configuration;
96
97    let local_var_client = &local_var_configuration.client;
98
99    let local_var_uri_str = format!(
100        "{}/workspaces/{workspace}/internal/users",
101        local_var_configuration.base_path,
102        workspace = crate::apis::urlencode(workspace)
103    );
104    let mut local_var_req_builder =
105        local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
106
107    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
108        local_var_req_builder =
109            local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
110    }
111    local_var_req_builder = local_var_req_builder.json(&create_developer_owned_user_request);
112
113    let local_var_req = local_var_req_builder.build()?;
114    let local_var_resp = local_var_client.execute(local_var_req).await?;
115
116    let local_var_status = local_var_resp.status();
117    let local_var_content = local_var_resp.text().await?;
118
119    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
120        serde_json::from_str(&local_var_content).map_err(Error::from)
121    } else {
122        let local_var_entity: Option<CreateDeveloperOwnedUserError> =
123            serde_json::from_str(&local_var_content).ok();
124        let local_var_error = ResponseContent {
125            status: local_var_status,
126            content: local_var_content,
127            entity: local_var_entity,
128        };
129        Err(Error::ResponseError(local_var_error))
130    }
131}
132
133pub async fn create_user(
134    configuration: &configuration::Configuration,
135    workspace: &str,
136    create_user_request: crate::models::CreateUserRequest,
137) -> Result<crate::models::User, Error<CreateUserError>> {
138    let local_var_configuration = configuration;
139
140    let local_var_client = &local_var_configuration.client;
141
142    let local_var_uri_str = format!(
143        "{}/workspaces/{workspace}/users",
144        local_var_configuration.base_path,
145        workspace = crate::apis::urlencode(workspace)
146    );
147    let mut local_var_req_builder =
148        local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
149
150    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
151        local_var_req_builder =
152            local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
153    }
154    local_var_req_builder = local_var_req_builder.json(&create_user_request);
155
156    let local_var_req = local_var_req_builder.build()?;
157    let local_var_resp = local_var_client.execute(local_var_req).await?;
158
159    let local_var_status = local_var_resp.status();
160    let local_var_content = local_var_resp.text().await?;
161
162    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
163        serde_json::from_str(&local_var_content).map_err(Error::from)
164    } else {
165        let local_var_entity: Option<CreateUserError> =
166            serde_json::from_str(&local_var_content).ok();
167        let local_var_error = ResponseContent {
168            status: local_var_status,
169            content: local_var_content,
170            entity: local_var_entity,
171        };
172        Err(Error::ResponseError(local_var_error))
173    }
174}
175
176pub async fn delete_user(
177    configuration: &configuration::Configuration,
178    workspace: &str,
179    id: &str,
180) -> Result<(), Error<DeleteUserError>> {
181    let local_var_configuration = configuration;
182
183    let local_var_client = &local_var_configuration.client;
184
185    let local_var_uri_str = format!(
186        "{}/workspaces/{workspace}/users/{id}",
187        local_var_configuration.base_path,
188        workspace = crate::apis::urlencode(workspace),
189        id = crate::apis::urlencode(id)
190    );
191    let mut local_var_req_builder =
192        local_var_client.request(reqwest::Method::DELETE, local_var_uri_str.as_str());
193
194    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
195        local_var_req_builder =
196            local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
197    }
198
199    let local_var_req = local_var_req_builder.build()?;
200    let local_var_resp = local_var_client.execute(local_var_req).await?;
201
202    let local_var_status = local_var_resp.status();
203    let local_var_content = local_var_resp.text().await?;
204
205    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
206        Ok(())
207    } else {
208        let local_var_entity: Option<DeleteUserError> =
209            serde_json::from_str(&local_var_content).ok();
210        let local_var_error = ResponseContent {
211            status: local_var_status,
212            content: local_var_content,
213            entity: local_var_entity,
214        };
215        Err(Error::ResponseError(local_var_error))
216    }
217}
218
219pub async fn get_user(
220    configuration: &configuration::Configuration,
221    workspace: &str,
222    id: &str,
223) -> Result<crate::models::User, Error<GetUserError>> {
224    let local_var_configuration = configuration;
225
226    let local_var_client = &local_var_configuration.client;
227
228    let local_var_uri_str = format!(
229        "{}/workspaces/{workspace}/users/{id}",
230        local_var_configuration.base_path,
231        workspace = crate::apis::urlencode(workspace),
232        id = crate::apis::urlencode(id)
233    );
234    let mut local_var_req_builder =
235        local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
236
237    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
238        local_var_req_builder =
239            local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
240    }
241
242    let local_var_req = local_var_req_builder.build()?;
243    let local_var_resp = local_var_client.execute(local_var_req).await?;
244
245    let local_var_status = local_var_resp.status();
246    let local_var_content = local_var_resp.text().await?;
247
248    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
249        serde_json::from_str(&local_var_content).map_err(Error::from)
250    } else {
251        let local_var_entity: Option<GetUserError> = serde_json::from_str(&local_var_content).ok();
252        let local_var_error = ResponseContent {
253            status: local_var_status,
254            content: local_var_content,
255            entity: local_var_entity,
256        };
257        Err(Error::ResponseError(local_var_error))
258    }
259}
260
261pub async fn list_users(
262    configuration: &configuration::Configuration,
263    workspace: &str,
264) -> Result<Vec<crate::models::User>, Error<ListUsersError>> {
265    let local_var_configuration = configuration;
266
267    let local_var_client = &local_var_configuration.client;
268
269    let local_var_uri_str = format!(
270        "{}/workspaces/{workspace}/users",
271        local_var_configuration.base_path,
272        workspace = crate::apis::urlencode(workspace)
273    );
274    let mut local_var_req_builder =
275        local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
276
277    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
278        local_var_req_builder =
279            local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
280    }
281
282    let local_var_req = local_var_req_builder.build()?;
283    let local_var_resp = local_var_client.execute(local_var_req).await?;
284
285    let local_var_status = local_var_resp.status();
286    let local_var_content = local_var_resp.text().await?;
287
288    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
289        serde_json::from_str(&local_var_content).map_err(Error::from)
290    } else {
291        let local_var_entity: Option<ListUsersError> =
292            serde_json::from_str(&local_var_content).ok();
293        let local_var_error = ResponseContent {
294            status: local_var_status,
295            content: local_var_content,
296            entity: local_var_entity,
297        };
298        Err(Error::ResponseError(local_var_error))
299    }
300}
301
302/// Login a user to Nile. This operation returns a JWT token. Most Nile operations require authentication and expect this token in the 'Authorization: Bearer <token>' header
303pub async fn login_user(
304    configuration: &configuration::Configuration,
305    workspace: &str,
306    login_info: crate::models::LoginInfo,
307) -> Result<crate::models::Token, Error<LoginUserError>> {
308    let local_var_configuration = configuration;
309
310    let local_var_client = &local_var_configuration.client;
311
312    let local_var_uri_str = format!(
313        "{}/workspaces/{workspace}/auth/login",
314        local_var_configuration.base_path,
315        workspace = crate::apis::urlencode(workspace)
316    );
317    let mut local_var_req_builder =
318        local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
319
320    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
321        local_var_req_builder =
322            local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
323    }
324    local_var_req_builder = local_var_req_builder.json(&login_info);
325
326    let local_var_req = local_var_req_builder.build()?;
327    let local_var_resp = local_var_client.execute(local_var_req).await?;
328
329    let local_var_status = local_var_resp.status();
330    let local_var_content = local_var_resp.text().await?;
331
332    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
333        serde_json::from_str(&local_var_content).map_err(Error::from)
334    } else {
335        let local_var_entity: Option<LoginUserError> =
336            serde_json::from_str(&local_var_content).ok();
337        let local_var_error = ResponseContent {
338            status: local_var_status,
339            content: local_var_content,
340            entity: local_var_entity,
341        };
342        Err(Error::ResponseError(local_var_error))
343    }
344}
345
346pub async fn me(
347    configuration: &configuration::Configuration,
348) -> Result<crate::models::User, Error<MeError>> {
349    let local_var_configuration = configuration;
350
351    let local_var_client = &local_var_configuration.client;
352
353    let local_var_uri_str = format!("{}/me", local_var_configuration.base_path);
354    let mut local_var_req_builder =
355        local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
356
357    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
358        local_var_req_builder =
359            local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
360    }
361
362    let local_var_req = local_var_req_builder.build()?;
363    let local_var_resp = local_var_client.execute(local_var_req).await?;
364
365    let local_var_status = local_var_resp.status();
366    let local_var_content = local_var_resp.text().await?;
367
368    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
369        serde_json::from_str(&local_var_content).map_err(Error::from)
370    } else {
371        let local_var_entity: Option<MeError> = serde_json::from_str(&local_var_content).ok();
372        let local_var_error = ResponseContent {
373            status: local_var_status,
374            content: local_var_content,
375            entity: local_var_entity,
376        };
377        Err(Error::ResponseError(local_var_error))
378    }
379}
380
381/// Echoes the auth token of the currently authenticated user. This operation requires that the auth token is passed either as a Bearer token in the authorization header or as a cookie named 'token'. When both are present, they must match.
382pub async fn token(
383    configuration: &configuration::Configuration,
384) -> Result<crate::models::Token, Error<TokenError>> {
385    let local_var_configuration = configuration;
386
387    let local_var_client = &local_var_configuration.client;
388
389    let local_var_uri_str = format!("{}/me/token", local_var_configuration.base_path);
390    let mut local_var_req_builder =
391        local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
392
393    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
394        local_var_req_builder =
395            local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
396    }
397
398    let local_var_req = local_var_req_builder.build()?;
399    let local_var_resp = local_var_client.execute(local_var_req).await?;
400
401    let local_var_status = local_var_resp.status();
402    let local_var_content = local_var_resp.text().await?;
403
404    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
405        serde_json::from_str(&local_var_content).map_err(Error::from)
406    } else {
407        let local_var_entity: Option<TokenError> = serde_json::from_str(&local_var_content).ok();
408        let local_var_error = ResponseContent {
409            status: local_var_status,
410            content: local_var_content,
411            entity: local_var_entity,
412        };
413        Err(Error::ResponseError(local_var_error))
414    }
415}
416
417pub async fn update_user(
418    configuration: &configuration::Configuration,
419    workspace: &str,
420    id: &str,
421    update_user_request: crate::models::UpdateUserRequest,
422) -> Result<crate::models::User, Error<UpdateUserError>> {
423    let local_var_configuration = configuration;
424
425    let local_var_client = &local_var_configuration.client;
426
427    let local_var_uri_str = format!(
428        "{}/workspaces/{workspace}/users/{id}",
429        local_var_configuration.base_path,
430        workspace = crate::apis::urlencode(workspace),
431        id = crate::apis::urlencode(id)
432    );
433    let mut local_var_req_builder =
434        local_var_client.request(reqwest::Method::PUT, local_var_uri_str.as_str());
435
436    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
437        local_var_req_builder =
438            local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
439    }
440    local_var_req_builder = local_var_req_builder.json(&update_user_request);
441
442    let local_var_req = local_var_req_builder.build()?;
443    let local_var_resp = local_var_client.execute(local_var_req).await?;
444
445    let local_var_status = local_var_resp.status();
446    let local_var_content = local_var_resp.text().await?;
447
448    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
449        serde_json::from_str(&local_var_content).map_err(Error::from)
450    } else {
451        let local_var_entity: Option<UpdateUserError> =
452            serde_json::from_str(&local_var_content).ok();
453        let local_var_error = ResponseContent {
454            status: local_var_status,
455            content: local_var_content,
456            entity: local_var_entity,
457        };
458        Err(Error::ResponseError(local_var_error))
459    }
460}
461
462/// Validates a user token. Use this when using Nile authentication to validate access to non-Nile resources. See the [Add Authentication Guide](https://nile-docs.vercel.app/docs/current/guides/how-to/add_signup_authn#decorating-the-endpoint) for a full example
463pub async fn validate_user(
464    configuration: &configuration::Configuration,
465    workspace: &str,
466    token: crate::models::Token,
467) -> Result<(), Error<ValidateUserError>> {
468    let local_var_configuration = configuration;
469
470    let local_var_client = &local_var_configuration.client;
471
472    let local_var_uri_str = format!(
473        "{}/workspaces/{workspace}/auth/validate",
474        local_var_configuration.base_path,
475        workspace = crate::apis::urlencode(workspace)
476    );
477    let mut local_var_req_builder =
478        local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
479
480    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
481        local_var_req_builder =
482            local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
483    }
484    local_var_req_builder = local_var_req_builder.json(&token);
485
486    let local_var_req = local_var_req_builder.build()?;
487    let local_var_resp = local_var_client.execute(local_var_req).await?;
488
489    let local_var_status = local_var_resp.status();
490    let local_var_content = local_var_resp.text().await?;
491
492    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
493        Ok(())
494    } else {
495        let local_var_entity: Option<ValidateUserError> =
496            serde_json::from_str(&local_var_content).ok();
497        let local_var_error = ResponseContent {
498            status: local_var_status,
499            content: local_var_content,
500            entity: local_var_entity,
501        };
502        Err(Error::ResponseError(local_var_error))
503    }
504}