1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#[derive(Clone, Default)]
pub struct Client(reqwest::Client);

impl Client {
    /// # Errors
    ///
    /// Returns errors on network or deserialization failures.
    pub async fn entrypoint(&self, s: &str) -> Result<Entrypoint, &'static str> {
        let client = &self.0;

        let url = match crate::resolve(s)? {
            crate::Resolved::Url(url) => url,
            crate::Resolved::Auth(_, core) => return Ok(Entrypoint::Auth(Auth { client, core })),
            crate::Resolved::Withdraw(_, core) => {
                return Ok(Entrypoint::Withdraw(Withdraw { client, core }))
            }
        };

        let response = client.get(url).send().await.map_err(|_| "request failed")?;
        let bytes = response.bytes().await.map_err(|_| "body failed")?;

        (&bytes as &[u8])
            .try_into()
            .map_err(|_| "parse failed")
            .map(|query: crate::Entrypoint| match query {
                crate::Entrypoint::Channel(core) => Entrypoint::Channel(Channel { client, core }),
                crate::Entrypoint::Pay(core) => Entrypoint::Pay(Pay { client, core }),
                crate::Entrypoint::Withdraw(core) => {
                    Entrypoint::Withdraw(Withdraw { client, core })
                }
            })
    }
}

#[derive(Clone, Debug)]
pub enum Entrypoint<'a> {
    Auth(Auth<'a>),
    Channel(Channel<'a>),
    Pay(Pay<'a>),
    Withdraw(Withdraw<'a>),
}

#[derive(Clone, Debug)]
pub struct Auth<'a> {
    client: &'a reqwest::Client,
    pub core: crate::auth::Entrypoint,
}

#[derive(Clone, Debug)]
pub struct Channel<'a> {
    client: &'a reqwest::Client,
    pub core: crate::channel::client::Entrypoint,
}

#[derive(Clone, Debug)]
pub struct Pay<'a> {
    client: &'a reqwest::Client,
    pub core: Box<crate::pay::client::Entrypoint>,
}

#[derive(Clone, Debug)]
pub struct Withdraw<'a> {
    client: &'a reqwest::Client,
    pub core: crate::withdraw::client::Entrypoint,
}

impl Auth<'_> {
    /// # Errors
    ///
    /// Returns errors on network or deserialization failures.
    pub async fn auth(
        &self,
        key: &str,
        sig: &[u8; 64],
    ) -> Result<crate::CallbackResponse, &'static str> {
        let callback = self.core.auth(key, sig);

        let response = self
            .client
            .get(callback.to_string())
            .send()
            .await
            .map_err(|_| "request failed")?;

        let bytes = response.bytes().await.map_err(|_| "body failed")?;
        (&bytes as &[u8]).try_into().map_err(|_| "parse failed")
    }
}

impl Channel<'_> {
    /// # Errors
    ///
    /// Returns errors on network or deserialization failures.
    pub async fn accept(
        &self,
        remoteid: &str,
        private: bool,
    ) -> Result<crate::CallbackResponse, &'static str> {
        let callback = self.core.accept(remoteid, private);

        let response = self
            .client
            .get(callback.to_string())
            .send()
            .await
            .map_err(|_| "request failed")?;

        let bytes = response.bytes().await.map_err(|_| "body failed")?;
        (&bytes as &[u8]).try_into().map_err(|_| "parse failed")
    }

    /// # Errors
    ///
    /// Returns errors on network or deserialization failures.
    pub async fn cancel(&self, remoteid: &str) -> Result<crate::CallbackResponse, &'static str> {
        let callback = self.core.cancel(remoteid);

        let response = self
            .client
            .get(callback.to_string())
            .send()
            .await
            .map_err(|_| "request failed")?;

        let bytes = response.bytes().await.map_err(|_| "body failed")?;
        (&bytes as &[u8]).try_into().map_err(|_| "parse failed")
    }
}

impl Pay<'_> {
    /// # Errors
    ///
    /// Returns errors on network or deserialization failures.
    pub async fn invoice(
        &self,
        amount: &crate::pay::Amount,
        comment: Option<&str>,
        convert: Option<&str>,
        payer: Option<crate::pay::PayerInformations>,
    ) -> Result<crate::pay::client::CallbackResponse, &'static str> {
        let callback = self.core.invoice(amount, comment, convert, payer);

        let response = self
            .client
            .get(callback.to_string())
            .send()
            .await
            .map_err(|_| "request failed")?;

        let text = response.text().await.map_err(|_| "body failed")?;
        text.parse().map_err(|_| "parse failed")
    }
}

impl Withdraw<'_> {
    /// # Errors
    ///
    /// Returns errors on network or deserialization failures.
    pub async fn submit(&self, pr: &str) -> Result<crate::CallbackResponse, &'static str> {
        let callback = self.core.submit(pr);

        let response = self
            .client
            .get(callback.to_string())
            .send()
            .await
            .map_err(|_| "request failed")?;

        let bytes = response.bytes().await.map_err(|_| "body failed")?;
        (&bytes as &[u8]).try_into().map_err(|_| "parse failed")
    }
}