Changed reqwest for ureq

Seemed much smaller and more what I needed
This commit is contained in:
2024-07-13 21:15:10 +01:00
parent f791c0743a
commit d820929e68
3 changed files with 70 additions and 890 deletions

892
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -8,5 +8,5 @@ edition = "2021"
[dependencies] [dependencies]
chrono = "0.4.38" chrono = "0.4.38"
lazy_static = "1.5.0" lazy_static = "1.5.0"
reqwest = { version = "0.12.5", features = ["json", "blocking"] }
serde_json = "1.0.120" serde_json = "1.0.120"
ureq = { version = "2.10.0", features = ["json"] }

View File

@@ -1,10 +1,9 @@
use chrono::prelude::*; use chrono::prelude::*;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use std::convert::Into;
use std::sync::RwLock;
use reqwest;
use serde_json; use serde_json;
use std::collections::HashMap; use std::collections::HashMap;
use std::convert::Into;
use std::sync::RwLock;
// TODO review whether a genesis block has a block height of 1 or 0 // TODO review whether a genesis block has a block height of 1 or 0
@@ -12,10 +11,9 @@ pub enum BlockchainSupported {
BTC, BTC,
BSV, BSV,
XMR, XMR,
ETH ETH,
} }
type BlockHeight = i64; type BlockHeight = i64;
#[derive(Clone, Copy, PartialEq, Debug)] #[derive(Clone, Copy, PartialEq, Debug)]
@@ -75,11 +73,9 @@ pub fn time_to_blockheight(time: DateTime<Utc>, bi: BlockchainInfo) -> BlockHeig
pub fn update_bsv_blockheight() { pub fn update_bsv_blockheight() {
let whatsonchain_url = "https://api.whatsonchain.com/v1/bsv/main/block/headers"; let whatsonchain_url = "https://api.whatsonchain.com/v1/bsv/main/block/headers";
match reqwest::blocking::get(whatsonchain_url) { match ureq::get(whatsonchain_url).call() {
Ok(resp) => { Ok(resp) => {
let json = resp.json::<Vec<HashMap<String, serde_json::Value>>>(); let json: serde_json::Value = resp.into_json().unwrap();
match json {
Ok(json) => {
let height = json[0].get("height").unwrap().as_i64().unwrap(); let height = json[0].get("height").unwrap().as_i64().unwrap();
// I am presuming that "time" is when WhatsOnChain saw the block rather than the // I am presuming that "time" is when WhatsOnChain saw the block rather than the
@@ -99,19 +95,12 @@ pub fn update_bsv_blockheight() {
} }
} }
} }
Err(err) => {
println!("Err: {}", err);
}
}
}
pub fn update_btc_blockheight() { pub fn update_btc_blockheight() {
let blockcypher_url = "https://api.blockcypher.com/v1/btc/main"; let blockcypher_url = "https://api.blockcypher.com/v1/btc/main";
match reqwest::blocking::get(blockcypher_url) { match ureq::get(blockcypher_url).call() {
Ok(resp) => { Ok(resp) => {
let json = resp.json::<HashMap<String, serde_json::Value>>(); let json: serde_json::Value = resp.into_json().unwrap();
match json {
Ok(json) => {
let height = json.get("height").unwrap().as_i64().unwrap(); let height = json.get("height").unwrap().as_i64().unwrap();
let time = json.get("time").unwrap().as_str().unwrap(); let time = json.get("time").unwrap().as_str().unwrap();
let datetime = time.parse::<DateTime<Utc>>().unwrap(); let datetime = time.parse::<DateTime<Utc>>().unwrap();
@@ -126,11 +115,6 @@ pub fn update_btc_blockheight() {
} }
} }
} }
Err(err) => {
println!("Err: {}", err);
}
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {