lnbits_rs/api/
mod.rs

1//! LNbits api
2
3use std::fmt;
4
5use anyhow::{bail, Result};
6
7pub mod invoice;
8pub mod payment;
9pub mod wallet;
10pub mod websocket;
11
12/// LNbits api key type
13#[derive(Debug, Clone, Hash)]
14pub enum LNBitsRequestKey {
15    /// Admin api key
16    Admin,
17    /// Read invoice api key
18    InvoiceRead,
19}
20
21/// LNbits endpoints
22#[derive(Debug, Clone, Hash)]
23pub enum LNBitsEndpoint {
24    /// Payments endpoint
25    Payments,
26    /// Decode payments endpoint
27    PaymentsDecode,
28    /// Payments endpoint with hash
29    PaymentHash(String),
30    /// Wallet info endpoint
31    Wallet,
32}
33
34impl fmt::Display for LNBitsEndpoint {
35    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36        match self {
37            LNBitsEndpoint::Payments => write!(f, "api/v1/payments"),
38            LNBitsEndpoint::PaymentsDecode => write!(f, "api/v1/payments/decode"),
39            LNBitsEndpoint::PaymentHash(hash) => write!(f, "api/v1/payments/{}", hash),
40            LNBitsEndpoint::Wallet => write!(f, "api/v1/wallet"),
41        }
42    }
43}
44
45impl crate::LNBitsClient {
46    /// Make get request
47    pub async fn make_get(
48        &self,
49        endpoint: LNBitsEndpoint,
50        key: LNBitsRequestKey,
51    ) -> Result<String> {
52        let url = self.lnbits_url.join(&endpoint.to_string())?;
53        let response = self
54            .reqwest_client
55            .get(url)
56            .header("x-api-key", {
57                match key {
58                    LNBitsRequestKey::Admin => self.admin_key.clone(),
59                    LNBitsRequestKey::InvoiceRead => self.invoice_read_key.clone(),
60                }
61            })
62            .send()
63            .await?;
64
65        if response.status() == reqwest::StatusCode::NOT_FOUND {
66            println!("{:?}", response);
67            bail!("Not found")
68        }
69
70        let body = response.text().await?;
71
72        Ok(body)
73    }
74
75    /// Make post request
76    pub async fn make_post(
77        &self,
78        endpoint: LNBitsEndpoint,
79        key: LNBitsRequestKey,
80        body: &str,
81    ) -> Result<String> {
82        let url = self.lnbits_url.join(&endpoint.to_string())?;
83        let response = self
84            .reqwest_client
85            .post(url)
86            .header("X-Api-Key", {
87                match key {
88                    LNBitsRequestKey::Admin => self.admin_key.clone(),
89                    LNBitsRequestKey::InvoiceRead => self.invoice_read_key.clone(),
90                }
91            })
92            .body(body.to_string())
93            .send()
94            .await?;
95
96        if response.status() == reqwest::StatusCode::NOT_FOUND {
97            println!("{:?}", response);
98            println!("{:?}", response.text().await.unwrap());
99            bail!("Not Found")
100        }
101
102        if response.status() == reqwest::StatusCode::UNAUTHORIZED {
103            bail!("Unauthorized")
104        }
105
106        let body = response.text().await?;
107
108        Ok(body)
109    }
110}