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
use crate::private_types::*;
use crate::types::*;
use reqwest::header::{
HeaderMap, HeaderValue, InvalidHeaderValue, CONTENT_ENCODING, CONTENT_TYPE, USER_AGENT,
};
use reqwest::Client;
use serde::Serialize;
use std::convert::From;
use std::error::Error;
use std::fmt::{Display, Formatter, Result as FmtResult};
const CONTENT_ENCODING_IDENTITY: &str = "identity";
const CONTENT_TYPE_JSON: &str = "application/json";
#[derive(Debug)]
pub enum EventsV2Error {
ReqwestError(reqwest::Error),
InvalidHeaderValue(InvalidHeaderValue),
HttpNotAccepted(u16),
HttpError(u16),
}
impl Error for EventsV2Error {}
impl Display for EventsV2Error {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
match self {
Self::ReqwestError(e) => write!(f, "RequestError: {}", e),
Self::InvalidHeaderValue(e) => write!(f, "InvalidHeaderValue: {}", e),
Self::HttpNotAccepted(e) => write!(f, "HttpNotAccepted: {}", e),
Self::HttpError(e) => write!(f, "HttpError: {}", e),
}
}
}
impl From<reqwest::Error> for EventsV2Error {
fn from(err: reqwest::Error) -> Self {
Self::ReqwestError(err)
}
}
impl From<InvalidHeaderValue> for EventsV2Error {
fn from(err: InvalidHeaderValue) -> Self {
Self::InvalidHeaderValue(err)
}
}
pub type EventsV2Result = Result<(), EventsV2Error>;
pub struct EventsV2 {
integration_key: String,
client: Client,
}
impl EventsV2 {
pub fn new(
integration_key: String,
user_agent: Option<String>,
) -> Result<EventsV2, EventsV2Error> {
let mut headers = HeaderMap::new();
headers.insert(CONTENT_TYPE, HeaderValue::from_str(CONTENT_TYPE_JSON)?);
headers.insert(
CONTENT_ENCODING,
HeaderValue::from_str(CONTENT_ENCODING_IDENTITY)?,
);
if let Some(ua) = user_agent {
headers.insert(USER_AGENT, HeaderValue::from_str(ua.as_str())?);
}
let client = Client::builder().default_headers(headers).build()?;
Ok(EventsV2 {
integration_key,
client,
})
}
pub async fn event<T: Serialize>(&self, event: Event<T>) -> EventsV2Result {
match event {
Event::Change(c) => self.change(c).await,
Event::AlertTrigger(at) => self.alert_trigger(at).await,
Event::AlertAcknowledge(aa) => {
self.alert_followup(aa.dedup_key, Action::Acknowledge).await
}
Event::AlertResolve(ar) => self.alert_followup(ar.dedup_key, Action::Resolve).await,
}
}
async fn change<T: Serialize>(&self, change: Change<T>) -> EventsV2Result {
let sendable_change = SendableChange::from_change(change, self.integration_key.clone());
self.do_post(
"https://events.pagerduty.com/v2/change/enqueue",
sendable_change,
)
.await
}
async fn alert_trigger<T: Serialize>(&self, alert_trigger: AlertTrigger<T>) -> EventsV2Result {
let sendable_alert_trigger =
SendableAlertTrigger::from_alert_trigger(alert_trigger, self.integration_key.clone());
self.do_post(
"https://events.pagerduty.com/v2/enqueue",
sendable_alert_trigger,
)
.await
}
async fn alert_followup(&self, dedup_key: String, action: Action) -> EventsV2Result {
let sendable_alert_followup =
SendableAlertFollowup::new(dedup_key, action, self.integration_key.clone());
self.do_post(
"https://events.pagerduty.com/v2/enqueue",
sendable_alert_followup,
)
.await
}
async fn do_post<T: Serialize>(&self, url: &str, content: T) -> EventsV2Result {
let res = self.client.post(url).json(&content).send().await?;
match res.status().as_u16() {
202 => Ok(()),
e if e < 400 => Err(EventsV2Error::HttpNotAccepted(e)),
e => Err(EventsV2Error::HttpError(e)),
}
}
}