From 58aa8b249df9029bb4d2d9e2bc987dfbd6eb6d5a Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sun, 8 May 2022 00:06:35 -0400 Subject: [PATCH] Support detecting no events, but not sleeping after first one. --- src/githubctl/github_endpoint_watcher.rs | 46 ++++++++++++++++++------ src/githubctl/githubctl.rs | 8 +++-- src/main.rs | 2 +- 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/src/githubctl/github_endpoint_watcher.rs b/src/githubctl/github_endpoint_watcher.rs index 04abb38..5481ec8 100644 --- a/src/githubctl/github_endpoint_watcher.rs +++ b/src/githubctl/github_endpoint_watcher.rs @@ -8,6 +8,7 @@ pub struct GithubEndpointWatcher { url: String, http_client: reqwest::Client, etag: Option, + last_modified_at: Option, next_poll_allowed: u64, ratelimit_remaining: Option, ratelimit_reset: Option, @@ -26,13 +27,16 @@ impl GithubEndpointWatcher { url, http_client, etag: None, + last_modified_at: None, next_poll_allowed: 0, ratelimit_remaining: None, ratelimit_reset: None, }) } - pub async fn get_results(&mut self) -> Result> { + pub async fn get_results( + &mut self, + ) -> Result, Box> { let mut request = self .http_client .get(&self.url) @@ -40,7 +44,17 @@ impl GithubEndpointWatcher { .header("User-Agent", "github_watcher") .header("Accept", "application/vnd.github.v3+json"); if let Some(etag) = &self.etag { - request = request.header(reqwest::header::ETAG, etag); + let trimmed = etag.to_str()?.trim_matches('"'); + println!("Setting etag to {}", trimmed); + request = request.header(reqwest::header::ETAG, trimmed); + } + if let Some(last_modified_at) = &self.last_modified_at { + // let trimmed = etag.to_str()?.trim_matches('"'); + println!( + "Setting If-Modified-Since to {}", + last_modified_at.to_str()? + ); + request = request.header(reqwest::header::IF_MODIFIED_SINCE, last_modified_at); } self.sleep_until_next_poll().await?; self.sleep_until_ratelimit().await?; @@ -56,16 +70,26 @@ impl GithubEndpointWatcher { self.ratelimit_reset = number_header(headers.get("x-ratelimit-reset"))?; // let ratelimit_used = headers.get("x-ratelimit-used").map(|x| x.to_owned()); // let ratelimit_resource = headers.get("x-ratelimit-resource").map(|x| x.to_owned()); + let last_modified_at = headers + .get(reqwest::header::LAST_MODIFIED) + .map(|x| x.to_owned()); - let body = response.json::().await?; - println!("{}", serde_json::to_string(&body)?); - self.etag = etag; - let poll_interval_parsed: u64 = poll_interval.unwrap_or_else(|| { - println!("No poll interval returned, defaulting to 5 minute polling."); - 300 - }); - self.next_poll_allowed = request_started_at + poll_interval_parsed; - Ok(body) + if let reqwest::StatusCode::NOT_MODIFIED = response.status() { + self.etag = etag; + self.last_modified_at = last_modified_at; + return Ok(None); + } else { + let body = response.json::().await?; + self.etag = etag; + self.last_modified_at = last_modified_at; + let poll_interval_parsed: u64 = poll_interval.unwrap_or_else(|| { + println!("No poll interval returned, defaulting to 5 minute polling."); + 300 + }); + self.next_poll_allowed = request_started_at + poll_interval_parsed; + + return Ok(Some(body)); + } } async fn sleep_until_next_poll(&self) -> Result<(), Box> { diff --git a/src/githubctl/githubctl.rs b/src/githubctl/githubctl.rs index dc408fa..7f54391 100644 --- a/src/githubctl/githubctl.rs +++ b/src/githubctl/githubctl.rs @@ -80,18 +80,20 @@ impl<'a> GithubCtl<'a> { loop { let api_result = match endpoint_watcher.get_results().await { Ok(result) => result, - Err(_) => { - println!("Failed to get results."); + Err(e) => { + println!("Failed to get results. {}", e); return; } }; - if let serde_json::Value::Array(events) = api_result { + if let Some(serde_json::Value::Array(events)) = api_result { for event in events { if let Err(_) = event_stream.send(event).await { println!("Receiver dropped."); return; } } + } else if let None = api_result { + println!("No new results available."); } else { println!("Unsupported JSON type."); } diff --git a/src/main.rs b/src/main.rs index 91c387d..aa6141c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,6 +13,6 @@ async fn main() -> Result<(), Box> { github.watch_repo(ORG, REPO).await?; loop { let event = github.get_event().await?; - println!("{}", serde_json::to_string(&event)?); + // println!("{}", serde_json::to_string(&event)?); } }