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]
chrono = "0.4.38"
lazy_static = "1.5.0"
reqwest = { version = "0.12.5", features = ["json", "blocking"] }
serde_json = "1.0.120"
ureq = { version = "2.10.0", features = ["json"] }

View File

@@ -1,10 +1,9 @@
use chrono::prelude::*;
use lazy_static::lazy_static;
use std::convert::Into;
use std::sync::RwLock;
use reqwest;
use serde_json;
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
@@ -12,10 +11,9 @@ pub enum BlockchainSupported {
BTC,
BSV,
XMR,
ETH
ETH,
}
type BlockHeight = i64;
#[derive(Clone, Copy, PartialEq, Debug)]
@@ -75,28 +73,21 @@ pub fn time_to_blockheight(time: DateTime<Utc>, bi: BlockchainInfo) -> BlockHeig
pub fn update_bsv_blockheight() {
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) => {
let json = resp.json::<Vec<HashMap<String, serde_json::Value>>>();
match json {
Ok(json) => {
let height = json[0].get("height").unwrap().as_i64().unwrap();
let json: serde_json::Value = resp.into_json().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
// timestamp in the block. I could be very wrong about that though... As I understand
// the timestamp in the block has no garuntee of anything other than maybe it's more
// than the last one.
let unixtime = json[0].get("time").unwrap().as_i64().unwrap();
let datetime = Utc.timestamp_opt(unixtime, 0).unwrap();
{
let mut bi = BSV_BI.write().unwrap();
bi.latest_block_height = height;
bi.latest_block_time = datetime;
}
}
Err(err) => {
println!("Err: {}", err);
}
// I am presuming that "time" is when WhatsOnChain saw the block rather than the
// timestamp in the block. I could be very wrong about that though... As I understand
// the timestamp in the block has no garuntee of anything other than maybe it's more
// than the last one.
let unixtime = json[0].get("time").unwrap().as_i64().unwrap();
let datetime = Utc.timestamp_opt(unixtime, 0).unwrap();
{
let mut bi = BSV_BI.write().unwrap();
bi.latest_block_height = height;
bi.latest_block_time = datetime;
}
}
Err(err) => {
@@ -107,23 +98,16 @@ pub fn update_bsv_blockheight() {
pub fn update_btc_blockheight() {
let blockcypher_url = "https://api.blockcypher.com/v1/btc/main";
match reqwest::blocking::get(blockcypher_url) {
match ureq::get(blockcypher_url).call() {
Ok(resp) => {
let json = resp.json::<HashMap<String, serde_json::Value>>();
match json {
Ok(json) => {
let height = json.get("height").unwrap().as_i64().unwrap();
let time = json.get("time").unwrap().as_str().unwrap();
let datetime = time.parse::<DateTime<Utc>>().unwrap();
{
let mut bi = BTC_BI.write().unwrap();
bi.latest_block_height = height;
bi.latest_block_time = datetime;
}
}
Err(err) => {
println!("Err: {}", err);
}
let json: serde_json::Value = resp.into_json().unwrap();
let height = json.get("height").unwrap().as_i64().unwrap();
let time = json.get("time").unwrap().as_str().unwrap();
let datetime = time.parse::<DateTime<Utc>>().unwrap();
{
let mut bi = BTC_BI.write().unwrap();
bi.latest_block_height = height;
bi.latest_block_time = datetime;
}
}
Err(err) => {