Get rid of tokio and rust_decimal_macros, use blocking reqwest

This commit is contained in:
2024-03-21 11:33:32 +01:00
parent d68ddf5d5e
commit b3347ad3d4
5 changed files with 140 additions and 140 deletions
+5 -6
View File
@@ -1,12 +1,11 @@
use crate::*;
use rust_decimal::prelude::*;
use rusty_money::{iso::find, ExchangeRate, Money};
pub async fn update_rate(code: &String) {
pub fn update_rate(code: &String) {
if cache::get_next_update(code).expect("Error getting next update time from cache")
<= config::get_current_time()
{
let status = requests::get_rates(code)
.await
.expect("Error while fetching rates");
if status == requests::Status::INVALID {
panic!("Invalid api key when getting rates")
@@ -17,7 +16,7 @@ pub async fn update_rate(code: &String) {
}
}
}
pub async fn get_rate(code_from: &String, code_to: &String) -> String {
pub fn get_rate(code_from: &String, code_to: &String) -> String {
if !cache::check_code(code_from).expect("Error on getting code status") {
panic!("Code {} doesn't exists, use correct code!", code_from);
}
@@ -28,16 +27,16 @@ pub async fn get_rate(code_from: &String, code_to: &String) -> String {
|| (cache::get_next_update(code_from).expect("Error getting next update time from cache")
<= config::get_current_time())
{
update_rate(code_from).await;
update_rate(code_from);
}
cache::get_rate(code_from, code_to).expect("Error when getting cached rate")
}
pub async fn convert_value(code_from: &String, code_to: &String, value: &String) {
pub fn convert_value(code_from: &String, code_to: &String, value: &String) {
if value.parse::<f64>().is_err() {
panic!("{} is not a number!", value);
}
let text_rate = get_rate(code_from, code_to).await;
let text_rate = get_rate(code_from, code_to);
let from_currency = find(code_from);
if from_currency.is_none() {
panic!("{} not found in ISO formats", code_from);
+7 -11
View File
@@ -45,9 +45,9 @@ struct Cli {
#[arg(short = 'L', long = "list-rates", value_names = ["currency"])]
list_rates: Option<String>,
}
async fn setup_key(key: String) -> Result<bool, Box<dyn std::error::Error>> {
fn setup_key(key: String) -> Result<bool, Box<dyn std::error::Error>> {
set_api_key(key)?;
let status = get_currencies().await?;
let status = get_currencies()?;
if status == requests::Status::INVALID {
set_api_key("".to_string())?;
println!("Api Key is invalid");
@@ -64,8 +64,7 @@ async fn setup_key(key: String) -> Result<bool, Box<dyn std::error::Error>> {
Ok(true)
}
#[tokio::main]
async fn main() -> Result<ExitCode, Box<dyn std::error::Error>> {
fn main() -> Result<ExitCode, Box<dyn std::error::Error>> {
let args = Cli::parse();
let all_args =
args.currency_from.is_some() && args.currency_to.is_some() && args.value.is_some();
@@ -92,7 +91,6 @@ async fn main() -> Result<ExitCode, Box<dyn std::error::Error>> {
None => {}
Some(key) => {
let res = setup_key(key)
.await
.expect("Unknown error while setting up key");
if !res {
return Ok(ExitCode::FAILURE);
@@ -125,7 +123,7 @@ async fn main() -> Result<ExitCode, Box<dyn std::error::Error>> {
println!("Code {} not found", code);
return Ok(ExitCode::FAILURE);
}
exchange::update_rate(&code).await;
exchange::update_rate(&code);
let rates = cache::list_rates(&code)?;
for rate in rates {
println!("{} to {} rate: {}", code, rate[0], rate[1]);
@@ -139,14 +137,13 @@ async fn main() -> Result<ExitCode, Box<dyn std::error::Error>> {
&args.currency_to.unwrap().to_uppercase(),
&args.value.unwrap(),
)
.await
}
} else {
interactive().await?;
interactive()?;
}
Ok(ExitCode::SUCCESS)
}
async fn interactive() -> Result<(), Box<dyn std::error::Error>> {
fn interactive() -> Result<(), Box<dyn std::error::Error>> {
let mut key_setup = cache::get_api_key()
.expect("Error while getting api key")
.len()
@@ -159,7 +156,6 @@ async fn interactive() -> Result<(), Box<dyn std::error::Error>> {
.read_line(&mut key_string)
.expect("Did not enter a correct string");
setup_key(key_string.trim().to_string())
.await
.expect("Unknown error while setting up key");
key_setup = cache::get_api_key()
.expect("Error while getting api key")
@@ -216,7 +212,7 @@ async fn interactive() -> Result<(), Box<dyn std::error::Error>> {
amount_check = true
}
}
convert_value(&code_from, &code_to, &amount).await;
convert_value(&code_from, &code_to, &amount);
Ok(())
}
+10 -12
View File
@@ -29,18 +29,17 @@ struct Err {
error_type: String,
}
pub async fn get_rates(code: &String) -> Result<Status, reqwest::Error> {
let response = reqwest::get(format!(
pub fn get_rates(code: &String) -> Result<Status, reqwest::Error> {
let response = reqwest::blocking::get(format!(
"{}{}{}{}",
get_endpoint(),
get_api_key().expect("Error when getting api key from cache"),
"/latest/",
code.to_uppercase()
))
.await?;
))?;
if response.status().is_success() {
let response: ConversionRates =
serde_json::from_str(&response.text().await?).expect("Error when deserializng");
serde_json::from_str(&response.text()?).expect("Error when deserializng");
cache::add_rates(
response.time_next_update_unix,
&response.base_code,
@@ -50,7 +49,7 @@ pub async fn get_rates(code: &String) -> Result<Status, reqwest::Error> {
return Ok(Status::OK);
} else {
let err: Err =
serde_json::from_str(&response.text().await?).expect("Error when deserializng");
serde_json::from_str(&response.text()?).expect("Error when deserializng");
if err.error_type == "invalid-key" {
return Ok(Status::INVALID);
} else if err.error_type == "quota-reached" {
@@ -60,24 +59,23 @@ pub async fn get_rates(code: &String) -> Result<Status, reqwest::Error> {
Ok(Status::ERROR)
}
pub async fn get_currencies() -> Result<Status, reqwest::Error> {
let response = reqwest::get(format!(
pub fn get_currencies() -> Result<Status, reqwest::Error> {
let response = reqwest::blocking::get(format!(
"{}{}{}",
get_endpoint(),
get_api_key().expect("Error when getting api key from cache"),
"/codes"
))
.await?;
))?;
if response.status().is_success() {
let codes: CurrencyCodes =
serde_json::from_str(&response.text().await?).expect("Error when deserializng");
serde_json::from_str(&response.text()?).expect("Error when deserializng");
for code in codes.supported_codes {
cache::add_code(code).expect("Error when adding code to cache");
}
return Ok(Status::OK);
} else {
let err: Err =
serde_json::from_str(&response.text().await?).expect("Error when deserializng");
serde_json::from_str(&response.text()?).expect("Error when deserializng");
if err.error_type == "invalid-key" {
return Ok(Status::INVALID);
} else if err.error_type == "quota-reached" {