#![allow(dead_code)]
use color_eyre::eyre::{WrapErr, eyre};
use crate::config::NodeConfig;
pub const BEE_TUI_USER_AGENT: &str = concat!("bee-tui/", env!("CARGO_PKG_VERSION"));
#[derive(Clone, Debug)]
pub struct ApiClient {
pub name: String,
pub url: String,
pub authenticated: bool,
inner: bee::Client,
}
impl ApiClient {
pub fn from_node(node: &NodeConfig) -> color_eyre::Result<Self> {
let url = node.url.trim_end_matches('/').to_string();
let token = node.resolved_token();
let authenticated = token.is_some();
let mut http_builder = reqwest::Client::builder().user_agent(BEE_TUI_USER_AGENT);
if let Some(t) = token.as_deref() {
let mut headers = reqwest::header::HeaderMap::new();
let value = reqwest::header::HeaderValue::from_str(&format!("Bearer {t}"))
.map_err(|e| eyre!("invalid bearer token: {e}"))?;
headers.insert(reqwest::header::AUTHORIZATION, value);
http_builder = http_builder.default_headers(headers);
}
let http = http_builder
.build()
.map_err(|e| eyre!("failed to build http client: {e}"))?;
let inner = bee::Client::with_http_client(&url, http)
.map_err(|e| eyre!("invalid bee endpoint: {e}"))?;
Ok(Self {
name: node.name.clone(),
url,
authenticated,
inner,
})
}
pub fn bee(&self) -> &bee::Client {
&self.inner
}
pub async fn ping(&self) -> color_eyre::Result<std::time::Duration> {
self.inner
.ping()
.await
.wrap_err_with(|| format!("ping {} ({}) failed", self.name, self.url))
}
}
#[cfg(test)]
mod tests {
use super::*;
fn node(url: &str, token: Option<&str>) -> NodeConfig {
NodeConfig {
name: "test".into(),
url: url.into(),
token: token.map(String::from),
default: false,
}
}
#[test]
fn from_node_strips_trailing_slash() {
let c = ApiClient::from_node(&node("http://localhost:1633/", None)).unwrap();
assert_eq!(c.url, "http://localhost:1633");
assert!(!c.authenticated);
}
#[test]
fn from_node_with_token_marks_authenticated() {
let c = ApiClient::from_node(&node("http://localhost:1633", Some("dummy-jwt"))).unwrap();
assert!(c.authenticated);
}
#[test]
fn from_node_rejects_bogus_url() {
let err = ApiClient::from_node(&node("not a url", None)).unwrap_err();
assert!(format!("{err}").contains("invalid"));
}
}