-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapi.rs
More file actions
283 lines (242 loc) · 8.93 KB
/
api.rs
File metadata and controls
283 lines (242 loc) · 8.93 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
use crate::client::{AuthenticatedOpenPaymentsClient, BaseClient};
use crate::types::{
AccessTokenResponse, ContinueResponse, GrantRequest, GrantResponse, IncomingPayment,
IncomingPaymentRequest, JsonWebKeySet, ListIncomingPaymentsResponse,
ListOutgoingPaymentsResponse, OutgoingPayment, OutgoingPaymentRequest, PublicIncomingPayment,
Quote, QuoteRequest, WalletAddress,
};
use crate::{
grant::{cancel_grant, continue_grant, request_grant},
payments::{
complete_incoming_payment, create_incoming_payment, create_outgoing_payment,
get_incoming_payment, get_outgoing_payment, get_public_incoming_payment,
list_incoming_payments, list_outgoing_payments,
},
quotes::{create_quote, get_quote},
token::{revoke_access_token, rotate_access_token},
wallet_address::{get_keys, get_wallet_address},
Result,
};
pub mod authenticated {
use super::*;
pub struct QuoteResource<'a> {
client: &'a AuthenticatedOpenPaymentsClient,
}
impl<'a> QuoteResource<'a> {
pub(crate) fn new(client: &'a AuthenticatedOpenPaymentsClient) -> Self {
Self { client }
}
pub async fn create(
&self,
resource_server_url: &str,
req_body: &QuoteRequest,
access_token: Option<&str>,
) -> Result<Quote> {
create_quote(self.client, resource_server_url, req_body, access_token).await
}
pub async fn get(&self, quote_url: &str, access_token: Option<&str>) -> Result<Quote> {
get_quote(self.client, quote_url, access_token).await
}
}
pub struct IncomingPaymentResource<'a> {
client: &'a AuthenticatedOpenPaymentsClient,
}
impl<'a> IncomingPaymentResource<'a> {
pub(crate) fn new(client: &'a AuthenticatedOpenPaymentsClient) -> Self {
Self { client }
}
pub async fn create(
&self,
resource_server_url: &str,
req_body: &IncomingPaymentRequest,
access_token: Option<&str>,
) -> Result<IncomingPayment> {
create_incoming_payment(self.client, resource_server_url, req_body, access_token).await
}
pub async fn get(
&self,
payment_url: &str,
access_token: Option<&str>,
) -> Result<IncomingPayment> {
get_incoming_payment(self.client, payment_url, access_token).await
}
pub async fn complete(
&self,
payment_url: &str,
access_token: Option<&str>,
) -> Result<IncomingPayment> {
complete_incoming_payment(self.client, payment_url, access_token).await
}
pub async fn list(
&self,
resource_server_url: &str,
wallet_address: &str,
cursor: Option<&str>,
first: Option<u32>,
last: Option<u32>,
access_token: Option<&str>,
) -> Result<ListIncomingPaymentsResponse> {
list_incoming_payments(
self.client,
resource_server_url,
wallet_address,
cursor,
first,
last,
access_token,
)
.await
}
}
pub struct OutgoingPaymentResource<'a> {
client: &'a AuthenticatedOpenPaymentsClient,
}
impl<'a> OutgoingPaymentResource<'a> {
pub(crate) fn new(client: &'a AuthenticatedOpenPaymentsClient) -> Self {
Self { client }
}
pub async fn create(
&self,
resource_server_url: &str,
req_body: &OutgoingPaymentRequest,
access_token: Option<&str>,
) -> Result<OutgoingPayment> {
create_outgoing_payment(self.client, resource_server_url, req_body, access_token).await
}
pub async fn list(
&self,
resource_server_url: &str,
wallet_address: &str,
cursor: Option<&str>,
first: Option<u32>,
last: Option<u32>,
access_token: Option<&str>,
) -> Result<ListOutgoingPaymentsResponse> {
list_outgoing_payments(
self.client,
resource_server_url,
wallet_address,
cursor,
first,
last,
access_token,
)
.await
}
pub async fn get(
&self,
payment_url: &str,
access_token: Option<&str>,
) -> Result<OutgoingPayment> {
get_outgoing_payment(self.client, payment_url, access_token).await
}
}
pub struct Grant<'a> {
client: &'a AuthenticatedOpenPaymentsClient,
}
impl<'a> Grant<'a> {
pub(crate) fn new(client: &'a AuthenticatedOpenPaymentsClient) -> Self {
Self { client }
}
pub async fn request(&self, auth_url: &str, grant: &GrantRequest) -> Result<GrantResponse> {
request_grant(self.client, auth_url, grant).await
}
pub async fn continue_grant(
&self,
continue_uri: &str,
interact_ref: &str,
access_token: Option<&str>,
) -> Result<ContinueResponse> {
continue_grant(self.client, continue_uri, interact_ref, access_token).await
}
pub async fn cancel(&self, continue_uri: &str, access_token: Option<&str>) -> Result<()> {
cancel_grant(self.client, continue_uri, access_token).await
}
}
pub struct Token<'a> {
client: &'a AuthenticatedOpenPaymentsClient,
}
impl<'a> Token<'a> {
pub(crate) fn new(client: &'a AuthenticatedOpenPaymentsClient) -> Self {
Self { client }
}
pub async fn rotate(
&self,
auth_url: &str,
access_token: Option<&str>,
) -> Result<AccessTokenResponse> {
rotate_access_token(self.client, auth_url, access_token).await
}
pub async fn revoke(&self, auth_url: &str, access_token: Option<&str>) -> Result<()> {
revoke_access_token(self.client, auth_url, access_token).await
}
}
}
pub mod unauthenticated {
use super::*;
pub struct WalletAddressResource<'a, C: BaseClient> {
client: &'a C,
}
impl<'a, C: BaseClient> WalletAddressResource<'a, C> {
pub(crate) fn new(client: &'a C) -> Self {
Self { client }
}
pub async fn get(&self, wallet_address_url: &str) -> Result<WalletAddress> {
get_wallet_address(self.client.http_client(), wallet_address_url).await
}
pub async fn get_keys(&self, wallet: &WalletAddress) -> Result<JsonWebKeySet> {
get_keys(self.client.http_client(), wallet).await
}
pub async fn get_did_document(&self, _wallet: &WalletAddress) -> Result<()> {
unimplemented!()
}
}
pub struct IncomingPaymentResource<'a, C: BaseClient> {
client: &'a C,
}
impl<'a, C: BaseClient> IncomingPaymentResource<'a, C> {
pub(crate) fn new(client: &'a C) -> Self {
Self { client }
}
pub async fn get(&self, payment_url: &str) -> Result<PublicIncomingPayment> {
get_public_incoming_payment(self.client, payment_url).await
}
}
}
pub trait AuthenticatedResources {
fn quotes(&self) -> authenticated::QuoteResource<'_>;
fn incoming_payments(&self) -> authenticated::IncomingPaymentResource<'_>;
fn outgoing_payments(&self) -> authenticated::OutgoingPaymentResource<'_>;
fn grant(&self) -> authenticated::Grant<'_>;
fn token(&self) -> authenticated::Token<'_>;
}
/// Extension trait for any client (authenticated or not)
pub trait UnauthenticatedResources: BaseClient + Sized {
fn wallet_address(&self) -> unauthenticated::WalletAddressResource<'_, Self>;
fn public_incoming_payments(&self) -> unauthenticated::IncomingPaymentResource<'_, Self>;
}
impl AuthenticatedResources for AuthenticatedOpenPaymentsClient {
fn quotes(&self) -> authenticated::QuoteResource<'_> {
authenticated::QuoteResource::new(self)
}
fn incoming_payments(&self) -> authenticated::IncomingPaymentResource<'_> {
authenticated::IncomingPaymentResource::new(self)
}
fn outgoing_payments(&self) -> authenticated::OutgoingPaymentResource<'_> {
authenticated::OutgoingPaymentResource::new(self)
}
fn grant(&self) -> authenticated::Grant<'_> {
authenticated::Grant::new(self)
}
fn token(&self) -> authenticated::Token<'_> {
authenticated::Token::new(self)
}
}
impl<C: BaseClient> UnauthenticatedResources for C {
fn wallet_address(&self) -> unauthenticated::WalletAddressResource<'_, Self> {
unauthenticated::WalletAddressResource::new(self)
}
fn public_incoming_payments(&self) -> unauthenticated::IncomingPaymentResource<'_, Self> {
unauthenticated::IncomingPaymentResource::new(self)
}
}