-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_in.rs
156 lines (136 loc) · 3.97 KB
/
check_in.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
use crate::client::client;
use crate::timestamp::Timestamp;
use reqwest::Request;
use serde::Serialize;
pub struct CheckInConfig {
pub api_key: String,
pub endpoint: String,
pub identifier: String,
}
#[derive(Serialize)]
struct CheckInQuery {
api_key: String,
identifier: String,
timestamp: u64,
#[serde(skip_serializing_if = "Option::is_none")]
kind: Option<CronKind>,
#[serde(skip_serializing_if = "Option::is_none")]
digest: Option<String>,
}
impl CheckInQuery {
pub fn from_cron(config: &CronConfig, timestamp: &mut impl Timestamp, kind: CronKind) -> Self {
Self::from_config(
&config.check_in,
timestamp,
Some(kind),
Some(config.digest.clone()),
)
}
pub fn from_heartbeat(config: &HeartbeatConfig, timestamp: &mut impl Timestamp) -> Self {
Self::from_config(&config.check_in, timestamp, None, None)
}
fn from_config(
config: &CheckInConfig,
timestamp: &mut impl Timestamp,
kind: Option<CronKind>,
digest: Option<String>,
) -> Self {
Self {
api_key: config.api_key.clone(),
identifier: config.identifier.clone(),
timestamp: timestamp.as_secs(),
kind,
digest,
}
}
}
#[derive(Copy, Clone, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum CronKind {
Start,
Finish,
}
pub struct CronConfig {
pub check_in: CheckInConfig,
pub digest: String,
}
impl CronConfig {
pub fn request(
&self,
timestamp: &mut impl Timestamp,
kind: CronKind,
) -> Result<Request, reqwest::Error> {
let url = format!("{}/check_ins/cron", self.check_in.endpoint);
client()
.post(url)
.query(&CheckInQuery::from_cron(self, timestamp, kind))
.build()
}
}
pub struct HeartbeatConfig {
pub check_in: CheckInConfig,
}
impl HeartbeatConfig {
pub fn request(&self, timestamp: &mut impl Timestamp) -> Result<Request, reqwest::Error> {
let url = format!("{}/check_ins/heartbeats", self.check_in.endpoint);
client()
.post(url)
.query(&CheckInQuery::from_heartbeat(self, timestamp))
.build()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::timestamp::tests::{timestamp, EXPECTED_SECS};
fn check_in_config() -> CheckInConfig {
CheckInConfig {
api_key: "some_api_key".to_string(),
endpoint: "https://some-endpoint.com".to_string(),
identifier: "some-identifier".to_string(),
}
}
#[test]
fn cron_config_request() {
let config = CronConfig {
check_in: check_in_config(),
digest: "some-digest".to_string(),
};
let request = config.request(&mut timestamp(), CronKind::Start).unwrap();
assert_eq!(request.method().as_str(), "POST");
assert_eq!(
request.url().as_str(),
format!(
concat!(
"https://some-endpoint.com/check_ins/cron",
"?api_key=some_api_key",
"&identifier=some-identifier",
"×tamp={}",
"&kind=start",
"&digest=some-digest"
),
EXPECTED_SECS
)
);
}
#[test]
fn heartbeat_config_request() {
let config = HeartbeatConfig {
check_in: check_in_config(),
};
let request = config.request(&mut timestamp()).unwrap();
assert_eq!(request.method().as_str(), "POST");
assert_eq!(
request.url().as_str(),
format!(
concat!(
"https://some-endpoint.com/check_ins/heartbeats",
"?api_key=some_api_key",
"&identifier=some-identifier",
"×tamp={}"
),
EXPECTED_SECS
)
);
}
}