phoenixd_rs/
lib.rs

1//! Phoenix API SDK
2//! Rust SDK for <https://phoenix.acinq.co>
3#![doc = include_str!("../README.md")]
4#![warn(missing_docs)]
5#![warn(rustdoc::bare_urls)]
6
7use std::str::FromStr;
8
9use reqwest::{Client, IntoUrl, Url};
10use serde::{Deserialize, Serialize};
11use serde_json::Value;
12
13mod error;
14pub mod invoice;
15pub mod pay_ln;
16pub mod webhooks;
17
18pub use error::Error;
19pub use invoice::*;
20pub use pay_ln::*;
21
22/// Phoenixd
23#[derive(Debug, Clone)]
24pub struct Phoenixd {
25    api_password: String,
26    api_url: Url,
27    client: Client,
28}
29
30/// Invoice state
31#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize, Serialize)]
32#[serde(rename_all = "UPPERCASE")]
33pub enum InvoiceState {
34    /// Payment Completed
35    Completed,
36    /// Invoice paid
37    Paid,
38    /// Invoice unpaid
39    Unpaid,
40    /// Invoice pending
41    Pending,
42}
43
44impl Phoenixd {
45    /// Create Strike client
46    /// # Arguments
47    /// * `api_password` - Phoenixd api password
48    /// * `url` - Optional Url of nodeless api
49    ///
50    /// # Example
51    /// ```
52    /// use phoenixd_rs::Phoenixd;
53    /// let client = Phoenixd::new("xxxxxxxxxxx", "https://test.com").unwrap();
54    /// ```
55    pub fn new(api_password: &str, api_url: &str) -> anyhow::Result<Self> {
56        let client = reqwest::Client::builder().build()?;
57        let api_url = Url::from_str(api_url)?;
58
59        Ok(Self {
60            api_password: api_password.to_string(),
61            api_url,
62            client,
63        })
64    }
65
66    async fn make_get<U>(&self, url: U) -> Result<Value, Error>
67    where
68        U: IntoUrl,
69    {
70        Ok(self
71            .client
72            .get(url)
73            .basic_auth("", Some(&self.api_password))
74            .send()
75            .await?
76            .json::<Value>()
77            .await?)
78    }
79
80    async fn make_post<U, T>(&self, url: U, data: Option<T>) -> anyhow::Result<Value>
81    where
82        U: IntoUrl,
83        T: Serialize,
84    {
85        let value = match data {
86            Some(data) => {
87                self.client
88                    .post(url)
89                    .basic_auth("", Some(&self.api_password))
90                    .form(&data)
91                    .send()
92                    .await?
93                    .json::<Value>()
94                    .await?
95            }
96            None => {
97                self.client
98                    .post(url)
99                    .basic_auth("", Some(&self.api_password))
100                    .send()
101                    .await?
102                    .json::<Value>()
103                    .await?
104            }
105        };
106        Ok(value)
107    }
108}