1#![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#[derive(Debug, Clone)]
24pub struct Phoenixd {
25 api_password: String,
26 api_url: Url,
27 client: Client,
28}
29
30#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize, Serialize)]
32#[serde(rename_all = "UPPERCASE")]
33pub enum InvoiceState {
34 Completed,
36 Paid,
38 Unpaid,
40 Pending,
42}
43
44impl Phoenixd {
45 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}