lnurlkit/
client.rs

1#[derive(Clone, Default)]
2pub struct Client(reqwest::Client);
3
4impl Client {
5    /// # Errors
6    ///
7    /// Returns errors on network or deserialization failures.
8    pub async fn entrypoint(&self, s: &str) -> Result<Entrypoint, &'static str> {
9        let client = &self.0;
10
11        let url = match crate::resolve(s)? {
12            crate::Resolved::Url(url) => url,
13            crate::Resolved::Auth(_, core) => return Ok(Entrypoint::Auth(Auth { client, core })),
14            crate::Resolved::Withdraw(_, core) => {
15                return Ok(Entrypoint::Withdraw(Withdraw { client, core }))
16            }
17        };
18
19        let response = client.get(url).send().await.map_err(|_| "request failed")?;
20        let bytes = response.bytes().await.map_err(|_| "body failed")?;
21
22        (&bytes as &[u8])
23            .try_into()
24            .map_err(|_| "parse failed")
25            .map(|query: crate::Entrypoint| match query {
26                crate::Entrypoint::Channel(core) => Entrypoint::Channel(Channel { client, core }),
27                crate::Entrypoint::Pay(core) => Entrypoint::Pay(Pay { client, core }),
28                crate::Entrypoint::Withdraw(core) => {
29                    Entrypoint::Withdraw(Withdraw { client, core })
30                }
31            })
32    }
33}
34
35#[derive(Clone, Debug)]
36pub enum Entrypoint<'a> {
37    Auth(Auth<'a>),
38    Channel(Channel<'a>),
39    Pay(Pay<'a>),
40    Withdraw(Withdraw<'a>),
41}
42
43#[derive(Clone, Debug)]
44pub struct Auth<'a> {
45    client: &'a reqwest::Client,
46    pub core: crate::auth::Entrypoint,
47}
48
49#[derive(Clone, Debug)]
50pub struct Channel<'a> {
51    client: &'a reqwest::Client,
52    pub core: crate::channel::client::Entrypoint,
53}
54
55#[derive(Clone, Debug)]
56pub struct Pay<'a> {
57    client: &'a reqwest::Client,
58    pub core: Box<crate::pay::client::Entrypoint>,
59}
60
61#[derive(Clone, Debug)]
62pub struct Withdraw<'a> {
63    client: &'a reqwest::Client,
64    pub core: crate::withdraw::client::Entrypoint,
65}
66
67impl Auth<'_> {
68    /// # Errors
69    ///
70    /// Returns errors on network or deserialization failures.
71    pub async fn auth(
72        &self,
73        key: &str,
74        sig: &[u8; 64],
75    ) -> Result<crate::CallbackResponse, &'static str> {
76        let callback = self.core.auth(key, sig);
77
78        let response = self
79            .client
80            .get(callback.to_string())
81            .send()
82            .await
83            .map_err(|_| "request failed")?;
84
85        let bytes = response.bytes().await.map_err(|_| "body failed")?;
86        (&bytes as &[u8]).try_into().map_err(|_| "parse failed")
87    }
88}
89
90impl Channel<'_> {
91    /// # Errors
92    ///
93    /// Returns errors on network or deserialization failures.
94    pub async fn accept(
95        &self,
96        remoteid: &str,
97        private: bool,
98    ) -> Result<crate::CallbackResponse, &'static str> {
99        let callback = self.core.accept(remoteid, private);
100
101        let response = self
102            .client
103            .get(callback.to_string())
104            .send()
105            .await
106            .map_err(|_| "request failed")?;
107
108        let bytes = response.bytes().await.map_err(|_| "body failed")?;
109        (&bytes as &[u8]).try_into().map_err(|_| "parse failed")
110    }
111
112    /// # Errors
113    ///
114    /// Returns errors on network or deserialization failures.
115    pub async fn cancel(&self, remoteid: &str) -> Result<crate::CallbackResponse, &'static str> {
116        let callback = self.core.cancel(remoteid);
117
118        let response = self
119            .client
120            .get(callback.to_string())
121            .send()
122            .await
123            .map_err(|_| "request failed")?;
124
125        let bytes = response.bytes().await.map_err(|_| "body failed")?;
126        (&bytes as &[u8]).try_into().map_err(|_| "parse failed")
127    }
128}
129
130impl Pay<'_> {
131    /// # Errors
132    ///
133    /// Returns errors on network or deserialization failures.
134    pub async fn invoice(
135        &self,
136        amount: &crate::pay::Amount,
137        comment: Option<&str>,
138        convert: Option<&str>,
139        payer: Option<crate::pay::PayerInformations>,
140    ) -> Result<crate::pay::client::CallbackResponse, &'static str> {
141        let callback = self.core.invoice(amount, comment, convert, payer);
142
143        let response = self
144            .client
145            .get(callback.to_string())
146            .send()
147            .await
148            .map_err(|_| "request failed")?;
149
150        let text = response.text().await.map_err(|_| "body failed")?;
151        text.parse().map_err(|_| "parse failed")
152    }
153}
154
155impl Withdraw<'_> {
156    /// # Errors
157    ///
158    /// Returns errors on network or deserialization failures.
159    pub async fn submit(&self, pr: &str) -> Result<crate::CallbackResponse, &'static str> {
160        let callback = self.core.submit(pr);
161
162        let response = self
163            .client
164            .get(callback.to_string())
165            .send()
166            .await
167            .map_err(|_| "request failed")?;
168
169        let bytes = response.bytes().await.map_err(|_| "body failed")?;
170        (&bytes as &[u8]).try_into().map_err(|_| "parse failed")
171    }
172}