#![doc = include_str!("../README.md")]
#![warn(missing_docs)]
#![warn(rustdoc::bare_urls)]
use std::sync::Arc;
use anyhow::Result;
use reqwest::Url;
use tokio::sync::Mutex;
pub mod api;
#[derive(Clone)]
pub struct LNBitsClient {
admin_key: String,
invoice_read_key: String,
lnbits_url: Url,
reqwest_client: reqwest::Client,
receiver: Arc<Mutex<tokio::sync::mpsc::Receiver<String>>>,
}
impl LNBitsClient {
pub fn new(
_wallet_id: &str,
admin_key: &str,
invoice_read_key: &str,
lnbits_url: &str,
tor_socket: Option<&str>,
) -> Result<LNBitsClient> {
let lnbits_url = Url::parse(lnbits_url)?;
let client = {
if let Some(tor_socket) = tor_socket {
let proxy = reqwest::Proxy::all(tor_socket).expect("tor proxy should be there");
reqwest::Client::builder().proxy(proxy).build()?
} else {
reqwest::Client::builder().build()?
}
};
let (_sender, receiver) = tokio::sync::mpsc::channel(8);
Ok(LNBitsClient {
admin_key: admin_key.to_string(),
invoice_read_key: invoice_read_key.to_string(),
lnbits_url,
reqwest_client: client,
receiver: Arc::new(Mutex::new(receiver)),
})
}
pub fn receiver(&self) -> Arc<Mutex<tokio::sync::mpsc::Receiver<String>>> {
self.receiver.clone()
}
}