Skip to main content

hanzo_client/apis/
translate_api.rs

1/*
2 * Hanzo Cloud API
3 *
4 * The Hanzo Cloud API as a customer calls it: every operation under /v1/ except the operator's admin product, relay routes, legacy spellings and capabilities still reached by flag. Tagged by product: the first path segment after /v1/.
5 *
6 * The version of the OpenAPI document: v1
7 * 
8 * Generated by: https://openapi-generator.tech
9 */
10
11
12use reqwest;
13use serde::{Deserialize, Serialize, de::Error as _};
14use crate::{apis::ResponseContent, models};
15use super::{Error, configuration, ContentType};
16
17
18/// struct for typed errors of method [`get_translate_memory`]
19#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum GetTranslateMemoryError {
22    UnknownValue(serde_json::Value),
23}
24
25/// struct for typed errors of method [`post_translate`]
26#[derive(Debug, Clone, Serialize, Deserialize)]
27#[serde(untagged)]
28pub enum PostTranslateError {
29    UnknownValue(serde_json::Value),
30}
31
32/// struct for typed errors of method [`put_translate_memory`]
33#[derive(Debug, Clone, Serialize, Deserialize)]
34#[serde(untagged)]
35pub enum PutTranslateMemoryError {
36    UnknownValue(serde_json::Value),
37}
38
39
40/// List returns the org's own translation-memory entries, newest first, optionally narrowed to one target language and/or one position on the review ladder. It is the review lane's read: what a human reviewer works through.  The org is ALWAYS the validated principal's org, never a request field, so one tenant can never read another's memory — the entries hold customer source text.
41pub async fn get_translate_memory(configuration: &configuration::Configuration, target: Option<&str>, state: Option<&str>, limit: Option<i32>) -> Result<models::MemoryPage, Error<GetTranslateMemoryError>> {
42    // add a prefix to parameters to efficiently prevent name collisions
43    let p_target = target;
44    let p_state = state;
45    let p_limit = limit;
46
47    let uri_str = format!("{}/v1/translate/memory", configuration.base_path);
48    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
49
50    if let Some(ref param_value) = p_target {
51        req_builder = req_builder.query(&[("target", &param_value.to_string())]);
52    }
53    if let Some(ref param_value) = p_state {
54        req_builder = req_builder.query(&[("state", &param_value.to_string())]);
55    }
56    if let Some(ref param_value) = p_limit {
57        req_builder = req_builder.query(&[("limit", &param_value.to_string())]);
58    }
59    if let Some(ref user_agent) = configuration.user_agent {
60        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
61    }
62    if let Some(ref token) = configuration.bearer_access_token {
63        req_builder = req_builder.bearer_auth(token.to_owned());
64    };
65
66    let req = req_builder.build()?;
67    let resp = configuration.client.execute(req).await?;
68
69    let status = resp.status();
70    let content_type = resp
71        .headers()
72        .get("content-type")
73        .and_then(|v| v.to_str().ok())
74        .unwrap_or("application/octet-stream");
75    let content_type = super::ContentType::from(content_type);
76
77    if !status.is_client_error() && !status.is_server_error() {
78        let content = resp.text().await?;
79        match content_type {
80            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
81            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::MemoryPage`"))),
82            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::MemoryPage`")))),
83        }
84    } else {
85        let content = resp.text().await?;
86        let entity: Option<GetTranslateMemoryError> = serde_json::from_str(&content).ok();
87        Err(Error::ResponseError(ResponseContent { status, content, entity }))
88    }
89}
90
91/// Returns one translation per input string, in input order, each carrying where it sits on the review ladder and whether it came from your memory rather than an engine — plus a usage block of REAL counts (strings, cached, translated, and the source characters that actually reached an engine). Send `text` for one string or `batch` for many, never both. When you name no `source`, the detected one is reported back.  THE TRANSLATION MEMORY IS CONSULTED FIRST AND IT IS NORMATIVE, NOT A CACHE. Every string keys on (source text, target, glossary version, tier); a hit is returned VERBATIM and never re-translated, which is what makes a locale rebuild idempotent under a non-deterministic model and the bill proportional to what actually changed. Misses go to the engine and are written back at state `machine`. Editing a glossary term changes the key, so a stale rendering can never be served.  IT CANNOT TRAMPLE REVIEWED WORK. A write from this route may create an entry or refresh one still at `machine`, and nothing else — a string a human moved to approved or published through the memory review lane survives every rebuild, and comes back here unchanged. The memory is the caller's OWN org's, a separate store per org: the source text you send is customer content and lands nowhere else. Read it back or review it at /v1/translate/memory.  `tier` picks the engine and defaults to quality — the model plane, which carries context, terminology and tone, and which bills its own tokens, so nothing is charged twice here. `bulk` is the high-volume engine and is metered HERE, on the source characters that reached it: a fully-cached rebuild reports zero characters and costs zero. BULK NEVER FALLS BACK TO QUALITY — on a deployment that does not serve it the answer is 503 for that tier, so a caller is never quietly served, or charged, at a tier it did not ask for. A bulk request beyond its balance is refused with the nested {\"error\":{\"code\",\"message\"}} body at 402/503.  `target` IS CHECKED FOR SHAPE, NOT FOR SUPPORT: anything BCP-47-shaped is accepted (`es`, `pt-BR`), anything else is 400. There is no unsupported-language error — a well-formed tag no engine can actually render is passed straight through, and whatever comes back is what gets stored and returned. `format` (text, html, markdown) tells the engine what markup to preserve; `glossary` fixes terms verbatim.  Requires a validated principal — 401 without one, and the org is always that principal's. At most 512 strings per call and 32768 characters per string; an engine that fails or answers a reply that does not cover every input is 502, and nothing is stored.
92pub async fn post_translate(configuration: &configuration::Configuration, ) -> Result<(), Error<PostTranslateError>> {
93
94    let uri_str = format!("{}/v1/translate", configuration.base_path);
95    let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
96
97    if let Some(ref user_agent) = configuration.user_agent {
98        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
99    }
100    if let Some(ref token) = configuration.bearer_access_token {
101        req_builder = req_builder.bearer_auth(token.to_owned());
102    };
103
104    let req = req_builder.build()?;
105    let resp = configuration.client.execute(req).await?;
106
107    let status = resp.status();
108
109    if !status.is_client_error() && !status.is_server_error() {
110        Ok(())
111    } else {
112        let content = resp.text().await?;
113        let entity: Option<PostTranslateError> = serde_json::from_str(&content).ok();
114        Err(Error::ResponseError(ResponseContent { status, content, entity }))
115    }
116}
117
118/// Review records a human decision on one translation-memory entry, and returns the entry as stored. A human write always wins over the stored value, and once it lands at approved or published no machine write can move it again — which is what makes a locale rebuild safe to run against reviewed work.  The org is ALWAYS the validated principal's org, never a request field, so a review can only ever land in the caller's own memory.
119pub async fn put_translate_memory(configuration: &configuration::Configuration, review_request: models::ReviewRequest) -> Result<models::MemoryEntry, Error<PutTranslateMemoryError>> {
120    // add a prefix to parameters to efficiently prevent name collisions
121    let p_review_request = review_request;
122
123    let uri_str = format!("{}/v1/translate/memory", configuration.base_path);
124    let mut req_builder = configuration.client.request(reqwest::Method::PUT, &uri_str);
125
126    if let Some(ref user_agent) = configuration.user_agent {
127        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
128    }
129    if let Some(ref token) = configuration.bearer_access_token {
130        req_builder = req_builder.bearer_auth(token.to_owned());
131    };
132    req_builder = req_builder.json(&p_review_request);
133
134    let req = req_builder.build()?;
135    let resp = configuration.client.execute(req).await?;
136
137    let status = resp.status();
138    let content_type = resp
139        .headers()
140        .get("content-type")
141        .and_then(|v| v.to_str().ok())
142        .unwrap_or("application/octet-stream");
143    let content_type = super::ContentType::from(content_type);
144
145    if !status.is_client_error() && !status.is_server_error() {
146        let content = resp.text().await?;
147        match content_type {
148            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
149            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::MemoryEntry`"))),
150            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::MemoryEntry`")))),
151        }
152    } else {
153        let content = resp.text().await?;
154        let entity: Option<PutTranslateMemoryError> = serde_json::from_str(&content).ok();
155        Err(Error::ResponseError(ResponseContent { status, content, entity }))
156    }
157}
158