mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-04-24 06:39:58 -07:00
global: snapshot
This commit is contained in:
@@ -11,7 +11,7 @@ use serde_json::Value;
|
||||
use tracing::info;
|
||||
|
||||
use crate::{
|
||||
PriceSource, default_retry,
|
||||
PriceSource, check_response, default_retry,
|
||||
ohlc::{compute_ohlc_from_range, date_from_timestamp, ohlc_from_array, timestamp_from_ms},
|
||||
};
|
||||
|
||||
@@ -73,8 +73,8 @@ impl Binance {
|
||||
default_retry(|_| {
|
||||
let url = Self::url("interval=1m&limit=1000");
|
||||
info!("Fetching {url} ...");
|
||||
let json: Value =
|
||||
serde_json::from_slice(minreq::get(url).with_timeout(30).send()?.as_bytes())?;
|
||||
let bytes = check_response(minreq::get(&url).with_timeout(30).send()?, &url)?;
|
||||
let json: Value = serde_json::from_slice(&bytes)?;
|
||||
Self::parse_ohlc_array(&json)
|
||||
})
|
||||
}
|
||||
@@ -96,8 +96,8 @@ impl Binance {
|
||||
default_retry(|_| {
|
||||
let url = Self::url("interval=1d");
|
||||
info!("Fetching {url} ...");
|
||||
let json: Value =
|
||||
serde_json::from_slice(minreq::get(url).with_timeout(30).send()?.as_bytes())?;
|
||||
let bytes = check_response(minreq::get(&url).with_timeout(30).send()?, &url)?;
|
||||
let json: Value = serde_json::from_slice(&bytes)?;
|
||||
Self::parse_date_ohlc_array(&json)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ use brk_types::{
|
||||
use serde_json::Value;
|
||||
use tracing::info;
|
||||
|
||||
use crate::{PriceSource, default_retry};
|
||||
use crate::{PriceSource, check_response, default_retry};
|
||||
|
||||
#[derive(Default, Clone)]
|
||||
#[allow(clippy::upper_case_acronyms)]
|
||||
@@ -49,8 +49,8 @@ impl BRK {
|
||||
);
|
||||
info!("Fetching {url} ...");
|
||||
|
||||
let body: Value =
|
||||
serde_json::from_slice(minreq::get(url).with_timeout(30).send()?.as_bytes())?;
|
||||
let bytes = check_response(minreq::get(&url).with_timeout(30).send()?, &url)?;
|
||||
let body: Value = serde_json::from_slice(&bytes)?;
|
||||
|
||||
body.as_array()
|
||||
.ok_or(Error::Parse("Expected JSON array".into()))?
|
||||
@@ -90,8 +90,8 @@ impl BRK {
|
||||
);
|
||||
info!("Fetching {url}...");
|
||||
|
||||
let body: Value =
|
||||
serde_json::from_slice(minreq::get(url).with_timeout(30).send()?.as_bytes())?;
|
||||
let bytes = check_response(minreq::get(&url).with_timeout(30).send()?, &url)?;
|
||||
let body: Value = serde_json::from_slice(&bytes)?;
|
||||
|
||||
body.as_array()
|
||||
.ok_or(Error::Parse("Expected JSON array".into()))?
|
||||
|
||||
@@ -6,7 +6,7 @@ use serde_json::Value;
|
||||
use tracing::info;
|
||||
|
||||
use crate::{
|
||||
PriceSource, default_retry,
|
||||
PriceSource, check_response, default_retry,
|
||||
ohlc::{compute_ohlc_from_range, date_from_timestamp, ohlc_from_array, timestamp_from_secs},
|
||||
};
|
||||
|
||||
@@ -39,8 +39,8 @@ impl Kraken {
|
||||
default_retry(|_| {
|
||||
let url = Self::url(1);
|
||||
info!("Fetching {url} ...");
|
||||
let json: Value =
|
||||
serde_json::from_slice(minreq::get(url).with_timeout(30).send()?.as_bytes())?;
|
||||
let bytes = check_response(minreq::get(&url).with_timeout(30).send()?, &url)?;
|
||||
let json: Value = serde_json::from_slice(&bytes)?;
|
||||
Self::parse_ohlc_response(&json)
|
||||
})
|
||||
}
|
||||
@@ -61,8 +61,8 @@ impl Kraken {
|
||||
default_retry(|_| {
|
||||
let url = Self::url(1440);
|
||||
info!("Fetching {url} ...");
|
||||
let json: Value =
|
||||
serde_json::from_slice(minreq::get(url).with_timeout(30).send()?.as_bytes())?;
|
||||
let bytes = check_response(minreq::get(&url).with_timeout(30).send()?, &url)?;
|
||||
let json: Value = serde_json::from_slice(&bytes)?;
|
||||
Self::parse_date_ohlc_response(&json)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -22,6 +22,19 @@ pub use source::{PriceSource, TrackedSource};
|
||||
|
||||
const MAX_RETRIES: usize = 12 * 60; // 12 hours of retrying
|
||||
|
||||
/// Check HTTP response status and return bytes or error
|
||||
pub fn check_response(response: minreq::Response, url: &str) -> Result<Vec<u8>> {
|
||||
let status = response.status_code as u16;
|
||||
if (200..300).contains(&status) {
|
||||
Ok(response.into_bytes())
|
||||
} else {
|
||||
Err(Error::HttpStatus {
|
||||
status,
|
||||
url: url.to_string(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Fetcher {
|
||||
pub binance: Option<TrackedSource<Binance>>,
|
||||
|
||||
@@ -98,8 +98,8 @@ impl<T: PriceSource> TrackedSource<T> {
|
||||
self.name(),
|
||||
self.cooldown.as_secs()
|
||||
);
|
||||
self.unhealthy_since = Some(Instant::now());
|
||||
}
|
||||
self.unhealthy_since = Some(Instant::now());
|
||||
}
|
||||
Err(_) => {} // Transient - no change
|
||||
}
|
||||
@@ -137,5 +137,6 @@ impl<T: PriceSource> PriceSource for TrackedSource<T> {
|
||||
|
||||
fn clear(&mut self) {
|
||||
self.source.clear();
|
||||
self.reset_health();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user