|
1 | | -use anyhow::{Context, Result, bail}; |
| 1 | +use anyhow::Result; |
2 | 2 |
|
3 | | -use super::HttpClient; |
4 | 3 | use super::retry::with_retry; |
5 | | -use crate::types::{CreateMonitorRequest, MonitorFilters, MonitorResource, SingleResponse}; |
6 | | - |
7 | | -/// Parse a single-resource response, or return an API error. |
8 | | -async fn parse_one<T: serde::de::DeserializeOwned>(resp: reqwest::Response) -> Result<T> { |
9 | | - let status = resp.status(); |
10 | | - if !status.is_success() { |
11 | | - let body = resp.text().await.unwrap_or_default(); |
12 | | - bail!("API error ({}): {}", status, body); |
13 | | - } |
14 | | - let single: SingleResponse<T> = resp.json().await.context("Failed to parse response")?; |
15 | | - Ok(single.data) |
16 | | -} |
17 | | - |
18 | | -/// Check response status and return an error with body if not successful. |
19 | | -async fn check_status(resp: reqwest::Response) -> Result<()> { |
20 | | - if !resp.status().is_success() { |
21 | | - let status = resp.status(); |
22 | | - let body = resp.text().await.unwrap_or_default(); |
23 | | - bail!("API error ({}): {}", status, body); |
24 | | - } |
25 | | - Ok(()) |
26 | | -} |
| 4 | +use super::{HttpClient, check_status, parse_one}; |
| 5 | +use crate::types::{ |
| 6 | + CreateMonitorRequest, MonitorFilters, MonitorResource, ResponseTimesResource, SlaResource, |
| 7 | + UpdateMonitorRequest, |
| 8 | +}; |
27 | 9 |
|
28 | 10 | impl HttpClient { |
29 | 11 | pub async fn list_monitors(&self, filters: &MonitorFilters) -> Result<Vec<MonitorResource>> { |
@@ -64,9 +46,61 @@ impl HttpClient { |
64 | 46 | parse_one(resp).await |
65 | 47 | } |
66 | 48 |
|
| 49 | + pub async fn update_monitor( |
| 50 | + &self, |
| 51 | + id: &str, |
| 52 | + req: &UpdateMonitorRequest, |
| 53 | + ) -> Result<MonitorResource> { |
| 54 | + let path = format!("/monitors/{id}"); |
| 55 | + let resp = with_retry(|| async { Ok(self.patch(&path).json(req).send().await?) }).await?; |
| 56 | + parse_one(resp).await |
| 57 | + } |
| 58 | + |
67 | 59 | pub async fn delete_monitor(&self, id: &str) -> Result<()> { |
68 | 60 | let path = format!("/monitors/{id}"); |
69 | 61 | let resp = with_retry(|| async { Ok(self.delete_req(&path).send().await?) }).await?; |
70 | 62 | check_status(resp).await |
71 | 63 | } |
| 64 | + |
| 65 | + pub async fn monitor_sla( |
| 66 | + &self, |
| 67 | + id: &str, |
| 68 | + from: Option<&str>, |
| 69 | + to: Option<&str>, |
| 70 | + ) -> Result<SlaResource> { |
| 71 | + let path = format!("/monitors/{id}/sla"); |
| 72 | + let resp = with_retry(|| async { |
| 73 | + let mut req = self.get(&path); |
| 74 | + if let Some(f) = from { |
| 75 | + req = req.query(&[("from", f)]); |
| 76 | + } |
| 77 | + if let Some(t) = to { |
| 78 | + req = req.query(&[("to", t)]); |
| 79 | + } |
| 80 | + Ok(req.send().await?) |
| 81 | + }) |
| 82 | + .await?; |
| 83 | + parse_one(resp).await |
| 84 | + } |
| 85 | + |
| 86 | + pub async fn monitor_response_times( |
| 87 | + &self, |
| 88 | + id: &str, |
| 89 | + from: Option<&str>, |
| 90 | + to: Option<&str>, |
| 91 | + ) -> Result<ResponseTimesResource> { |
| 92 | + let path = format!("/monitors/{id}/response-times"); |
| 93 | + let resp = with_retry(|| async { |
| 94 | + let mut req = self.get(&path); |
| 95 | + if let Some(f) = from { |
| 96 | + req = req.query(&[("from", f)]); |
| 97 | + } |
| 98 | + if let Some(t) = to { |
| 99 | + req = req.query(&[("to", t)]); |
| 100 | + } |
| 101 | + Ok(req.send().await?) |
| 102 | + }) |
| 103 | + .await?; |
| 104 | + parse_one(resp).await |
| 105 | + } |
72 | 106 | } |
0 commit comments