Support detecting no events, but not sleeping after first one.
This commit is contained in:
parent
211d8681b2
commit
58aa8b249d
@ -8,6 +8,7 @@ pub struct GithubEndpointWatcher {
|
||||
url: String,
|
||||
http_client: reqwest::Client,
|
||||
etag: Option<HeaderValue>,
|
||||
last_modified_at: Option<HeaderValue>,
|
||||
next_poll_allowed: u64,
|
||||
ratelimit_remaining: Option<u64>,
|
||||
ratelimit_reset: Option<u64>,
|
||||
@ -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<serde_json::Value, Box<dyn std::error::Error>> {
|
||||
pub async fn get_results(
|
||||
&mut self,
|
||||
) -> Result<Option<serde_json::Value>, Box<dyn std::error::Error>> {
|
||||
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::<serde_json::Value>().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::<serde_json::Value>().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<dyn std::error::Error>> {
|
||||
|
@ -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.");
|
||||
}
|
||||
|
@ -13,6 +13,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
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)?);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user