-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathclient.rs
284 lines (260 loc) · 10.5 KB
/
client.rs
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
284
//! Module containing the main TrueLayer API client.
//! This is where the main [`TrueLayerClient`](crate::client::TrueLayerClient) is.
use crate::{
apis::{
auth::{AuthApi, Credentials},
merchant_accounts::MerchantAccountsApi,
payments::PaymentsApi,
payments_providers::PaymentsProvidersApi,
payouts::PayoutsApi,
TrueLayerClientInner,
},
authenticator::Authenticator,
common::{
DEFAULT_AUTH_URL, DEFAULT_HOSTED_PAYMENTS_PAGE_URL, DEFAULT_PAYMENTS_URL,
DEFAULT_SANDBOX_AUTH_URL, DEFAULT_SANDBOX_HOSTED_PAYMENTS_PAGE_URL,
DEFAULT_SANDBOX_PAYMENTS_URL,
},
middlewares::{
authentication::AuthenticationMiddleware,
error_handling::ErrorHandlingMiddleware,
inject_user_agent::InjectUserAgentMiddleware,
retry_idempotent::{DynRetryPolicy, RetryIdempotentMiddleware},
signing::SigningMiddleware,
},
};
use reqwest::Url;
use reqwest_middleware::ClientWithMiddleware;
use reqwest_retry::{policies::ExponentialBackoff, RetryPolicy};
use reqwest_tracing::TracingMiddleware;
use std::sync::Arc;
/// Client for TrueLayer public APIs.
///
/// ## Authentication
///
/// All TrueLayer endpoints require authentication, and for that reason, a valid set of
/// [`Credentials`] must be provided when building a new client.
///
/// On the first request, the client automatically issues another request to the Auth server
/// to exchange the provided [`Credentials`] for an [`AccessToken`]
/// and caches the received token until it expires. All subsequent requests will reuse the cached
/// token without contacting the Auth server again.
///
/// If needed, you can call the [`get_access_token()`] function to retrieve the current [`AccessToken`],
/// even though that should rarely be necessary.
///
/// ## Idempotency and automatic retries
///
/// In case of a transient failure (e.g., a network error) the client automatically waits and
/// retries the failed request a few times before giving up and returning an error,
/// **only if the original request was idempotent**. Examples of idempotent requests are `GET`s and
/// `DELETE`s (see [RFC 7231] for a complete list).
///
/// In addition to the methods listed in [RFC 7231], the Payments V3 APIs support the usage of
/// [idempotency keys] to also make `POST`s idempotent. The client will transparently attach
/// an auto generated idempotency key to requests against endpoints supporting this feature
/// and thus will also retry them in case of transient failures, without causing unwanted double side-effects.
///
/// To change the retry policy (or to disable automatic retries entirely), use [`with_retry_policy()`]
/// when building a new client.
///
/// ## Request signature
///
/// Some endpoints that have notable side effects (like creating a new payment) require [requests signatures].
/// Signatures are handled automatically by the client if a key is provided at construction time
/// with [`with_signing_key()`].
///
/// [`AccessToken`]: crate::apis::auth::AccessToken
/// [`Credentials`]: crate::apis::auth::Credentials
/// [`get_access_token()`]: crate::apis::auth::AuthApi::get_access_token
/// [`with_retry_policy()`]: crate::client::TrueLayerClientBuilder::with_retry_policy
/// [`with_signing_key()`]: crate::client::TrueLayerClientBuilder::with_signing_key
/// [RFC 7231]: https://datatracker.ietf.org/doc/html/rfc7231#section-4.2.2
/// [idempotency keys]: https://docs.truelayer.com/docs/idempotency
/// [requests signatures]: https://docs.truelayer.com/docs/signing-your-requests
#[derive(Debug, Clone)]
pub struct TrueLayerClient {
/// Authentication APIs client.
pub auth: AuthApi,
/// Payments APIs client.
pub payments: PaymentsApi,
/// Payments Providers APIs client.
pub payments_providers: PaymentsProvidersApi,
/// Payouts APIs client.
pub payouts: PayoutsApi,
/// Merchant Accounts APIs client.
pub merchant_accounts: MerchantAccountsApi,
}
impl TrueLayerClient {
/// Builds a new [`TrueLayerClient`](crate::client::TrueLayerClient) with the default configuration.
pub fn new(credentials: Credentials) -> TrueLayerClient {
TrueLayerClientBuilder::new(credentials).build()
}
/// Returns a new builder to configure a new [`TrueLayerClient`](crate::client::TrueLayerClient).
pub fn builder(credentials: Credentials) -> TrueLayerClientBuilder {
TrueLayerClientBuilder::new(credentials)
}
}
/// Builder for a [`TrueLayerClient`](crate::client::TrueLayerClient).
#[derive(Debug)]
pub struct TrueLayerClientBuilder {
client: reqwest::Client,
retry_policy: Option<DynRetryPolicy>,
environment: Environment,
credentials: Credentials,
signing_key: Option<(String, Vec<u8>)>,
}
impl TrueLayerClientBuilder {
/// Creates a new builder to configure a [`TrueLayerClient`](crate::client::TrueLayerClient).
pub fn new(credentials: Credentials) -> Self {
Self {
client: reqwest::Client::new(),
retry_policy: Some(DynRetryPolicy(Arc::new(
ExponentialBackoff::builder().build_with_max_retries(3),
))),
environment: Environment::Live,
credentials,
signing_key: None,
}
}
/// Consumes the builder and builds a new [`TrueLayerClient`](crate::client::TrueLayerClient).
pub fn build(self) -> TrueLayerClient {
// Build an authenticator
let authenticator = Authenticator::new(
build_client_with_middleware(
self.client.clone(),
self.retry_policy.clone(),
None,
None,
),
self.environment.auth_url(),
self.credentials,
);
// Prepare the middlewares
let auth_middleware = Some(AuthenticationMiddleware {
authenticator: authenticator.clone(),
});
let signing_middleware = self
.signing_key
.map(|(key_id, private_key)| SigningMiddleware {
key_id,
private_key,
});
// Build the actual TL client
let inner = Arc::new(TrueLayerClientInner {
client: build_client_with_middleware(
self.client,
self.retry_policy.clone(),
auth_middleware,
signing_middleware,
),
environment: self.environment,
authenticator,
});
TrueLayerClient {
auth: AuthApi::new(inner.clone()),
payments: PaymentsApi::new(inner.clone()),
payments_providers: PaymentsProvidersApi::new(inner.clone()),
payouts: PayoutsApi::new(inner.clone()),
merchant_accounts: MerchantAccountsApi::new(inner),
}
}
/// Sets a specific reqwest [`Client`](reqwest::Client) to use.
pub fn with_http_client(mut self, client: reqwest::Client) -> Self {
self.client = client;
self
}
/// Sets a specific [`RetryPolicy`](retry_policies::RetryPolicy) to use when retrying transient failures.
///
/// To disable automatic retrying of failed requests, use `None`.
pub fn with_retry_policy(
mut self,
retry_policy: impl Into<Option<Arc<dyn RetryPolicy + Send + Sync + 'static>>>,
) -> Self {
self.retry_policy = retry_policy.into().map(DynRetryPolicy);
self
}
/// Configures a signing key for [request signing](https://docs.truelayer.com/docs/signing-your-requests).
/// Signing is required for some operations like initiating a new payment.
///
/// The private key is expected to be PEM encoded.
pub fn with_signing_key(mut self, key_id: &str, private_key: Vec<u8>) -> Self {
self.signing_key = Some((key_id.to_string(), private_key));
self
}
/// Sets the environment to which this client should connect
pub fn with_environment(mut self, environment: Environment) -> Self {
self.environment = environment;
self
}
}
fn build_client_with_middleware(
client: reqwest::Client,
retry_policy: Option<DynRetryPolicy>,
auth_middleware: Option<AuthenticationMiddleware>,
signing_middleware: Option<SigningMiddleware>,
) -> ClientWithMiddleware {
let mut builder = reqwest_middleware::ClientBuilder::new(client)
.with(InjectUserAgentMiddleware::new())
.with(TracingMiddleware::default())
.with(ErrorHandlingMiddleware);
if let Some(retry_policy) = retry_policy {
builder = builder.with(RetryIdempotentMiddleware::new(retry_policy));
}
if let Some(auth_middleware) = auth_middleware {
builder = builder.with(auth_middleware);
}
if let Some(signing_middleware) = signing_middleware {
builder = builder.with(signing_middleware);
}
builder.build()
}
/// TrueLayer environment to which a [`TrueLayerClient`](crate::client::TrueLayerClient) should connect.
#[derive(Debug, Clone)]
#[allow(clippy::large_enum_variant)]
pub enum Environment {
/// TrueLayer Live environment.
Live,
/// TrueLayer Sandbox environment.
Sandbox,
/// Custom environment. This variant is mainly used for tests.
Custom {
auth_url: Url,
payments_url: Url,
hpp_url: Url,
},
}
impl Environment {
/// Shortcut to build an `Environment::Custom` with all urls set to the given value.
pub fn from_single_url(url: &Url) -> Environment {
Environment::Custom {
auth_url: url.clone(),
payments_url: url.clone(),
hpp_url: url.clone(),
}
}
/// Base URL for authentication-related requests.
pub fn auth_url(&self) -> Url {
match self {
Environment::Live => Url::parse(DEFAULT_AUTH_URL).unwrap(),
Environment::Sandbox => Url::parse(DEFAULT_SANDBOX_AUTH_URL).unwrap(),
Environment::Custom { auth_url, .. } => auth_url.clone(),
}
}
/// Base URL for payments-related requests.
pub fn payments_url(&self) -> Url {
match self {
Environment::Live => Url::parse(DEFAULT_PAYMENTS_URL).unwrap(),
Environment::Sandbox => Url::parse(DEFAULT_SANDBOX_PAYMENTS_URL).unwrap(),
Environment::Custom { payments_url, .. } => payments_url.clone(),
}
}
/// Base URL for the Hosted Payments Page.
pub fn hpp_url(&self) -> Url {
match self {
Environment::Live => Url::parse(DEFAULT_HOSTED_PAYMENTS_PAGE_URL).unwrap(),
Environment::Sandbox => Url::parse(DEFAULT_SANDBOX_HOSTED_PAYMENTS_PAGE_URL).unwrap(),
Environment::Custom { hpp_url, .. } => hpp_url.clone(),
}
}
}