moved authorization to header

master
Mathias Rothenhaeusler 2026-01-23 10:46:42 +01:00
parent a725ab3928
commit faefafe675
6 changed files with 45 additions and 32 deletions

2
Cargo.lock generated
View File

@ -152,7 +152,7 @@ checksum = "ea221b5284a47e40033bf9b66f35f984ec0ea2931eb03505246cd27a963f981b"
[[package]] [[package]]
name = "deepl" name = "deepl"
version = "1.0.2" version = "1.0.3"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"clap", "clap",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "deepl" name = "deepl"
version = "1.0.2" version = "1.0.3"
edition = "2024" edition = "2024"
license = "MIT OR Apache-2.0" license = "MIT OR Apache-2.0"
authors = ["Mathias Rothenhäusler <safemind@posteo.net"] authors = ["Mathias Rothenhäusler <safemind@posteo.net"]

View File

@ -1,18 +1,18 @@
# Deepl # Deepl
Cli API for deepl translations. Create a .env config file in your user config folder (e.g. ~/config/deepl/.env). Cli API for deepl translations. Create a .env config file in your user config folder (e.g. ~/config/deepl/.env).
Required keys URI (eg. https://api-free.deepl.com/v2/translate) and KEY (eg. XYZ:rg). <br> Required keys URI (eg. https://api-free.deepl.com/v2/translate) and KEY (eg. XYZ:rg).
USAGE:<br> ### USAGE:
&emsp;deepl [OPTIONS] \<INPUT><br> deepl [OPTIONS] \<INPUT>
FLAGS:<br> ### FLAGS:
&emsp;-h, --help Prints help information<br> -h, --help Prints help information
&emsp;-V, --version Prints version information<br> -V, --version Prints version information
OPTIONS:<br> ### OPTIONS:
&emsp;-s, --source <source_lang> The language you want to translate. Default is autodectect.<br> -s, --source <source_lang> The language you want to translate. Default is autodectect.
&emsp;-t, --target <target_lang> The language your text should be tranlated to. Default is DE.<br> -t, --target <target_lang> The language your text should be tranlated to. Default is DE.
ARGS:<br> ### ARGS:
&emsp;\<INPUT> Text to translate<br> \<INPUT> Text to translate

View File

@ -27,9 +27,9 @@ pub struct Cli {
pub struct Params { pub struct Params {
pub source_lang: String, pub source_lang: String,
pub target_lang: String, pub target_lang: String,
key: String, pub key: String,
uri: String, uri: String,
text: String, pub input: String,
} }
impl Params { impl Params {
@ -47,14 +47,14 @@ impl Params {
Ok(Self { Ok(Self {
source_lang: cli.source, source_lang: cli.source,
target_lang: cli.target, target_lang: cli.target,
text: cli.input, input: cli.input,
key: env::var("KEY").context("KEY env var missing")?, key: env::var("KEY").context("KEY env var missing")?,
uri: env::var("URI").context("URI env var missing")?, uri: env::var("URI").context("URI env var missing")?,
}) })
} }
pub fn url(&self) -> Result<String> { pub fn url(&self) -> Result<String> {
let url = sprintf!("%s?auth_key=%s", self.uri, self.key) let url = sprintf!("%s", self.uri)
.map_err(|e| anyhow!("could not create request body: {e:?}"))?; .map_err(|e| anyhow!("could not create request body: {e:?}"))?;
Ok(url) Ok(url)
@ -62,9 +62,8 @@ impl Params {
pub fn body(&self) -> Result<String> { pub fn body(&self) -> Result<String> {
let mut body = sprintf!( let mut body = sprintf!(
"auth_key=%s&text=%s&target_lang=%s", "text=%s&target_lang=%s",
self.key, self.input,
self.text,
self.target_lang self.target_lang
) )
.map_err(|e| anyhow!("could not create request body: {e:?}"))?; .map_err(|e| anyhow!("could not create request body: {e:?}"))?;

View File

@ -1,5 +1,8 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::engine::params::Params;
use anyhow::Result;
#[derive(Serialize, Deserialize, Default)] #[derive(Serialize, Deserialize, Default)]
struct Translations { struct Translations {
detected_source_language: String, detected_source_language: String,
@ -12,18 +15,28 @@ pub struct DeeplResponse {
} }
impl DeeplResponse { impl DeeplResponse {
pub fn get_text(&self, target_language: &str) -> String { pub fn get_text(&self, params: &Params) -> Result<String> {
let mut trans: String = String::new();
for translation in self.translations.iter() { let max_src = self
trans.push_str(&format!( .translations
"--- Detected Source Language {} -------------------------\n", .iter()
translation.detected_source_language .map(|t| t.text.len())
.max()
.unwrap_or(0);
let mut out = String::new();
for t in &self.translations {
out.push_str(&format!(
"{src} >>> {:<width$} \n{tgt} >>> {}\n",
params.input,
t.text,
src = t.detected_source_language,
tgt = params.target_lang,
width = max_src,
)); ));
trans.push_str(&format!("{}: {}", target_language, translation.text));
trans.push('\n');
trans.push_str(&"-".repeat(57));
trans.push('\n');
} }
trans
Ok(out)
} }
} }

View File

@ -16,6 +16,7 @@ async fn main() -> Result<()> {
let request = Request::builder() let request = Request::builder()
.method(Method::POST) .method(Method::POST)
.uri(params.url()?) .uri(params.url()?)
.header("Authorization", format!("DeepL-Auth-Key {}", params.key))
.header("content-type", "application/x-www-form-urlencoded") .header("content-type", "application/x-www-form-urlencoded")
.body(Body::from(params.body()?))?; .body(Body::from(params.body()?))?;
@ -24,6 +25,6 @@ async fn main() -> Result<()> {
let body = String::from_utf8(body_bytes.to_vec())?; let body = String::from_utf8(body_bytes.to_vec())?;
let v: DeeplResponse = let v: DeeplResponse =
serde_json::from_str(&body).context(format!("Could not parse body: {}", body))?; serde_json::from_str(&body).context(format!("Could not parse body: {}", body))?;
println!("{}", v.get_text(&params.target_lang)); println!("{}", v.get_text(&params)?);
Ok(()) Ok(())
} }