-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapi_call.rs
More file actions
192 lines (161 loc) · 4.96 KB
/
api_call.rs
File metadata and controls
192 lines (161 loc) · 4.96 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
use std::prelude::v1::*;
use crate::{anyhow, Error};
use crate::{
application::Application, extensions::List, service::Service, transaction::Transaction,
usage::Usage, user::User,
};
use crate::ToParams;
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Kind {
Authorize,
AuthRep,
Report,
}
impl Kind {
// report requires specific treatment due to being the only call supporting
// multiple transactions.
pub fn is_report(self) -> bool {
matches!(self, Kind::Report)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ApiCall<'a> {
kind: Kind,
service: &'a Service,
transactions: &'a [Transaction<'a>],
extensions: Option<&'a List<'a>>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Builder<'a> {
service: &'a Service,
kind: Option<Kind>,
transactions: &'a [Transaction<'a>],
extensions: Option<&'a List<'a>>,
}
// TODO: we can improve this with a state machine of types so that we are required to set svc, app,
// user and kind before being able to set (required) the usage to build the call
impl<'a> Builder<'a> {
pub fn new(service: &'a Service) -> Self {
Builder {
service,
kind: Default::default(),
transactions: Default::default(),
extensions: Default::default(),
}
}
pub fn service(&mut self, s: &'a Service) -> &mut Self {
self.service = s;
self
}
pub fn kind(&mut self, t: Kind) -> &mut Self {
self.kind = Some(t);
self
}
pub fn transactions(&mut self, txns: &'a [Transaction]) -> &mut Self {
self.transactions = txns;
self
}
pub fn extensions(&mut self, extensions: &'a List) -> &mut Self {
self.extensions = Some(extensions);
self
}
pub fn build(&self) -> Result<ApiCall<'_>, Error> {
let kind = self.kind.ok_or_else(|| anyhow!("kind error"))?;
Ok(ApiCall::new(
kind,
self.service,
self.transactions,
self.extensions,
))
}
}
use std::borrow::Cow;
impl<'a> ApiCall<'a> {
pub fn builder(service: &'a Service) -> Builder<'a> {
Builder::new(service)
}
pub fn new(
kind: Kind,
service: &'a Service,
transactions: &'a [Transaction],
extensions: Option<&'a List>,
) -> Self {
Self {
kind,
service,
transactions,
extensions,
}
}
pub fn kind(&self) -> Kind {
self.kind
}
pub fn service(&self) -> &Service {
self.service
}
pub fn transactions(&self) -> &[Transaction<'a>] {
self.transactions
}
// helper to get a transaction only if it's the only one
// useful for non-report calls
pub fn transaction(&self) -> Option<&Transaction<'a>> {
let txns = self.transactions();
if txns.len() == 1 {
Some(&txns[0])
} else {
None
}
}
pub fn application(&self) -> Option<&Application> {
self.transaction().map(Transaction::application)
}
pub fn user(&self) -> Option<&User> {
self.transaction().and_then(Transaction::user)
}
pub fn usage(&self) -> Option<&Usage<'_>> {
self.transaction().and_then(Transaction::usage)
}
pub fn extensions(&self) -> Option<&List<'_>> {
self.extensions
}
pub fn params(&self) -> Vec<(Cow<'_, str>, &str)> {
let mut params = Vec::with_capacity(8);
self.to_params(&mut params);
params
}
}
impl<'k, 'v, 'this, E> ToParams<'k, 'v, 'this, E> for ApiCall<'_>
where
'this: 'k + 'v,
E: Extend<(Cow<'k, str>, &'v str)>,
{
fn to_params_with_mangling<F: FnMut(Cow<'k, str>) -> Cow<'k, str>>(
&'this self,
extendable: &mut E,
key_mangling: &mut F,
) {
self.service
.to_params_with_mangling(extendable, key_mangling);
// keep the borrowck happy about stack closures living long enough
let mut txfn_storage_report;
let mut txfn_storage_rest;
let key_mangling: &mut dyn FnMut(usize, Cow<'k, str>) -> Cow<'k, str> =
if self.kind().is_report() {
txfn_storage_report = |n, c: Cow<'k, str>| {
// 3scale Apisonator takes arguments using the Rack format
key_mangling(format!("transactions[{}]{}", n, c).into())
};
&mut txfn_storage_report
} else {
txfn_storage_rest = |_n, c: Cow<'k, str>| key_mangling(c);
&mut txfn_storage_rest
};
// having multiple transactions with non-report endpoints
// is not allowed, but we can't fail in this trait impl
// (plus it would make sense to allow transactions in the
// other endpoints).
for (e, tx) in self.transactions().iter().enumerate() {
tx.to_params_with_mangling(extendable, &mut |c| key_mangling(e, c));
}
}
}