trieve_client/apis/
user_api.rs

1/*
2 * Trieve API
3 *
4 * Trieve OpenAPI Specification. This document describes all of the operations available through the Trieve API.
5 *
6 * The version of the OpenAPI document: 0.11.7
7 * Contact: developers@trieve.ai
8 * Generated by: https://openapi-generator.tech
9 */
10
11
12use reqwest;
13
14use crate::{apis::ResponseContent, models};
15use super::{Error, configuration};
16
17
18/// struct for typed errors of method [`delete_user_api_key`]
19#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum DeleteUserApiKeyError {
22    Status400(models::ErrorResponseBody),
23    UnknownValue(serde_json::Value),
24}
25
26/// struct for typed errors of method [`set_user_api_key`]
27#[derive(Debug, Clone, Serialize, Deserialize)]
28#[serde(untagged)]
29pub enum SetUserApiKeyError {
30    Status400(models::ErrorResponseBody),
31    UnknownValue(serde_json::Value),
32}
33
34/// struct for typed errors of method [`update_user`]
35#[derive(Debug, Clone, Serialize, Deserialize)]
36#[serde(untagged)]
37pub enum UpdateUserError {
38    Status400(models::ErrorResponseBody),
39    UnknownValue(serde_json::Value),
40}
41
42
43/// Delete an api key for the auth'ed user.
44pub async fn delete_user_api_key(configuration: &configuration::Configuration, api_key_id: &str) -> Result<(), Error<DeleteUserApiKeyError>> {
45    let local_var_configuration = configuration;
46
47    let local_var_client = &local_var_configuration.client;
48
49    let local_var_uri_str = format!("{}/api/user/api_key/{api_key_id}", local_var_configuration.base_path, api_key_id=crate::apis::urlencode(api_key_id));
50    let mut local_var_req_builder = local_var_client.request(reqwest::Method::DELETE, local_var_uri_str.as_str());
51
52    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
53        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
54    }
55    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
56        let local_var_key = local_var_apikey.key.clone();
57        let local_var_value = match local_var_apikey.prefix {
58            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
59            None => local_var_key,
60        };
61        local_var_req_builder = local_var_req_builder.header("Authorization", local_var_value);
62    };
63
64    let local_var_req = local_var_req_builder.build()?;
65    let local_var_resp = local_var_client.execute(local_var_req).await?;
66
67    let local_var_status = local_var_resp.status();
68    let local_var_content = local_var_resp.text().await?;
69
70    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
71        Ok(())
72    } else {
73        let local_var_entity: Option<DeleteUserApiKeyError> = serde_json::from_str(&local_var_content).ok();
74        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
75        Err(Error::ResponseError(local_var_error))
76    }
77}
78
79/// Create a new api key for the auth'ed user. Successful response will contain the newly created api key. If a write role is assigned the api key will have permission level of the auth'ed user who calls this endpoint.
80pub async fn set_user_api_key(configuration: &configuration::Configuration, set_user_api_key_request: models::SetUserApiKeyRequest) -> Result<models::SetUserApiKeyResponse, Error<SetUserApiKeyError>> {
81    let local_var_configuration = configuration;
82
83    let local_var_client = &local_var_configuration.client;
84
85    let local_var_uri_str = format!("{}/api/user/api_key", local_var_configuration.base_path);
86    let mut local_var_req_builder = local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
87
88    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
89        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
90    }
91    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
92        let local_var_key = local_var_apikey.key.clone();
93        let local_var_value = match local_var_apikey.prefix {
94            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
95            None => local_var_key,
96        };
97        local_var_req_builder = local_var_req_builder.header("Authorization", local_var_value);
98    };
99    local_var_req_builder = local_var_req_builder.json(&set_user_api_key_request);
100
101    let local_var_req = local_var_req_builder.build()?;
102    let local_var_resp = local_var_client.execute(local_var_req).await?;
103
104    let local_var_status = local_var_resp.status();
105    let local_var_content = local_var_resp.text().await?;
106
107    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
108        serde_json::from_str(&local_var_content).map_err(Error::from)
109    } else {
110        let local_var_entity: Option<SetUserApiKeyError> = serde_json::from_str(&local_var_content).ok();
111        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
112        Err(Error::ResponseError(local_var_error))
113    }
114}
115
116/// Update a user's information. If the user_id is not provided, the auth'ed user will be updated. If the user_id is provided, the role of the auth'ed user or api key must be an admin (1) or owner (2) of the organization.
117pub async fn update_user(configuration: &configuration::Configuration, update_user_org_role_data: models::UpdateUserOrgRoleData) -> Result<(), Error<UpdateUserError>> {
118    let local_var_configuration = configuration;
119
120    let local_var_client = &local_var_configuration.client;
121
122    let local_var_uri_str = format!("{}/api/user", local_var_configuration.base_path);
123    let mut local_var_req_builder = local_var_client.request(reqwest::Method::PUT, local_var_uri_str.as_str());
124
125    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
126        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
127    }
128    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
129        let local_var_key = local_var_apikey.key.clone();
130        let local_var_value = match local_var_apikey.prefix {
131            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
132            None => local_var_key,
133        };
134        local_var_req_builder = local_var_req_builder.header("Authorization", local_var_value);
135    };
136    local_var_req_builder = local_var_req_builder.json(&update_user_org_role_data);
137
138    let local_var_req = local_var_req_builder.build()?;
139    let local_var_resp = local_var_client.execute(local_var_req).await?;
140
141    let local_var_status = local_var_resp.status();
142    let local_var_content = local_var_resp.text().await?;
143
144    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
145        Ok(())
146    } else {
147        let local_var_entity: Option<UpdateUserError> = serde_json::from_str(&local_var_content).ok();
148        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
149        Err(Error::ResponseError(local_var_error))
150    }
151}
152