Files
brk/crates/brk_client/examples/basic.rs
2026-02-23 17:22:12 +01:00

74 lines
1.8 KiB
Rust

//! Basic example of using the BRK client.
use brk_client::{BrkClient, BrkClientOptions};
use brk_types::{FormatResponse, Index, Metric};
fn main() -> brk_client::Result<()> {
// Create client with default options
let client = BrkClient::new("http://localhost:3110");
// Or with custom options
let _client_with_options = BrkClient::with_options(BrkClientOptions {
base_url: "http://localhost:3110".to_string(),
timeout_secs: 60,
});
// Fetch price data using the typed metrics API
// Using new idiomatic API: last(3).fetch()
let price_close = client
.metrics()
.prices
.usd
.split
.close
.by
.day1()
.last(3)
.fetch()?;
println!("Last 3 price close values: {:?}", price_close);
// Fetch block data
let block_count = client
.metrics()
.blocks
.count
.block_count
.sum
.by
.day1()
.last(3)
.fetch()?;
println!("Last 3 block count values: {:?}", block_count);
// Fetch supply data
dbg!(client.metrics().supply.circulating.btc.by.day1().path());
let circulating = client
.metrics()
.supply
.circulating
.btc
.by
.day1()
.last(3)
.fetch_csv()?;
println!("Last 3 circulating supply values: {:?}", circulating);
// Using generic metric fetching
let metricdata = client.get_metric(
Metric::from("price_close"),
Index::Day1,
Some(-3),
None,
None,
None,
)?;
match metricdata {
FormatResponse::Json(m) => {
println!("Generic fetch result count: {}", m.data.len());
}
FormatResponse::Csv(_) => panic!(),
};
Ok(())
}