Get rid of tokio and rust_decimal_macros, use blocking reqwest

This commit is contained in:
Mateusz Słodkowicz 2024-03-21 11:33:32 +01:00
parent d68ddf5d5e
commit b3347ad3d4
Signed by: materus
GPG Key ID: 28D140BCA60B4FD1
5 changed files with 140 additions and 140 deletions

223
Cargo.lock generated
View File

@ -135,9 +135,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]] [[package]]
name = "bitflags" name = "bitflags"
version = "2.4.2" version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf" checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1"
[[package]] [[package]]
name = "bitvec" name = "bitvec"
@ -297,11 +297,9 @@ dependencies = [
"reqwest", "reqwest",
"rusqlite", "rusqlite",
"rust_decimal", "rust_decimal",
"rust_decimal_macros",
"rusty-money", "rusty-money",
"serde", "serde",
"serde_json", "serde_json",
"tokio",
] ]
[[package]] [[package]]
@ -390,6 +388,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78" checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78"
dependencies = [ dependencies = [
"futures-core", "futures-core",
"futures-sink",
] ]
[[package]] [[package]]
@ -398,6 +397,12 @@ version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d" checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d"
[[package]]
name = "futures-io"
version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1"
[[package]] [[package]]
name = "futures-sink" name = "futures-sink"
version = "0.3.30" version = "0.3.30"
@ -417,9 +422,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48"
dependencies = [ dependencies = [
"futures-core", "futures-core",
"futures-io",
"futures-sink",
"futures-task", "futures-task",
"memchr",
"pin-project-lite", "pin-project-lite",
"pin-utils", "pin-utils",
"slab",
] ]
[[package]] [[package]]
@ -441,9 +450,9 @@ checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253"
[[package]] [[package]]
name = "h2" name = "h2"
version = "0.3.25" version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fbd2820c5e49886948654ab546d0688ff24530286bdcf8fca3cefb16d4618eb" checksum = "51ee2dd2e4f378392eeff5d51618cd9a63166a2513846bbc55f21cfacd9199d4"
dependencies = [ dependencies = [
"bytes", "bytes",
"fnv", "fnv",
@ -500,9 +509,9 @@ checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024"
[[package]] [[package]]
name = "http" name = "http"
version = "0.2.12" version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "601cbb57e577e2f5ef5be8e7b83f0f63994f25aa94d673e54a92d5c516d101f1" checksum = "21b9ddb458710bc376481b842f5da65cdf31522de232c1ca8146abce2a358258"
dependencies = [ dependencies = [
"bytes", "bytes",
"fnv", "fnv",
@ -511,12 +520,24 @@ dependencies = [
[[package]] [[package]]
name = "http-body" name = "http-body"
version = "0.4.6" version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ceab25649e9960c0311ea418d17bee82c0dcec1bd053b5f9a66e265a693bed2" checksum = "1cac85db508abc24a2e48553ba12a996e87244a0395ce011e62b37158745d643"
dependencies = [ dependencies = [
"bytes", "bytes",
"http", "http",
]
[[package]]
name = "http-body-util"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0475f8b2ac86659c21b64320d5d653f9efe42acd2a4e560073ec61a155a34f1d"
dependencies = [
"bytes",
"futures-core",
"http",
"http-body",
"pin-project-lite", "pin-project-lite",
] ]
@ -526,47 +547,60 @@ version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d897f394bad6a705d5f4104762e116a75639e470d80901eed05a860a95cb1904" checksum = "d897f394bad6a705d5f4104762e116a75639e470d80901eed05a860a95cb1904"
[[package]]
name = "httpdate"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9"
[[package]] [[package]]
name = "hyper" name = "hyper"
version = "0.14.28" version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf96e135eb83a2a8ddf766e426a841d8ddd7449d5f00d34ea02b41d2f19eef80" checksum = "186548d73ac615b32a73aafe38fb4f56c0d340e110e5a200bcadbaf2e199263a"
dependencies = [ dependencies = [
"bytes", "bytes",
"futures-channel", "futures-channel",
"futures-core",
"futures-util", "futures-util",
"h2", "h2",
"http", "http",
"http-body", "http-body",
"httparse", "httparse",
"httpdate",
"itoa", "itoa",
"pin-project-lite", "pin-project-lite",
"socket2", "smallvec",
"tokio", "tokio",
"tower-service",
"tracing",
"want", "want",
] ]
[[package]] [[package]]
name = "hyper-tls" name = "hyper-tls"
version = "0.5.0" version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905" checksum = "70206fc6890eaca9fde8a0bf71caa2ddfc9fe045ac9e5c70df101a7dbde866e0"
dependencies = [ dependencies = [
"bytes", "bytes",
"http-body-util",
"hyper", "hyper",
"hyper-util",
"native-tls", "native-tls",
"tokio", "tokio",
"tokio-native-tls", "tokio-native-tls",
"tower-service",
]
[[package]]
name = "hyper-util"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca38ef113da30126bbff9cd1705f9273e15d45498615d138b0c20279ac7a76aa"
dependencies = [
"bytes",
"futures-channel",
"futures-util",
"http",
"http-body",
"hyper",
"pin-project-lite",
"socket2",
"tokio",
"tower",
"tower-service",
"tracing",
] ]
[[package]] [[package]]
@ -639,16 +673,6 @@ version = "0.4.13"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "01cda141df6706de531b6c46c3a33ecca755538219bd484262fa09410c13539c" checksum = "01cda141df6706de531b6c46c3a33ecca755538219bd484262fa09410c13539c"
[[package]]
name = "lock_api"
version = "0.4.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c168f8615b12bc01f9c17e2eb0cc07dcae1940121185446edc3744920e8ef45"
dependencies = [
"autocfg",
"scopeguard",
]
[[package]] [[package]]
name = "log" name = "log"
version = "0.4.21" version = "0.4.21"
@ -745,7 +769,7 @@ version = "0.10.64"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95a0481286a310808298130d22dd1fef0fa571e05a8f44ec801801e84b216b1f" checksum = "95a0481286a310808298130d22dd1fef0fa571e05a8f44ec801801e84b216b1f"
dependencies = [ dependencies = [
"bitflags 2.4.2", "bitflags 2.5.0",
"cfg-if", "cfg-if",
"foreign-types", "foreign-types",
"libc", "libc",
@ -783,35 +807,32 @@ dependencies = [
"vcpkg", "vcpkg",
] ]
[[package]]
name = "parking_lot"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f"
dependencies = [
"lock_api",
"parking_lot_core",
]
[[package]]
name = "parking_lot_core"
version = "0.9.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c42a9226546d68acdd9c0a280d17ce19bfe27a46bf68784e4066115788d008e"
dependencies = [
"cfg-if",
"libc",
"redox_syscall",
"smallvec",
"windows-targets 0.48.5",
]
[[package]] [[package]]
name = "percent-encoding" name = "percent-encoding"
version = "2.3.1" version = "2.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e"
[[package]]
name = "pin-project"
version = "1.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6bf43b791c5b9e34c3d182969b4abb522f9343702850a2e57f460d00d09b4b3"
dependencies = [
"pin-project-internal",
]
[[package]]
name = "pin-project-internal"
version = "1.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.53",
]
[[package]] [[package]]
name = "pin-project-lite" name = "pin-project-lite"
version = "0.2.13" version = "0.2.13"
@ -942,15 +963,6 @@ dependencies = [
"getrandom", "getrandom",
] ]
[[package]]
name = "redox_syscall"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa"
dependencies = [
"bitflags 1.3.2",
]
[[package]] [[package]]
name = "rend" name = "rend"
version = "0.4.2" version = "0.4.2"
@ -962,20 +974,23 @@ dependencies = [
[[package]] [[package]]
name = "reqwest" name = "reqwest"
version = "0.11.26" version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78bf93c4af7a8bb7d879d51cebe797356ff10ae8516ace542b5182d9dcac10b2" checksum = "58b48d98d932f4ee75e541614d32a7f44c889b72bd9c2e04d95edd135989df88"
dependencies = [ dependencies = [
"base64", "base64",
"bytes", "bytes",
"encoding_rs", "encoding_rs",
"futures-channel",
"futures-core", "futures-core",
"futures-util", "futures-util",
"h2", "h2",
"http", "http",
"http-body", "http-body",
"http-body-util",
"hyper", "hyper",
"hyper-tls", "hyper-tls",
"hyper-util",
"ipnet", "ipnet",
"js-sys", "js-sys",
"log", "log",
@ -1035,7 +1050,7 @@ version = "0.31.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b838eba278d213a8beaf485bd313fd580ca4505a00d5871caeb1457c55322cae" checksum = "b838eba278d213a8beaf485bd313fd580ca4505a00d5871caeb1457c55322cae"
dependencies = [ dependencies = [
"bitflags 2.4.2", "bitflags 2.5.0",
"fallible-iterator", "fallible-iterator",
"fallible-streaming-iterator", "fallible-streaming-iterator",
"hashlink", "hashlink",
@ -1077,11 +1092,11 @@ checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76"
[[package]] [[package]]
name = "rustix" name = "rustix"
version = "0.38.31" version = "0.38.32"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ea3e1a662af26cd7a3ba09c0297a31af215563ecf42817c98df621387f4e949" checksum = "65e04861e65f21776e67888bfbea442b3642beaa0138fdb1dd7a84a52dffdb89"
dependencies = [ dependencies = [
"bitflags 2.4.2", "bitflags 2.5.0",
"errno", "errno",
"libc", "libc",
"linux-raw-sys", "linux-raw-sys",
@ -1122,12 +1137,6 @@ dependencies = [
"windows-sys 0.52.0", "windows-sys 0.52.0",
] ]
[[package]]
name = "scopeguard"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]] [[package]]
name = "seahash" name = "seahash"
version = "4.1.0" version = "4.1.0"
@ -1200,15 +1209,6 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "signal-hook-registry"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d8229b473baa5980ac72ef434c4415e70c4b5e71b423043adb4ba059f89c99a1"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "simdutf8" name = "simdutf8"
version = "0.1.4" version = "0.1.4"
@ -1226,9 +1226,9 @@ dependencies = [
[[package]] [[package]]
name = "smallvec" name = "smallvec"
version = "1.13.1" version = "1.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6ecd384b10a64542d77071bd64bd7b231f4ed5940fba55e98c3de13824cf3d7" checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"
[[package]] [[package]]
name = "socket2" name = "socket2"
@ -1351,25 +1351,11 @@ dependencies = [
"libc", "libc",
"mio", "mio",
"num_cpus", "num_cpus",
"parking_lot",
"pin-project-lite", "pin-project-lite",
"signal-hook-registry",
"socket2", "socket2",
"tokio-macros",
"windows-sys 0.48.0", "windows-sys 0.48.0",
] ]
[[package]]
name = "tokio-macros"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.53",
]
[[package]] [[package]]
name = "tokio-native-tls" name = "tokio-native-tls"
version = "0.3.1" version = "0.3.1"
@ -1411,6 +1397,28 @@ dependencies = [
"winnow", "winnow",
] ]
[[package]]
name = "tower"
version = "0.4.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8fa9be0de6cf49e536ce1851f987bd21a43b771b09473c3549a6c853db37c1c"
dependencies = [
"futures-core",
"futures-util",
"pin-project",
"pin-project-lite",
"tokio",
"tower-layer",
"tower-service",
"tracing",
]
[[package]]
name = "tower-layer"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c20c8dbed6283a09604c3e69b4b7eeb54e298b8a600d4d5ecb5ad39de609f1d0"
[[package]] [[package]]
name = "tower-service" name = "tower-service"
version = "0.3.2" version = "0.3.2"
@ -1423,6 +1431,7 @@ version = "0.1.40"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef"
dependencies = [ dependencies = [
"log",
"pin-project-lite", "pin-project-lite",
"tracing-core", "tracing-core",
] ]
@ -1482,9 +1491,9 @@ checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
[[package]] [[package]]
name = "uuid" name = "uuid"
version = "1.7.0" version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f00cc9702ca12d3c81455259621e676d0f7251cec66a21e98fe2e9a37db93b2a" checksum = "a183cf7feeba97b4dd1c0d46788634f6221d87fa961b305bed08c851829efcc0"
[[package]] [[package]]
name = "vcpkg" name = "vcpkg"

View File

@ -7,11 +7,9 @@ edition = "2021"
[dependencies] [dependencies]
clap = { version = "4.5.2", features = [ "derive" ] } clap = { version = "4.5.2", features = [ "derive" ] }
reqwest = "0.11.25" reqwest = { version = "0.12.0", features = [ "blocking" ] }
rusqlite = { version = "0.31.0", features = ["bundled", "array"] } rusqlite = { version = "0.31.0", features = ["bundled", "array"] }
rusty-money = "0.4.1" rusty-money = "0.4.1"
serde = { version ="1.0.197", features = ["derive"] } serde = { version ="1.0.197", features = ["derive"] }
serde_json = "1.0.114" serde_json = "1.0.114"
tokio = { version = "1.36.0", features = ["full"] }
rust_decimal = "1.34" rust_decimal = "1.34"
rust_decimal_macros = "1.34"

View File

@ -1,12 +1,11 @@
use crate::*; use crate::*;
use rust_decimal::prelude::*; use rust_decimal::prelude::*;
use rusty_money::{iso::find, ExchangeRate, Money}; 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") if cache::get_next_update(code).expect("Error getting next update time from cache")
<= config::get_current_time() <= config::get_current_time()
{ {
let status = requests::get_rates(code) let status = requests::get_rates(code)
.await
.expect("Error while fetching rates"); .expect("Error while fetching rates");
if status == requests::Status::INVALID { if status == requests::Status::INVALID {
panic!("Invalid api key when getting rates") 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") { if !cache::check_code(code_from).expect("Error on getting code status") {
panic!("Code {} doesn't exists, use correct code!", code_from); 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") || (cache::get_next_update(code_from).expect("Error getting next update time from cache")
<= config::get_current_time()) <= 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") 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() { if value.parse::<f64>().is_err() {
panic!("{} is not a number!", value); 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); let from_currency = find(code_from);
if from_currency.is_none() { if from_currency.is_none() {
panic!("{} not found in ISO formats", code_from); panic!("{} not found in ISO formats", code_from);

View File

@ -45,9 +45,9 @@ struct Cli {
#[arg(short = 'L', long = "list-rates", value_names = ["currency"])] #[arg(short = 'L', long = "list-rates", value_names = ["currency"])]
list_rates: Option<String>, 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)?; set_api_key(key)?;
let status = get_currencies().await?; let status = get_currencies()?;
if status == requests::Status::INVALID { if status == requests::Status::INVALID {
set_api_key("".to_string())?; set_api_key("".to_string())?;
println!("Api Key is invalid"); println!("Api Key is invalid");
@ -64,8 +64,7 @@ async fn setup_key(key: String) -> Result<bool, Box<dyn std::error::Error>> {
Ok(true) Ok(true)
} }
#[tokio::main] fn main() -> Result<ExitCode, Box<dyn std::error::Error>> {
async fn main() -> Result<ExitCode, Box<dyn std::error::Error>> {
let args = Cli::parse(); let args = Cli::parse();
let all_args = let all_args =
args.currency_from.is_some() && args.currency_to.is_some() && args.value.is_some(); 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 => {} None => {}
Some(key) => { Some(key) => {
let res = setup_key(key) let res = setup_key(key)
.await
.expect("Unknown error while setting up key"); .expect("Unknown error while setting up key");
if !res { if !res {
return Ok(ExitCode::FAILURE); return Ok(ExitCode::FAILURE);
@ -125,7 +123,7 @@ async fn main() -> Result<ExitCode, Box<dyn std::error::Error>> {
println!("Code {} not found", code); println!("Code {} not found", code);
return Ok(ExitCode::FAILURE); return Ok(ExitCode::FAILURE);
} }
exchange::update_rate(&code).await; exchange::update_rate(&code);
let rates = cache::list_rates(&code)?; let rates = cache::list_rates(&code)?;
for rate in rates { for rate in rates {
println!("{} to {} rate: {}", code, rate[0], rate[1]); 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.currency_to.unwrap().to_uppercase(),
&args.value.unwrap(), &args.value.unwrap(),
) )
.await
} }
} else { } else {
interactive().await?; interactive()?;
} }
Ok(ExitCode::SUCCESS) 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() let mut key_setup = cache::get_api_key()
.expect("Error while getting api key") .expect("Error while getting api key")
.len() .len()
@ -159,7 +156,6 @@ async fn interactive() -> Result<(), Box<dyn std::error::Error>> {
.read_line(&mut key_string) .read_line(&mut key_string)
.expect("Did not enter a correct string"); .expect("Did not enter a correct string");
setup_key(key_string.trim().to_string()) setup_key(key_string.trim().to_string())
.await
.expect("Unknown error while setting up key"); .expect("Unknown error while setting up key");
key_setup = cache::get_api_key() key_setup = cache::get_api_key()
.expect("Error while getting api key") .expect("Error while getting api key")
@ -216,7 +212,7 @@ async fn interactive() -> Result<(), Box<dyn std::error::Error>> {
amount_check = true amount_check = true
} }
} }
convert_value(&code_from, &code_to, &amount).await; convert_value(&code_from, &code_to, &amount);
Ok(()) Ok(())
} }

View File

@ -29,18 +29,17 @@ struct Err {
error_type: String, error_type: String,
} }
pub async fn get_rates(code: &String) -> Result<Status, reqwest::Error> { pub fn get_rates(code: &String) -> Result<Status, reqwest::Error> {
let response = reqwest::get(format!( let response = reqwest::blocking::get(format!(
"{}{}{}{}", "{}{}{}{}",
get_endpoint(), get_endpoint(),
get_api_key().expect("Error when getting api key from cache"), get_api_key().expect("Error when getting api key from cache"),
"/latest/", "/latest/",
code.to_uppercase() code.to_uppercase()
)) ))?;
.await?;
if response.status().is_success() { if response.status().is_success() {
let response: ConversionRates = 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( cache::add_rates(
response.time_next_update_unix, response.time_next_update_unix,
&response.base_code, &response.base_code,
@ -50,7 +49,7 @@ pub async fn get_rates(code: &String) -> Result<Status, reqwest::Error> {
return Ok(Status::OK); return Ok(Status::OK);
} else { } else {
let err: Err = 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" { if err.error_type == "invalid-key" {
return Ok(Status::INVALID); return Ok(Status::INVALID);
} else if err.error_type == "quota-reached" { } 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) Ok(Status::ERROR)
} }
pub async fn get_currencies() -> Result<Status, reqwest::Error> { pub fn get_currencies() -> Result<Status, reqwest::Error> {
let response = reqwest::get(format!( let response = reqwest::blocking::get(format!(
"{}{}{}", "{}{}{}",
get_endpoint(), get_endpoint(),
get_api_key().expect("Error when getting api key from cache"), get_api_key().expect("Error when getting api key from cache"),
"/codes" "/codes"
)) ))?;
.await?;
if response.status().is_success() { if response.status().is_success() {
let codes: CurrencyCodes = 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 { for code in codes.supported_codes {
cache::add_code(code).expect("Error when adding code to cache"); cache::add_code(code).expect("Error when adding code to cache");
} }
return Ok(Status::OK); return Ok(Status::OK);
} else { } else {
let err: Err = 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" { if err.error_type == "invalid-key" {
return Ok(Status::INVALID); return Ok(Status::INVALID);
} else if err.error_type == "quota-reached" { } else if err.error_type == "quota-reached" {