trieve_client/apis/
auth_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 [`callback`]
19#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum CallbackError {
22    Status400(models::ErrorResponseBody),
23    UnknownValue(serde_json::Value),
24}
25
26/// struct for typed errors of method [`get_me`]
27#[derive(Debug, Clone, Serialize, Deserialize)]
28#[serde(untagged)]
29pub enum GetMeError {
30    Status400(models::ErrorResponseBody),
31    UnknownValue(serde_json::Value),
32}
33
34/// struct for typed errors of method [`login`]
35#[derive(Debug, Clone, Serialize, Deserialize)]
36#[serde(untagged)]
37pub enum LoginError {
38    Status400(models::ErrorResponseBody),
39    UnknownValue(serde_json::Value),
40}
41
42/// struct for typed errors of method [`logout`]
43#[derive(Debug, Clone, Serialize, Deserialize)]
44#[serde(untagged)]
45pub enum LogoutError {
46    UnknownValue(serde_json::Value),
47}
48
49
50/// This is the callback route for the OAuth provider, it should not be called directly. Redirects to browser with set-cookie header.
51pub async fn callback(configuration: &configuration::Configuration, ) -> Result<models::SlimUser, Error<CallbackError>> {
52    let local_var_configuration = configuration;
53
54    let local_var_client = &local_var_configuration.client;
55
56    let local_var_uri_str = format!("{}/api/auth/callback", local_var_configuration.base_path);
57    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
58
59    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
60        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
61    }
62
63    let local_var_req = local_var_req_builder.build()?;
64    let local_var_resp = local_var_client.execute(local_var_req).await?;
65
66    let local_var_status = local_var_resp.status();
67    let local_var_content = local_var_resp.text().await?;
68
69    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
70        serde_json::from_str(&local_var_content).map_err(Error::from)
71    } else {
72        let local_var_entity: Option<CallbackError> = serde_json::from_str(&local_var_content).ok();
73        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
74        Err(Error::ResponseError(local_var_error))
75    }
76}
77
78/// Get the user corresponding to your current auth credentials.
79pub async fn get_me(configuration: &configuration::Configuration, ) -> Result<models::SlimUser, Error<GetMeError>> {
80    let local_var_configuration = configuration;
81
82    let local_var_client = &local_var_configuration.client;
83
84    let local_var_uri_str = format!("{}/api/auth/me", local_var_configuration.base_path);
85    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
86
87    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
88        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
89    }
90    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
91        let local_var_key = local_var_apikey.key.clone();
92        let local_var_value = match local_var_apikey.prefix {
93            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
94            None => local_var_key,
95        };
96        local_var_req_builder = local_var_req_builder.header("Authorization", local_var_value);
97    };
98
99    let local_var_req = local_var_req_builder.build()?;
100    let local_var_resp = local_var_client.execute(local_var_req).await?;
101
102    let local_var_status = local_var_resp.status();
103    let local_var_content = local_var_resp.text().await?;
104
105    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
106        serde_json::from_str(&local_var_content).map_err(Error::from)
107    } else {
108        let local_var_entity: Option<GetMeError> = serde_json::from_str(&local_var_content).ok();
109        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
110        Err(Error::ResponseError(local_var_error))
111    }
112}
113
114/// This will redirect you to the OAuth provider for authentication with email/pass, SSO, Google, Github, etc.
115pub async fn login(configuration: &configuration::Configuration, organization_id: Option<&str>, redirect_uri: Option<&str>, inv_code: Option<&str>) -> Result<(), Error<LoginError>> {
116    let local_var_configuration = configuration;
117
118    let local_var_client = &local_var_configuration.client;
119
120    let local_var_uri_str = format!("{}/api/auth", local_var_configuration.base_path);
121    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
122
123    if let Some(ref local_var_str) = organization_id {
124        local_var_req_builder = local_var_req_builder.query(&[("organization_id", &local_var_str.to_string())]);
125    }
126    if let Some(ref local_var_str) = redirect_uri {
127        local_var_req_builder = local_var_req_builder.query(&[("redirect_uri", &local_var_str.to_string())]);
128    }
129    if let Some(ref local_var_str) = inv_code {
130        local_var_req_builder = local_var_req_builder.query(&[("inv_code", &local_var_str.to_string())]);
131    }
132    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
133        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
134    }
135
136    let local_var_req = local_var_req_builder.build()?;
137    let local_var_resp = local_var_client.execute(local_var_req).await?;
138
139    let local_var_status = local_var_resp.status();
140    let local_var_content = local_var_resp.text().await?;
141
142    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
143        Ok(())
144    } else {
145        let local_var_entity: Option<LoginError> = serde_json::from_str(&local_var_content).ok();
146        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
147        Err(Error::ResponseError(local_var_error))
148    }
149}
150
151/// Invalidate your current auth credential stored typically stored in a cookie. This does not invalidate your API key.
152pub async fn logout(configuration: &configuration::Configuration, ) -> Result<(), Error<LogoutError>> {
153    let local_var_configuration = configuration;
154
155    let local_var_client = &local_var_configuration.client;
156
157    let local_var_uri_str = format!("{}/api/auth", local_var_configuration.base_path);
158    let mut local_var_req_builder = local_var_client.request(reqwest::Method::DELETE, local_var_uri_str.as_str());
159
160    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
161        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
162    }
163
164    let local_var_req = local_var_req_builder.build()?;
165    let local_var_resp = local_var_client.execute(local_var_req).await?;
166
167    let local_var_status = local_var_resp.status();
168    let local_var_content = local_var_resp.text().await?;
169
170    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
171        Ok(())
172    } else {
173        let local_var_entity: Option<LogoutError> = serde_json::from_str(&local_var_content).ok();
174        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
175        Err(Error::ResponseError(local_var_error))
176    }
177}
178