Replace dbg macros with tracing events

This commit is contained in:
2021-07-11 19:45:26 -04:00
parent 22757bc475
commit 792e78391c
13 changed files with 199 additions and 19 deletions

View File

@@ -6,6 +6,7 @@ use std::{env, time::Duration};
use tempfile::tempfile;
use tokio::fs::File;
use tokio_util::compat::FuturesAsyncReadCompatExt;
use tracing::{info, instrument};
use super::{rate_limit_wait_duration, GAME_NAME, USER_AGENT};
@@ -14,6 +15,7 @@ pub struct DownloadLinkResponse {
json: Value,
}
#[instrument(skip(client))]
pub async fn get(client: &Client, mod_id: i32, file_id: i64) -> Result<DownloadLinkResponse> {
let res = client
.get(format!(
@@ -27,6 +29,7 @@ pub async fn get(client: &Client, mod_id: i32, file_id: i64) -> Result<DownloadL
.await?
.error_for_status()?;
info!(status = %res.status(), "fetched file download link from API");
let wait = rate_limit_wait_duration(&res)?;
let json = res.json::<Value>().await?;
@@ -34,6 +37,7 @@ pub async fn get(client: &Client, mod_id: i32, file_id: i64) -> Result<DownloadL
}
impl DownloadLinkResponse {
#[instrument(skip(self))]
pub fn link<'a>(&'a self) -> Result<&'a str> {
let link = self
.json
@@ -43,9 +47,11 @@ impl DownloadLinkResponse {
.ok_or_else(|| anyhow!("Missing URI key in link in API response"))?
.as_str()
.ok_or_else(|| anyhow!("URI value in API response link is not a string"))?;
info!(link = %link, "parsed download link from API response");
Ok(link)
}
#[instrument(skip(self, client))]
pub async fn download_file(&self, client: &Client) -> Result<File> {
let mut tokio_file = File::from_std(tempfile()?);
let res = client
@@ -55,6 +61,7 @@ impl DownloadLinkResponse {
.send()
.await?
.error_for_status()?;
info!(status = %res.status(), "downloaded file from nexus");
// See: https://github.com/benkay86/async-applied/blob/master/reqwest-tokio-compat/src/main.rs
let mut byte_stream = res

View File

@@ -3,6 +3,7 @@ use chrono::NaiveDateTime;
use reqwest::Client;
use serde_json::Value;
use std::{env, time::Duration};
use tracing::{info, instrument};
use super::{rate_limit_wait_duration, GAME_NAME, USER_AGENT};
@@ -21,6 +22,7 @@ pub struct ApiFile<'a> {
pub uploaded_at: NaiveDateTime,
}
#[instrument(skip(client))]
pub async fn get(client: &Client, nexus_mod_id: i32) -> Result<FilesResponse> {
let res = client
.get(format!(
@@ -34,6 +36,7 @@ pub async fn get(client: &Client, nexus_mod_id: i32) -> Result<FilesResponse> {
.await?
.error_for_status()?;
info!(status = %res.status(), "fetched files for mod from API");
let wait = rate_limit_wait_duration(&res)?;
let json = res.json::<Value>().await?;
@@ -41,6 +44,7 @@ pub async fn get(client: &Client, nexus_mod_id: i32) -> Result<FilesResponse> {
}
impl FilesResponse {
#[instrument(skip(self))]
pub fn files<'a>(&'a self) -> Result<Vec<ApiFile<'a>>> {
let files = self
.json
@@ -48,7 +52,7 @@ impl FilesResponse {
.ok_or_else(|| anyhow!("Missing files key in API response"))?
.as_array()
.ok_or_else(|| anyhow!("files value in API response is not an array"))?;
files
let files: Vec<ApiFile> = files
.into_iter()
.map(|file| {
let file_id = file
@@ -56,7 +60,6 @@ impl FilesResponse {
.ok_or_else(|| anyhow!("Missing file_id key in file in API response"))?
.as_i64()
.ok_or_else(|| anyhow!("file_id value in API response file is not a number"))?;
dbg!(file_id);
let name = file
.get("name")
.ok_or_else(|| anyhow!("Missing name key in file in API response"))?
@@ -102,6 +105,8 @@ impl FilesResponse {
uploaded_at,
})
})
.collect()
.collect::<Result<Vec<ApiFile>>>()?;
info!(num_files = files.len(), "parsed files out of API response");
Ok(files)
}
}

View File

@@ -3,6 +3,7 @@ use chrono::DateTime;
use chrono::Duration;
use chrono::Utc;
use reqwest::Response;
use tracing::info;
pub mod download_link;
pub mod files;
@@ -24,8 +25,7 @@ pub fn rate_limit_wait_duration(res: &Response) -> Result<Option<std::time::Dura
.headers()
.get("x-rl-hourly-reset")
.expect("No hourly reset in response headers");
dbg!(daily_remaining);
dbg!(hourly_remaining);
info!(daily_remaining = ?daily_remaining, hourly_remaining = ?hourly_remaining, "rate limit check");
if hourly_remaining == "0" {
let hourly_reset = hourly_reset.to_str()?.trim();
@@ -33,9 +33,11 @@ pub fn rate_limit_wait_duration(res: &Response) -> Result<Option<std::time::Dura
(DateTime::parse_from_str(hourly_reset, "%Y-%m-%d %H:%M:%S %z")?
+ Duration::seconds(5))
.into();
dbg!(hourly_reset);
let duration = (hourly_reset - Utc::now()).to_std()?;
dbg!(duration);
info!(
hourly_reset = ?hourly_reset,
duration = ?duration, "need to wait until rate-limit hourly reset"
);
return Ok(Some(duration));
}