forked from arlyon/async-stripe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstripe_request.rs
82 lines (65 loc) · 1.96 KB
/
stripe_request.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
use std::marker::PhantomData;
use serde::de::DeserializeOwned;
use serde::Serialize;
use crate::request_strategy::RequestStrategy;
#[derive(Debug, Copy, Clone)]
pub enum StripeMethod {
Get,
Post,
Delete,
}
pub trait StripeClient {
type Err;
fn execute<T: DeserializeOwned>(
&self,
req: CustomizedStripeRequest<T>,
) -> impl std::future::Future<Output = Result<T, Self::Err>>;
}
pub trait StripeBlockingClient {
type Err;
fn execute<T: DeserializeOwned>(&self, req: CustomizedStripeRequest<T>)
-> Result<T, Self::Err>;
}
pub trait StripeRequest {
type Output;
fn build(&self) -> RequestBuilder;
fn customize(&self) -> CustomizedStripeRequest<Self::Output> {
CustomizedStripeRequest {
inner: self.build(),
request_strategy: RequestStrategy::Once,
_output: PhantomData,
}
}
}
pub struct CustomizedStripeRequest<T> {
pub inner: RequestBuilder,
pub request_strategy: RequestStrategy,
_output: PhantomData<T>,
}
impl<T: DeserializeOwned> CustomizedStripeRequest<T> {
pub async fn send<C: StripeClient>(self, client: &C) -> Result<T, C::Err> {
client.execute(self).await
}
pub fn send_blocking<C: StripeBlockingClient>(self, client: &C) -> Result<T, C::Err> {
client.execute(self)
}
}
pub struct RequestBuilder {
pub query: Option<String>,
pub body: Option<String>,
pub path: String,
pub method: StripeMethod,
}
impl RequestBuilder {
pub fn new(method: StripeMethod, path: impl Into<String>) -> Self {
Self { path: path.into(), method, query: None, body: None }
}
pub fn query<P: Serialize>(mut self, params: &P) -> Self {
self.query = Some(serde_qs::to_string(params).expect("valid serialization"));
self
}
pub fn form<F: Serialize>(mut self, form: &F) -> Self {
self.body = Some(serde_qs::to_string(form).expect("valid serialization"));
self
}
}