|
|
|
@ -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());
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
return Ok(Some(body));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn sleep_until_next_poll(&self) -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
|