#![doc = include_str!("../README.md")]
mod endpoint;
mod lang;
use std::sync::Arc;
pub use endpoint::{
document::{DocumentStatusResp, DocumentTranslateStatus, UploadDocumentResp},
glossary,
languages::{LangInfo, LangType},
translate::{ModelType, TagHandling, TranslateTextResp},
usage::UsageResponse,
Error, Formality,
};
pub use lang::{Lang, LangConvertError};
pub use reqwest;
#[derive(Debug, Clone)]
pub struct DeepLApi {
inner: Arc<DeepLApiInner>,
}
#[derive(Debug)]
struct DeepLApiInner {
client: reqwest::Client,
key: String,
endpoint: reqwest::Url,
}
impl DeepLApi {
pub fn with(key: &str) -> DeepLApiBuilder {
DeepLApiBuilder::init(key.to_string())
}
fn del(&self, url: reqwest::Url) -> reqwest::RequestBuilder {
self.inner
.client
.delete(url)
.header("Authorization", &self.inner.key)
}
fn post(&self, url: reqwest::Url) -> reqwest::RequestBuilder {
self.inner
.client
.post(url)
.header("Authorization", &self.inner.key)
}
fn get(&self, url: reqwest::Url) -> reqwest::RequestBuilder {
self.inner
.client
.get(url)
.header("Authorization", &self.inner.key)
}
fn get_endpoint(&self, route: &str) -> reqwest::Url {
self.inner.endpoint.join(route).unwrap()
}
}
pub struct DeepLApiBuilder {
is_pro: bool,
client: Option<reqwest::Client>,
key: String,
}
impl DeepLApiBuilder {
fn init(key: String) -> Self {
Self {
key,
is_pro: false,
client: None,
}
}
pub fn client(&mut self, c: reqwest::Client) -> &mut Self {
self.client = Some(c);
self
}
pub fn is_pro(&mut self, is_pro: bool) -> &mut Self {
self.is_pro = is_pro;
self
}
pub fn new(&self) -> DeepLApi {
let client = self.client.clone().unwrap_or_else(reqwest::Client::new);
let endpoint = if self.is_pro || !self.key.ends_with(":fx") {
"https://api.deepl.com/v2/"
} else {
"https://api-free.deepl.com/v2/"
};
let inner = DeepLApiInner {
key: format!("DeepL-Auth-Key {}", self.key),
client,
endpoint: reqwest::Url::parse(endpoint).unwrap(),
};
DeepLApi {
inner: Arc::new(inner),
}
}
}