deepl_openapi/apis/
translate_documents_api.rs

1/*
2 * DeepL API Documentation
3 *
4 * The DeepL API provides programmatic access to DeepL’s machine translation technology.
5 *
6 * The version of the OpenAPI document: 2.7.0
7 *
8 * Generated by: https://openapi-generator.tech
9 */
10
11use reqwest;
12
13use super::{configuration, Error};
14use crate::apis::ResponseContent;
15
16/// struct for typed errors of method [`download_document`]
17#[derive(Debug, Clone, Serialize, Deserialize)]
18#[serde(untagged)]
19pub enum DownloadDocumentError {
20    Status400(),
21    Status403(),
22    Status404(crate::models::DocumentTranslationError),
23    Status413(),
24    Status429(),
25    Status456(),
26    Status500(),
27    Status503(crate::models::DocumentTranslationError),
28    Status504(),
29    Status529(),
30    UnknownValue(serde_json::Value),
31}
32
33/// struct for typed errors of method [`get_document_status`]
34#[derive(Debug, Clone, Serialize, Deserialize)]
35#[serde(untagged)]
36pub enum GetDocumentStatusError {
37    Status400(),
38    Status403(),
39    Status404(),
40    Status413(),
41    Status429(),
42    Status456(),
43    Status500(),
44    Status504(),
45    Status529(),
46    UnknownValue(serde_json::Value),
47}
48
49/// struct for typed errors of method [`translate_document`]
50#[derive(Debug, Clone, Serialize, Deserialize)]
51#[serde(untagged)]
52pub enum TranslateDocumentError {
53    Status400(),
54    Status403(),
55    Status404(),
56    Status413(),
57    Status429(),
58    Status456(),
59    Status500(),
60    Status504(),
61    Status529(),
62    UnknownValue(serde_json::Value),
63}
64
65/// Once the status of the document translation process is `done`, the result can be downloaded.   For privacy reasons the translated document is automatically removed from the server once it was downloaded and cannot be downloaded again.
66pub async fn download_document(
67    configuration: &configuration::Configuration,
68    document_id: &str,
69    get_document_status_request: crate::models::GetDocumentStatusRequest,
70) -> Result<std::path::PathBuf, Error<DownloadDocumentError>> {
71    let local_var_configuration = configuration;
72
73    let local_var_client = &local_var_configuration.client;
74
75    let local_var_uri_str = format!(
76        "{}/document/{document_id}/result",
77        local_var_configuration.base_path,
78        document_id = crate::apis::urlencode(document_id)
79    );
80    let mut local_var_req_builder =
81        local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
82
83    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
84        local_var_req_builder =
85            local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
86    }
87    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
88        let local_var_key = local_var_apikey.key.clone();
89        let local_var_value = match local_var_apikey.prefix {
90            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
91            None => local_var_key,
92        };
93        local_var_req_builder = local_var_req_builder.header("Authorization", local_var_value);
94    };
95    local_var_req_builder = local_var_req_builder.json(&get_document_status_request);
96
97    let local_var_req = local_var_req_builder.build()?;
98    let local_var_resp = local_var_client.execute(local_var_req).await?;
99
100    let local_var_status = local_var_resp.status();
101    let local_var_content = local_var_resp.text().await?;
102
103    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
104        serde_json::from_str(&local_var_content).map_err(Error::from)
105    } else {
106        let local_var_entity: Option<DownloadDocumentError> =
107            serde_json::from_str(&local_var_content).ok();
108        let local_var_error = ResponseContent {
109            status: local_var_status,
110            content: local_var_content,
111            entity: local_var_entity,
112        };
113        Err(Error::ResponseError(local_var_error))
114    }
115}
116
117/// Retrieve the current status of a document translation process. If the translation is still in progress, the estimated time remaining is also included in the response.
118pub async fn get_document_status(
119    configuration: &configuration::Configuration,
120    document_id: &str,
121    get_document_status_request: crate::models::GetDocumentStatusRequest,
122) -> Result<crate::models::GetDocumentStatus200Response, Error<GetDocumentStatusError>> {
123    let local_var_configuration = configuration;
124
125    let local_var_client = &local_var_configuration.client;
126
127    let local_var_uri_str = format!(
128        "{}/document/{document_id}",
129        local_var_configuration.base_path,
130        document_id = crate::apis::urlencode(document_id)
131    );
132    let mut local_var_req_builder =
133        local_var_client.request(reqwest::Method::POST, 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 =
137            local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
138    }
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    local_var_req_builder = local_var_req_builder.json(&get_document_status_request);
148
149    let local_var_req = local_var_req_builder.build()?;
150    let local_var_resp = local_var_client.execute(local_var_req).await?;
151
152    let local_var_status = local_var_resp.status();
153    let local_var_content = local_var_resp.text().await?;
154
155    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
156        serde_json::from_str(&local_var_content).map_err(Error::from)
157    } else {
158        let local_var_entity: Option<GetDocumentStatusError> =
159            serde_json::from_str(&local_var_content).ok();
160        let local_var_error = ResponseContent {
161            status: local_var_status,
162            content: local_var_content,
163            entity: local_var_entity,
164        };
165        Err(Error::ResponseError(local_var_error))
166    }
167}
168
169/// This call uploads a document and queues it for translation. The call returns once the upload is complete, returning a document ID and key which can be used to [query the translation status](https://www.deepl.com/docs-api/documents/get-document-status) and to [download the translated document](https://www.deepl.com/docs-api/documents/download-document) once translation is complete.    Because the request includes a file upload, it must be an HTTP POST request with content type `multipart/form-data`.   Please be aware that the uploaded document is automatically removed from the server once the translated document has been downloaded. You have to upload the document again in order to restart the translation.   The maximum upload limit for documents is [available here](https://support.deepl.com/hc/articles/360020582359-Document-formats) and may vary based on API plan and document type.   You may specify the glossary to use for the document translation using the `glossary_id` parameter. **Important:** This requires the `source_lang` parameter to be set and the language pair of the glossary has to match the language pair of the request.
170pub async fn translate_document(
171    configuration: &configuration::Configuration,
172    target_lang: crate::models::TargetLanguage,
173    _file: std::path::PathBuf,
174    source_lang: Option<crate::models::SourceLanguage>,
175    filename: Option<&str>,
176    formality: Option<crate::models::Formality>,
177    glossary_id: Option<&str>,
178) -> Result<crate::models::TranslateDocument200Response, Error<TranslateDocumentError>> {
179    let local_var_configuration = configuration;
180
181    let local_var_client = &local_var_configuration.client;
182
183    let local_var_uri_str = format!("{}/document", local_var_configuration.base_path);
184    let mut local_var_req_builder =
185        local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
186
187    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
188        local_var_req_builder =
189            local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
190    }
191    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
192        let local_var_key = local_var_apikey.key.clone();
193        let local_var_value = match local_var_apikey.prefix {
194            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
195            None => local_var_key,
196        };
197        local_var_req_builder = local_var_req_builder.header("Authorization", local_var_value);
198    };
199    let mut local_var_form = reqwest::multipart::Form::new();
200    if let Some(local_var_param_value) = source_lang {
201        local_var_form = local_var_form.text("source_lang", local_var_param_value.to_string());
202    }
203    local_var_form = local_var_form.text("target_lang", target_lang.to_string());
204    // TODO: support file upload for 'file' parameter
205    if let Some(local_var_param_value) = filename {
206        local_var_form = local_var_form.text("filename", local_var_param_value.to_string());
207    }
208    if let Some(local_var_param_value) = formality {
209        local_var_form = local_var_form.text("formality", local_var_param_value.to_string());
210    }
211    if let Some(local_var_param_value) = glossary_id {
212        local_var_form = local_var_form.text("glossary_id", local_var_param_value.to_string());
213    }
214    local_var_req_builder = local_var_req_builder.multipart(local_var_form);
215
216    let local_var_req = local_var_req_builder.build()?;
217    let local_var_resp = local_var_client.execute(local_var_req).await?;
218
219    let local_var_status = local_var_resp.status();
220    let local_var_content = local_var_resp.text().await?;
221
222    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
223        serde_json::from_str(&local_var_content).map_err(Error::from)
224    } else {
225        let local_var_entity: Option<TranslateDocumentError> =
226            serde_json::from_str(&local_var_content).ok();
227        let local_var_error = ResponseContent {
228            status: local_var_status,
229            content: local_var_content,
230            entity: local_var_entity,
231        };
232        Err(Error::ResponseError(local_var_error))
233    }
234}