added dotenv, INPUT, sprintf

This commit is contained in:
2021-09-17 18:52:22 +02:00
parent 10079766f3
commit 4f80acb5e9
5 changed files with 100 additions and 17 deletions
+7 -4
View File
@@ -11,7 +11,7 @@ args:
takes_value: true
required: false
- target:
short: t
short: d
long: target
value_name: target_lang
help: The language your text should be tranlated to.
@@ -20,8 +20,11 @@ args:
- file:
short: f
long: file
desc: File you want to translate.
help: Csv delimiter character
help: File you want to translate.
required: false
takes_value: true
takes_value: true
- INPUT:
help: Text to translate
required: true
index: 1
+39 -10
View File
@@ -1,14 +1,37 @@
pub mod deepl_helper {
use clap::ArgMatches;
use clap::{load_yaml, App};
use directories::BaseDirs;
use dotenv;
use sprintf::sprintf;
use std::env::{self};
pub struct Params {
source_lang: String,
destination_lang: String,
file: String,
key: String,
uri: String,
text: String,
}
impl Params {
pub fn new_from_matches(matches: ArgMatches) -> Self {
pub fn new() -> Self {
let base_dir = match BaseDirs::new() {
Some(dirs) => dirs,
None => {
panic!("Please provide key in ~/.config/deepl/.env file")
}
};
let my_path = base_dir.config_dir().join("deepl/.env");
match dotenv::from_path(my_path.as_path()) {
Ok(env) => env,
Err(_) => panic!("could not load .env file {:?}", my_path.as_path()),
};
let yaml = load_yaml!("cli.yml");
let matches = App::from_yaml(yaml).get_matches();
Self {
source_lang: matches.value_of("source_lang").unwrap_or("EN").to_string(),
destination_lang: matches
@@ -16,19 +39,25 @@ pub mod deepl_helper {
.unwrap_or("DE")
.to_string(),
file: matches.value_of("file").unwrap_or_default().to_string(),
key: env::var("key").unwrap(),
uri: env::var("uri").unwrap(),
text: matches.value_of("INPUT").unwrap().to_string(),
}
}
pub fn source(&self) -> &String {
&self.source_lang
pub fn url(&self) -> String {
sprintf!("%s?auth_key=%s", self.uri, self.key).unwrap()
}
pub fn dest(&self) -> &String {
&self.destination_lang
}
pub fn file(&self) -> &String {
&self.file
pub fn body(&self) -> String {
sprintf!(
"auth_key=%s&text=%s&target_lang=%s&source_lang=%s",
self.key,
self.text,
self.destination_lang,
self.source_lang
)
.unwrap()
}
}
}
+5 -2
View File
@@ -1,3 +1,4 @@
use deepl::deepl_helper::Params;
use hyper::{Body, Client, Method, Request, body};
use hyper_tls::HttpsConnector;
use serde_json::Value;
@@ -7,11 +8,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let https = HttpsConnector::new();
let client = Client::builder().build::<_, hyper::Body>(https);
let params = Params::new();
let request = Request::builder()
.method(Method::POST)
.uri("https://api-free.deepl.com/v2/translate?auth_key=7e47a305-561c-0a25-1e1a-ba929d96a88c:fx")
.uri(params.url())
.header("content-type", "application/x-www-form-urlencoded")
.body(Body::from(r#"auth_key=7e47a305-561c-0a25-1e1a-ba929d96a88c:fx&text=Hello, world&target_lang=DE"#))?;
.body(Body::from(params.body()))?;
let res = client.request(request).await?;
let body_bytes = body::to_bytes(res.into_body()).await?;