-
Notifications
You must be signed in to change notification settings - Fork 46
/
callback_executors.rs
173 lines (155 loc) · 6.12 KB
/
callback_executors.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
//! Executor abstraction for executing callbacks to user code (request filters, provider state change callbacks)
use std::collections::HashMap;
use std::fmt::{Debug, Display, Formatter};
use std::sync::Arc;
use async_trait::async_trait;
use bytes::Bytes;
use itertools::Either;
use maplit::*;
use serde_json::{json, Value};
use pact_models::bodies::OptionalBody;
use pact_models::content_types::JSON;
use pact_models::provider_states::ProviderState;
use pact_models::v4::http_parts::HttpRequest;
use tracing::warn;
use crate::provider_client::make_state_change_request;
/// Trait for executors that call request filters
pub trait RequestFilterExecutor: Debug {
/// Mutates HTTP requests based on some criteria.
fn call(self: Arc<Self>, request: &HttpRequest) -> HttpRequest;
/// Callback to mutate request data. This form is used by plugins.
fn call_non_http(
&self,
request_body: &OptionalBody,
metadata: &HashMap<String, Either<Value, Bytes>>
) -> (OptionalBody, HashMap<String, Either<Value, Bytes>>);
}
/// A "null" request filter executor, which does nothing, but permits
/// bypassing of typechecking issues where no filter should be applied.
#[derive(Debug, Clone)]
pub struct NullRequestFilterExecutor {
// This field is added (and is private) to guarantee that this struct
// is never instantiated accidentally, and is instead only able to be
// used for type-level programming.
_private_field: (),
}
impl RequestFilterExecutor for NullRequestFilterExecutor {
fn call(self: Arc<Self>, _request: &HttpRequest) -> HttpRequest {
unimplemented!("NullRequestFilterExecutor should never be called")
}
fn call_non_http(
&self,
_body: &OptionalBody,
_metadata: &HashMap<String, Either<Value, Bytes>>
) -> (OptionalBody, HashMap<String, Either<Value, Bytes>>) {
unimplemented!("NullRequestFilterExecutor should never be called")
}
}
/// Struct for returning errors from executing a provider state
#[derive(Debug, Clone)]
pub struct ProviderStateError {
/// Description of the error
pub description: String,
/// Interaction ID of the interaction that the error occurred
pub interaction_id: Option<String>
}
impl Display for ProviderStateError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "Provider state failed: {}{}", self.interaction_id.as_ref()
.map(|id| format!("(interaction_id: {}) ", id)).unwrap_or_default(), self.description)
}
}
impl std::error::Error for ProviderStateError {}
/// Trait for executors that call provider state callbacks
#[async_trait]
pub trait ProviderStateExecutor: Debug {
/// Invoke the callback for the given provider state, returning an optional Map of values
async fn call(
self: Arc<Self>,
interaction_id: Option<String>,
provider_state: &ProviderState,
setup: bool,
client: Option<&reqwest::Client>
) -> anyhow::Result<HashMap<String, Value>>;
/// If a teardown call for the Executor should be performed
fn teardown(self: &Self)-> bool;
}
/// Default provider state callback executor, which executes an HTTP request
#[derive(Debug, Clone)]
pub struct HttpRequestProviderStateExecutor {
/// URL to post state change requests to
pub state_change_url: Option<String>,
/// If teardown state change requests should be made (default is false)
pub state_change_teardown: bool,
/// If state change request data should be sent in the body (true) or as query parameters (false)
pub state_change_body: bool,
/// Number of times to retry the provider state request, zero means none
pub reties: u8
}
impl Default for HttpRequestProviderStateExecutor {
/// Create a default executor
fn default() -> HttpRequestProviderStateExecutor {
HttpRequestProviderStateExecutor {
state_change_url: None,
state_change_teardown: false,
state_change_body: true,
reties: 3
}
}
}
#[async_trait]
impl ProviderStateExecutor for HttpRequestProviderStateExecutor {
async fn call(
self: Arc<Self>,
interaction_id: Option<String>,
provider_state: &ProviderState,
setup: bool,
client: Option<&reqwest::Client>
) -> anyhow::Result<HashMap<String, Value>> {
match &self.state_change_url {
Some(state_change_url) => {
let mut state_change_request = HttpRequest { method: "POST".to_string(), .. HttpRequest::default() };
if self.state_change_body {
let json_body = json!({
"state".to_string() : provider_state.name.clone(),
"params".to_string() : provider_state.params.clone(),
"action".to_string() : if setup {
"setup".to_string()
} else {
"teardown".to_string()
}
});
state_change_request.body = OptionalBody::Present(json_body.to_string().into(), Some(JSON.clone()), None);
state_change_request.headers = Some(hashmap!{ "Content-Type".to_string() => vec!["application/json".to_string()] });
} else {
let mut query = hashmap!{ "state".to_string() => vec![Some(provider_state.name.clone())] };
if setup {
query.insert("action".to_string(), vec![Some("setup".to_string())]);
} else {
query.insert("action".to_string(), vec![Some("teardown".to_string())]);
}
for (k, v) in provider_state.params.clone() {
query.insert(k, vec![match v {
Value::String(ref s) => Some(s.clone()),
_ => Some(v.to_string())
}]);
}
state_change_request.query = Some(query);
}
make_state_change_request(client.unwrap_or(&reqwest::Client::default()), &state_change_url, &state_change_request, self.reties).await
.map_err(|err| ProviderStateError { description: err.to_string(), interaction_id }.into())
},
None => {
if !provider_state.name.is_empty() && setup {
warn!("State Change ignored as there is no state change URL provided for interaction {}", interaction_id.unwrap_or_default());
}
Ok(hashmap!{})
}
}
}
fn teardown(
self: &Self,
) -> bool {
return self.state_change_teardown;
}
}