Skip to content

Commit

Permalink
feat: Add timestamp as qs of url to bypass cache (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
igorbrasileiro authored Jul 17, 2023
1 parent 010a0bb commit 22cd128
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ serde_json = "1.0.94"
futures = "0.3.27"
clap = "3.1.8"
csv = "1.1.6"
url = "2.4.0"
chrono = "0.4.26"
18 changes: 17 additions & 1 deletion src/tester.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use chrono::{DateTime, Utc};
use futures::StreamExt;
use url::Url;

use crate::{Audit, Audits, Categories, Category, LHResult, PSIResult, PSIResultValues, Strategy};

Expand All @@ -21,6 +23,16 @@ const EMPTY_LH_RESULT: LHResult = LHResult {
},
};

fn add_query_param(
url_str: &str,
param_name: &str,
param_value: &str,
) -> Result<String, url::ParseError> {
let mut url = Url::parse(url_str)?;
url.query_pairs_mut().append_pair(param_name, param_value);
Ok(url.into())
}

/// This methods makes requests to google PSI API in batches with BUFFER_SIZE and add the result
/// into a return list.
/// This APIs has a though throttling and multiple times returns errors, so, when errors happen,
Expand All @@ -31,8 +43,12 @@ pub async fn get_page_audits(
number_of_runs: i8,
strategy: Strategy,
) -> Result<PSIResultValues, reqwest::Error> {
let now: DateTime<Utc> = Utc::now();
let timestamp = now.timestamp_millis().to_string();
let url_with_timestamp = add_query_param(url, "__v", &timestamp).unwrap();

let list_urls = (0..number_of_runs).map(|_| {
format!("https://www.googleapis.com/pagespeedonline/v5/runPagespeed?key={api_key}&url={url}&strategy={strategy}&category=performance", url = url, api_key = token, strategy = strategy)
format!("https://www.googleapis.com/pagespeedonline/v5/runPagespeed?key={api_key}&url={url}&strategy={strategy}&category=performance", url = url_with_timestamp, api_key = token, strategy = strategy)
}).collect::<Vec<String>>();
let client = reqwest::Client::new();

Expand Down

0 comments on commit 22cd128

Please sign in to comment.