fix: use mercxry translations api
This commit is contained in:
@@ -150,8 +150,8 @@ mod tests {
|
||||
let translations = MockServer::start().await;
|
||||
mount_species(&pokeapi, "mewtwo", true, "rare", "Created by science.").await;
|
||||
Mock::given(matchers::method("POST"))
|
||||
.and(matchers::path("/translate/yoda.json"))
|
||||
.and(matchers::body_string_contains("Created+by+science"))
|
||||
.and(matchers::path("/translate/yoda"))
|
||||
.and(matchers::body_string_contains("Created by science"))
|
||||
.respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
|
||||
"contents": { "translated": "Created by science, it was." }
|
||||
})))
|
||||
@@ -174,7 +174,7 @@ mod tests {
|
||||
let translations = MockServer::start().await;
|
||||
mount_species(&pokeapi, "pikachu", false, "forest", "Electric cheeks.").await;
|
||||
Mock::given(matchers::method("POST"))
|
||||
.and(matchers::path("/translate/shakespeare.json"))
|
||||
.and(matchers::path("/translate/shakespeare"))
|
||||
.respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
|
||||
"contents": { "translated": "Electric cheeks, good sir." }
|
||||
})))
|
||||
@@ -197,7 +197,7 @@ mod tests {
|
||||
let translations = MockServer::start().await;
|
||||
mount_species(&pokeapi, "zubat", false, "cave", "It has no eyes.").await;
|
||||
Mock::given(matchers::method("POST"))
|
||||
.and(matchers::path("/translate/yoda.json"))
|
||||
.and(matchers::path("/translate/yoda"))
|
||||
.respond_with(ResponseTemplate::new(500))
|
||||
.expect(1)
|
||||
.mount(&translations)
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -17,7 +17,7 @@ impl AppConfig {
|
||||
Self {
|
||||
bind_addr: default_bind_addr(),
|
||||
pokeapi_base_url: "https://pokeapi.co/api/v2".to_owned(),
|
||||
translations_base_url: "https://funtranslations.mercxry.me".to_owned(),
|
||||
translations_base_url: "https://api.funtranslations.mercxry.me/v1".to_owned(),
|
||||
request_timeout: Duration::from_secs(5),
|
||||
rate_limit_per_second: 20.0,
|
||||
rate_limit_burst: 40,
|
||||
|
||||
Reference in New Issue
Block a user