Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add timestamp as qs of url to bypass cache #15

Merged
merged 2 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading