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,
|
url: String,
|
||||||
http_client: reqwest::Client,
|
http_client: reqwest::Client,
|
||||||
etag: Option<HeaderValue>,
|
etag: Option<HeaderValue>,
|
||||||
|
last_modified_at: Option<HeaderValue>,
|
||||||
next_poll_allowed: u64,
|
next_poll_allowed: u64,
|
||||||
ratelimit_remaining: Option<u64>,
|
ratelimit_remaining: Option<u64>,
|
||||||
ratelimit_reset: Option<u64>,
|
ratelimit_reset: Option<u64>,
|
||||||
@ -26,13 +27,16 @@ impl GithubEndpointWatcher {
|
|||||||
url,
|
url,
|
||||||
http_client,
|
http_client,
|
||||||
etag: None,
|
etag: None,
|
||||||
|
last_modified_at: None,
|
||||||
next_poll_allowed: 0,
|
next_poll_allowed: 0,
|
||||||
ratelimit_remaining: None,
|
ratelimit_remaining: None,
|
||||||
ratelimit_reset: 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
|
let mut request = self
|
||||||
.http_client
|
.http_client
|
||||||
.get(&self.url)
|
.get(&self.url)
|
||||||
@ -40,7 +44,17 @@ impl GithubEndpointWatcher {
|
|||||||
.header("User-Agent", "github_watcher")
|
.header("User-Agent", "github_watcher")
|
||||||
.header("Accept", "application/vnd.github.v3+json");
|
.header("Accept", "application/vnd.github.v3+json");
|
||||||
if let Some(etag) = &self.etag {
|
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_next_poll().await?;
|
||||||
self.sleep_until_ratelimit().await?;
|
self.sleep_until_ratelimit().await?;
|
||||||
@ -56,16 +70,26 @@ impl GithubEndpointWatcher {
|
|||||||
self.ratelimit_reset = number_header(headers.get("x-ratelimit-reset"))?;
|
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_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 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?;
|
if let reqwest::StatusCode::NOT_MODIFIED = response.status() {
|
||||||
println!("{}", serde_json::to_string(&body)?);
|
|
||||||
self.etag = etag;
|
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(|| {
|
let poll_interval_parsed: u64 = poll_interval.unwrap_or_else(|| {
|
||||||
println!("No poll interval returned, defaulting to 5 minute polling.");
|
println!("No poll interval returned, defaulting to 5 minute polling.");
|
||||||
300
|
300
|
||||||
});
|
});
|
||||||
self.next_poll_allowed = request_started_at + poll_interval_parsed;
|
self.next_poll_allowed = request_started_at + poll_interval_parsed;
|
||||||
Ok(body)
|
|
||||||
|
return Ok(Some(body));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn sleep_until_next_poll(&self) -> Result<(), Box<dyn std::error::Error>> {
|
async fn sleep_until_next_poll(&self) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
@ -80,18 +80,20 @@ impl<'a> GithubCtl<'a> {
|
|||||||
loop {
|
loop {
|
||||||
let api_result = match endpoint_watcher.get_results().await {
|
let api_result = match endpoint_watcher.get_results().await {
|
||||||
Ok(result) => result,
|
Ok(result) => result,
|
||||||
Err(_) => {
|
Err(e) => {
|
||||||
println!("Failed to get results.");
|
println!("Failed to get results. {}", e);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if let serde_json::Value::Array(events) = api_result {
|
if let Some(serde_json::Value::Array(events)) = api_result {
|
||||||
for event in events {
|
for event in events {
|
||||||
if let Err(_) = event_stream.send(event).await {
|
if let Err(_) = event_stream.send(event).await {
|
||||||
println!("Receiver dropped.");
|
println!("Receiver dropped.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if let None = api_result {
|
||||||
|
println!("No new results available.");
|
||||||
} else {
|
} else {
|
||||||
println!("Unsupported JSON type.");
|
println!("Unsupported JSON type.");
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
github.watch_repo(ORG, REPO).await?;
|
github.watch_repo(ORG, REPO).await?;
|
||||||
loop {
|
loop {
|
||||||
let event = github.get_event().await?;
|
let event = github.get_event().await?;
|
||||||
println!("{}", serde_json::to_string(&event)?);
|
// println!("{}", serde_json::to_string(&event)?);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user