fix: use mercxry translations api

This commit is contained in:
2026-06-13 11:15:38 +02:00
parent d6fc3bdd4b
commit c35f8cce14
5 changed files with 24 additions and 19 deletions

View File

@@ -24,11 +24,11 @@ impl TranslationClient {
#[instrument(skip(self, text), fields(translation.kind = ?kind))]
pub async fn translate(&self, kind: TranslationKind, text: &str) -> Result<String, AppError> {
let url = format!("{}/translate/{}.json", self.base_url, endpoint_for(kind));
let url = format!("{}/translate/{}", self.base_url, endpoint_for(kind));
let response = self
.http
.post(url)
.form(&[("text", text)])
.json(&TranslationRequest { text })
.send()
.await
.map_err(|error| map_reqwest_error(&error))?;
@@ -51,6 +51,11 @@ impl TranslationClient {
}
}
#[derive(serde::Serialize)]
struct TranslationRequest<'a> {
text: &'a str,
}
#[derive(Debug, Deserialize)]
struct TranslationResponse {
contents: TranslationContents,
@@ -97,8 +102,8 @@ mod tests {
async fn posts_text_to_the_requested_translation_endpoint() {
let server = MockServer::start().await;
Mock::given(matchers::method("POST"))
.and(matchers::path("/translate/yoda.json"))
.and(matchers::body_string_contains("text=Created"))
.and(matchers::path("/translate/yoda"))
.and(matchers::body_string_contains("Created"))
.respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
"contents": { "translated": "Created, it was." }
})))
@@ -119,7 +124,7 @@ mod tests {
async fn rejects_empty_translations_so_callers_can_fallback() {
let server = MockServer::start().await;
Mock::given(matchers::method("POST"))
.and(matchers::path("/translate/yoda.json"))
.and(matchers::path("/translate/yoda"))
.respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
"contents": { "translated": " " }
})))
@@ -139,7 +144,7 @@ mod tests {
async fn maps_translation_http_failures_to_upstream_errors() {
let server = MockServer::start().await;
Mock::given(matchers::method("POST"))
.and(matchers::path("/translate/shakespeare.json"))
.and(matchers::path("/translate/shakespeare"))
.respond_with(ResponseTemplate::new(429))
.mount(&server)
.await;