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]]
name = "deepl"
version = "1.0.2"
version = "1.0.3"
dependencies = [
"anyhow",
"clap",

View File

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

View File

@ -1,18 +1,18 @@
# Deepl
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>
&emsp;deepl [OPTIONS] \<INPUT><br>
### USAGE:
deepl [OPTIONS] \<INPUT>
FLAGS:<br>
&emsp;-h, --help Prints help information<br>
&emsp;-V, --version Prints version information<br>
### FLAGS:
-h, --help Prints help information
-V, --version Prints version information
OPTIONS:<br>
&emsp;-s, --source <source_lang> The language you want to translate. Default is autodectect.<br>
&emsp;-t, --target <target_lang> The language your text should be tranlated to. Default is DE.<br>
### OPTIONS:
-s, --source <source_lang> The language you want to translate. Default is autodectect.
-t, --target <target_lang> The language your text should be tranlated to. Default is DE.
ARGS:<br>
&emsp;\<INPUT> Text to translate<br>
### ARGS:
\<INPUT> Text to translate

View File

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

View File

@ -1,5 +1,8 @@
use serde::{Deserialize, Serialize};
use crate::engine::params::Params;
use anyhow::Result;
#[derive(Serialize, Deserialize, Default)]
struct Translations {
detected_source_language: String,
@ -12,18 +15,28 @@ pub struct DeeplResponse {
}
impl DeeplResponse {
pub fn get_text(&self, target_language: &str) -> String {
let mut trans: String = String::new();
for translation in self.translations.iter() {
trans.push_str(&format!(
"--- Detected Source Language {} -------------------------\n",
translation.detected_source_language
pub fn get_text(&self, params: &Params) -> Result<String> {
let max_src = self
.translations
.iter()
.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()
.method(Method::POST)
.uri(params.url()?)
.header("Authorization", format!("DeepL-Auth-Key {}", params.key))
.header("content-type", "application/x-www-form-urlencoded")
.body(Body::from(params.body()?))?;
@ -24,6 +25,6 @@ async fn main() -> Result<()> {
let body = String::from_utf8(body_bytes.to_vec())?;
let v: DeeplResponse =
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(())
}