trieve_client/apis/
file_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_file_handler`]
19#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum DeleteFileHandlerError {
22    Status400(models::ErrorResponseBody),
23    UnknownValue(serde_json::Value),
24}
25
26/// struct for typed errors of method [`get_dataset_files_handler`]
27#[derive(Debug, Clone, Serialize, Deserialize)]
28#[serde(untagged)]
29pub enum GetDatasetFilesHandlerError {
30    Status400(models::ErrorResponseBody),
31    UnknownValue(serde_json::Value),
32}
33
34/// struct for typed errors of method [`get_file_handler`]
35#[derive(Debug, Clone, Serialize, Deserialize)]
36#[serde(untagged)]
37pub enum GetFileHandlerError {
38    Status400(models::ErrorResponseBody),
39    Status404(models::ErrorResponseBody),
40    UnknownValue(serde_json::Value),
41}
42
43/// struct for typed errors of method [`upload_file_handler`]
44#[derive(Debug, Clone, Serialize, Deserialize)]
45#[serde(untagged)]
46pub enum UploadFileHandlerError {
47    Status400(models::ErrorResponseBody),
48    UnknownValue(serde_json::Value),
49}
50
51
52/// Delete a file from S3 attached to the server based on its id. This will disassociate chunks from the file, but only delete them all together if you specify delete_chunks to be true. Auth'ed user or api key must have an admin or owner role for the specified dataset's organization.
53pub async fn delete_file_handler(configuration: &configuration::Configuration, tr_dataset: &str, file_id: &str) -> Result<(), Error<DeleteFileHandlerError>> {
54    let local_var_configuration = configuration;
55
56    let local_var_client = &local_var_configuration.client;
57
58    let local_var_uri_str = format!("{}/api/file/{file_id}", local_var_configuration.base_path, file_id=crate::apis::urlencode(file_id));
59    let mut local_var_req_builder = local_var_client.request(reqwest::Method::DELETE, local_var_uri_str.as_str());
60
61    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
62        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
63    }
64    local_var_req_builder = local_var_req_builder.header("TR-Dataset", tr_dataset.to_string());
65    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
66        let local_var_key = local_var_apikey.key.clone();
67        let local_var_value = match local_var_apikey.prefix {
68            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
69            None => local_var_key,
70        };
71        local_var_req_builder = local_var_req_builder.header("Authorization", local_var_value);
72    };
73
74    let local_var_req = local_var_req_builder.build()?;
75    let local_var_resp = local_var_client.execute(local_var_req).await?;
76
77    let local_var_status = local_var_resp.status();
78    let local_var_content = local_var_resp.text().await?;
79
80    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
81        Ok(())
82    } else {
83        let local_var_entity: Option<DeleteFileHandlerError> = serde_json::from_str(&local_var_content).ok();
84        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
85        Err(Error::ResponseError(local_var_error))
86    }
87}
88
89/// Get all files which belong to a given dataset specified by the dataset_id parameter. 10 files are returned per page.
90pub async fn get_dataset_files_handler(configuration: &configuration::Configuration, tr_dataset: &str, dataset_id: &str, page: i64) -> Result<Vec<models::File>, Error<GetDatasetFilesHandlerError>> {
91    let local_var_configuration = configuration;
92
93    let local_var_client = &local_var_configuration.client;
94
95    let local_var_uri_str = format!("{}/api/dataset/files/{dataset_id}/{page}", local_var_configuration.base_path, dataset_id=crate::apis::urlencode(dataset_id), page=page);
96    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
97
98    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
99        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
100    }
101    local_var_req_builder = local_var_req_builder.header("TR-Dataset", tr_dataset.to_string());
102    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
103        let local_var_key = local_var_apikey.key.clone();
104        let local_var_value = match local_var_apikey.prefix {
105            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
106            None => local_var_key,
107        };
108        local_var_req_builder = local_var_req_builder.header("Authorization", local_var_value);
109    };
110
111    let local_var_req = local_var_req_builder.build()?;
112    let local_var_resp = local_var_client.execute(local_var_req).await?;
113
114    let local_var_status = local_var_resp.status();
115    let local_var_content = local_var_resp.text().await?;
116
117    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
118        serde_json::from_str(&local_var_content).map_err(Error::from)
119    } else {
120        let local_var_entity: Option<GetDatasetFilesHandlerError> = serde_json::from_str(&local_var_content).ok();
121        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
122        Err(Error::ResponseError(local_var_error))
123    }
124}
125
126/// Download a file based on its id.
127pub async fn get_file_handler(configuration: &configuration::Configuration, tr_dataset: &str, file_id: &str) -> Result<models::FileDto, Error<GetFileHandlerError>> {
128    let local_var_configuration = configuration;
129
130    let local_var_client = &local_var_configuration.client;
131
132    let local_var_uri_str = format!("{}/api/file/{file_id}", local_var_configuration.base_path, file_id=crate::apis::urlencode(file_id));
133    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
134
135    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
136        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
137    }
138    local_var_req_builder = local_var_req_builder.header("TR-Dataset", tr_dataset.to_string());
139    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
140        let local_var_key = local_var_apikey.key.clone();
141        let local_var_value = match local_var_apikey.prefix {
142            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
143            None => local_var_key,
144        };
145        local_var_req_builder = local_var_req_builder.header("Authorization", local_var_value);
146    };
147
148    let local_var_req = local_var_req_builder.build()?;
149    let local_var_resp = local_var_client.execute(local_var_req).await?;
150
151    let local_var_status = local_var_resp.status();
152    let local_var_content = local_var_resp.text().await?;
153
154    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
155        serde_json::from_str(&local_var_content).map_err(Error::from)
156    } else {
157        let local_var_entity: Option<GetFileHandlerError> = serde_json::from_str(&local_var_content).ok();
158        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
159        Err(Error::ResponseError(local_var_error))
160    }
161}
162
163/// Upload a file to S3 attached to the server. The file will be converted to HTML with tika and chunked algorithmically, images will be OCR'ed with tesseract. The resulting chunks will be indexed and searchable. Optionally, you can only upload the file and manually create chunks associated to the file after. See docs.trieve.ai and/or contact us for more details and tips. Auth'ed user must be an admin or owner of the dataset's organization to upload a file.
164pub async fn upload_file_handler(configuration: &configuration::Configuration, tr_dataset: &str, upload_file_req_payload: models::UploadFileReqPayload) -> Result<models::UploadFileResult, Error<UploadFileHandlerError>> {
165    let local_var_configuration = configuration;
166
167    let local_var_client = &local_var_configuration.client;
168
169    let local_var_uri_str = format!("{}/api/file", local_var_configuration.base_path);
170    let mut local_var_req_builder = local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
171
172    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
173        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
174    }
175    local_var_req_builder = local_var_req_builder.header("TR-Dataset", tr_dataset.to_string());
176    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
177        let local_var_key = local_var_apikey.key.clone();
178        let local_var_value = match local_var_apikey.prefix {
179            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
180            None => local_var_key,
181        };
182        local_var_req_builder = local_var_req_builder.header("Authorization", local_var_value);
183    };
184    local_var_req_builder = local_var_req_builder.json(&upload_file_req_payload);
185
186    let local_var_req = local_var_req_builder.build()?;
187    let local_var_resp = local_var_client.execute(local_var_req).await?;
188
189    let local_var_status = local_var_resp.status();
190    let local_var_content = local_var_resp.text().await?;
191
192    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
193        serde_json::from_str(&local_var_content).map_err(Error::from)
194    } else {
195        let local_var_entity: Option<UploadFileHandlerError> = serde_json::from_str(&local_var_content).ok();
196        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
197        Err(Error::ResponseError(local_var_error))
198    }
199}
200