#![allow(clippy::ptr_arg)]
use std::collections::{BTreeSet, HashMap};
use tokio::time::sleep;
#[derive(Clone)]
pub struct Pagespeedonline<C> {
pub client: common::Client<C>,
pub auth: Box<dyn common::GetToken>,
_user_agent: String,
_base_url: String,
_root_url: String,
}
impl<C> common::Hub for Pagespeedonline<C> {}
impl<'a, C> Pagespeedonline<C> {
pub fn new<A: 'static + common::GetToken>(
client: common::Client<C>,
auth: A,
) -> Pagespeedonline<C> {
Pagespeedonline {
client,
auth: Box::new(auth),
_user_agent: "google-api-rust-client/6.0.0".to_string(),
_base_url: "https://www.googleapis.com/pagespeedonline/v2/".to_string(),
_root_url: "https://www.googleapis.com/".to_string(),
}
}
pub fn pagespeedapi(&'a self) -> PagespeedapiMethods<'a, C> {
PagespeedapiMethods { hub: self }
}
pub fn user_agent(&mut self, agent_name: String) -> String {
std::mem::replace(&mut self._user_agent, agent_name)
}
pub fn base_url(&mut self, new_base_url: String) -> String {
std::mem::replace(&mut self._base_url, new_base_url)
}
pub fn root_url(&mut self, new_root_url: String) -> String {
std::mem::replace(&mut self._root_url, new_root_url)
}
}
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[serde_with::serde_as]
#[derive(Default, Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct PagespeedApiFormatStringV2 {
pub args: Option<Vec<PagespeedApiFormatStringV2Args>>,
pub format: Option<String>,
}
impl common::Part for PagespeedApiFormatStringV2 {}
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[serde_with::serde_as]
#[derive(Default, Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct PagespeedApiImageV2 {
#[serde_as(as = "Option<common::serde::standard_base64::Wrapper>")]
pub data: Option<Vec<u8>>,
pub height: Option<i32>,
pub key: Option<String>,
pub mime_type: Option<String>,
pub page_rect: Option<PagespeedApiImageV2PageRect>,
pub width: Option<i32>,
}
impl common::Part for PagespeedApiImageV2 {}
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[serde_with::serde_as]
#[derive(Default, Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct Result {
#[serde(rename = "captchaResult")]
pub captcha_result: Option<String>,
#[serde(rename = "formattedResults")]
pub formatted_results: Option<ResultFormattedResults>,
pub id: Option<String>,
#[serde(rename = "invalidRules")]
pub invalid_rules: Option<Vec<String>>,
pub kind: Option<String>,
#[serde(rename = "pageStats")]
pub page_stats: Option<ResultPageStats>,
#[serde(rename = "responseCode")]
pub response_code: Option<i32>,
#[serde(rename = "ruleGroups")]
pub rule_groups: Option<HashMap<String, ResultRuleGroups>>,
pub screenshot: Option<PagespeedApiImageV2>,
pub title: Option<String>,
pub version: Option<ResultVersion>,
}
impl common::ResponseResult for Result {}
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[serde_with::serde_as]
#[derive(Default, Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct PagespeedApiFormatStringV2Args {
pub key: Option<String>,
pub rects: Option<Vec<PagespeedApiFormatStringV2ArgsRects>>,
pub secondary_rects: Option<Vec<PagespeedApiFormatStringV2ArgsSecondaryRects>>,
#[serde(rename = "type")]
pub type_: Option<String>,
pub value: Option<String>,
}
impl common::NestedType for PagespeedApiFormatStringV2Args {}
impl common::Part for PagespeedApiFormatStringV2Args {}
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[serde_with::serde_as]
#[derive(Default, Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct PagespeedApiFormatStringV2ArgsRects {
pub height: Option<i32>,
pub left: Option<i32>,
pub top: Option<i32>,
pub width: Option<i32>,
}
impl common::NestedType for PagespeedApiFormatStringV2ArgsRects {}
impl common::Part for PagespeedApiFormatStringV2ArgsRects {}
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[serde_with::serde_as]
#[derive(Default, Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct PagespeedApiFormatStringV2ArgsSecondaryRects {
pub height: Option<i32>,
pub left: Option<i32>,
pub top: Option<i32>,
pub width: Option<i32>,
}
impl common::NestedType for PagespeedApiFormatStringV2ArgsSecondaryRects {}
impl common::Part for PagespeedApiFormatStringV2ArgsSecondaryRects {}
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[serde_with::serde_as]
#[derive(Default, Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct PagespeedApiImageV2PageRect {
pub height: Option<i32>,
pub left: Option<i32>,
pub top: Option<i32>,
pub width: Option<i32>,
}
impl common::NestedType for PagespeedApiImageV2PageRect {}
impl common::Part for PagespeedApiImageV2PageRect {}
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[serde_with::serde_as]
#[derive(Default, Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct ResultFormattedResults {
pub locale: Option<String>,
#[serde(rename = "ruleResults")]
pub rule_results: Option<HashMap<String, ResultFormattedResultsRuleResults>>,
}
impl common::NestedType for ResultFormattedResults {}
impl common::Part for ResultFormattedResults {}
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[serde_with::serde_as]
#[derive(Default, Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct ResultFormattedResultsRuleResults {
pub groups: Option<Vec<String>>,
#[serde(rename = "localizedRuleName")]
pub localized_rule_name: Option<String>,
#[serde(rename = "ruleImpact")]
pub rule_impact: Option<f64>,
pub summary: Option<PagespeedApiFormatStringV2>,
#[serde(rename = "urlBlocks")]
pub url_blocks: Option<Vec<ResultFormattedResultsRuleResultsUrlBlocks>>,
}
impl common::NestedType for ResultFormattedResultsRuleResults {}
impl common::Part for ResultFormattedResultsRuleResults {}
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[serde_with::serde_as]
#[derive(Default, Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct ResultFormattedResultsRuleResultsUrlBlocks {
pub header: Option<PagespeedApiFormatStringV2>,
pub urls: Option<Vec<ResultFormattedResultsRuleResultsUrlBlocksUrls>>,
}
impl common::NestedType for ResultFormattedResultsRuleResultsUrlBlocks {}
impl common::Part for ResultFormattedResultsRuleResultsUrlBlocks {}
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[serde_with::serde_as]
#[derive(Default, Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct ResultFormattedResultsRuleResultsUrlBlocksUrls {
pub details: Option<Vec<PagespeedApiFormatStringV2>>,
pub result: Option<PagespeedApiFormatStringV2>,
}
impl common::NestedType for ResultFormattedResultsRuleResultsUrlBlocksUrls {}
impl common::Part for ResultFormattedResultsRuleResultsUrlBlocksUrls {}
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[serde_with::serde_as]
#[derive(Default, Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct ResultPageStats {
#[serde(rename = "cssResponseBytes")]
#[serde_as(as = "Option<serde_with::DisplayFromStr>")]
pub css_response_bytes: Option<i64>,
#[serde(rename = "flashResponseBytes")]
#[serde_as(as = "Option<serde_with::DisplayFromStr>")]
pub flash_response_bytes: Option<i64>,
#[serde(rename = "htmlResponseBytes")]
#[serde_as(as = "Option<serde_with::DisplayFromStr>")]
pub html_response_bytes: Option<i64>,
#[serde(rename = "imageResponseBytes")]
#[serde_as(as = "Option<serde_with::DisplayFromStr>")]
pub image_response_bytes: Option<i64>,
#[serde(rename = "javascriptResponseBytes")]
#[serde_as(as = "Option<serde_with::DisplayFromStr>")]
pub javascript_response_bytes: Option<i64>,
#[serde(rename = "numberCssResources")]
pub number_css_resources: Option<i32>,
#[serde(rename = "numberHosts")]
pub number_hosts: Option<i32>,
#[serde(rename = "numberJsResources")]
pub number_js_resources: Option<i32>,
#[serde(rename = "numberResources")]
pub number_resources: Option<i32>,
#[serde(rename = "numberStaticResources")]
pub number_static_resources: Option<i32>,
#[serde(rename = "otherResponseBytes")]
#[serde_as(as = "Option<serde_with::DisplayFromStr>")]
pub other_response_bytes: Option<i64>,
#[serde(rename = "textResponseBytes")]
#[serde_as(as = "Option<serde_with::DisplayFromStr>")]
pub text_response_bytes: Option<i64>,
#[serde(rename = "totalRequestBytes")]
#[serde_as(as = "Option<serde_with::DisplayFromStr>")]
pub total_request_bytes: Option<i64>,
}
impl common::NestedType for ResultPageStats {}
impl common::Part for ResultPageStats {}
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[serde_with::serde_as]
#[derive(Default, Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct ResultRuleGroups {
pub score: Option<i32>,
}
impl common::NestedType for ResultRuleGroups {}
impl common::Part for ResultRuleGroups {}
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[serde_with::serde_as]
#[derive(Default, Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct ResultVersion {
pub major: Option<i32>,
pub minor: Option<i32>,
}
impl common::NestedType for ResultVersion {}
impl common::Part for ResultVersion {}
pub struct PagespeedapiMethods<'a, C>
where
C: 'a,
{
hub: &'a Pagespeedonline<C>,
}
impl<'a, C> common::MethodsBuilder for PagespeedapiMethods<'a, C> {}
impl<'a, C> PagespeedapiMethods<'a, C> {
pub fn runpagespeed(&self, url: &str) -> PagespeedapiRunpagespeedCall<'a, C> {
PagespeedapiRunpagespeedCall {
hub: self.hub,
_url: url.to_string(),
_strategy: Default::default(),
_screenshot: Default::default(),
_rule: Default::default(),
_locale: Default::default(),
_filter_third_party_resources: Default::default(),
_delegate: Default::default(),
_additional_params: Default::default(),
}
}
}
pub struct PagespeedapiRunpagespeedCall<'a, C>
where
C: 'a,
{
hub: &'a Pagespeedonline<C>,
_url: String,
_strategy: Option<String>,
_screenshot: Option<bool>,
_rule: Vec<String>,
_locale: Option<String>,
_filter_third_party_resources: Option<bool>,
_delegate: Option<&'a mut dyn common::Delegate>,
_additional_params: HashMap<String, String>,
}
impl<'a, C> common::CallBuilder for PagespeedapiRunpagespeedCall<'a, C> {}
impl<'a, C> PagespeedapiRunpagespeedCall<'a, C>
where
C: common::Connector,
{
pub async fn doit(mut self) -> common::Result<(common::Response, Result)> {
use std::borrow::Cow;
use std::io::{Read, Seek};
use common::{url::Params, ToParts};
use hyper::header::{AUTHORIZATION, CONTENT_LENGTH, CONTENT_TYPE, LOCATION, USER_AGENT};
let mut dd = common::DefaultDelegate;
let mut dlg: &mut dyn common::Delegate = self._delegate.unwrap_or(&mut dd);
dlg.begin(common::MethodInfo {
id: "pagespeedonline.pagespeedapi.runpagespeed",
http_method: hyper::Method::GET,
});
for &field in [
"alt",
"url",
"strategy",
"screenshot",
"rule",
"locale",
"filter_third_party_resources",
]
.iter()
{
if self._additional_params.contains_key(field) {
dlg.finished(false);
return Err(common::Error::FieldClash(field));
}
}
let mut params = Params::with_capacity(8 + self._additional_params.len());
params.push("url", self._url);
if let Some(value) = self._strategy.as_ref() {
params.push("strategy", value);
}
if let Some(value) = self._screenshot.as_ref() {
params.push("screenshot", value.to_string());
}
if !self._rule.is_empty() {
for f in self._rule.iter() {
params.push("rule", f);
}
}
if let Some(value) = self._locale.as_ref() {
params.push("locale", value);
}
if let Some(value) = self._filter_third_party_resources.as_ref() {
params.push("filter_third_party_resources", value.to_string());
}
params.extend(self._additional_params.iter());
params.push("alt", "json");
let mut url = self.hub._base_url.clone() + "runPagespeed";
match dlg.api_key() {
Some(value) => params.push("key", value),
None => {
dlg.finished(false);
return Err(common::Error::MissingAPIKey);
}
}
let url = params.parse_with_url(&url);
loop {
let mut req_result = {
let client = &self.hub.client;
dlg.pre_request();
let mut req_builder = hyper::Request::builder()
.method(hyper::Method::GET)
.uri(url.as_str())
.header(USER_AGENT, self.hub._user_agent.clone());
let request = req_builder
.header(CONTENT_LENGTH, 0_u64)
.body(common::to_body::<String>(None));
client.request(request.unwrap()).await
};
match req_result {
Err(err) => {
if let common::Retry::After(d) = dlg.http_error(&err) {
sleep(d).await;
continue;
}
dlg.finished(false);
return Err(common::Error::HttpError(err));
}
Ok(res) => {
let (mut parts, body) = res.into_parts();
let mut body = common::Body::new(body);
if !parts.status.is_success() {
let bytes = common::to_bytes(body).await.unwrap_or_default();
let error = serde_json::from_str(&common::to_string(&bytes));
let response = common::to_response(parts, bytes.into());
if let common::Retry::After(d) =
dlg.http_failure(&response, error.as_ref().ok())
{
sleep(d).await;
continue;
}
dlg.finished(false);
return Err(match error {
Ok(value) => common::Error::BadRequest(value),
_ => common::Error::Failure(response),
});
}
let response = {
let bytes = common::to_bytes(body).await.unwrap_or_default();
let encoded = common::to_string(&bytes);
match serde_json::from_str(&encoded) {
Ok(decoded) => (common::to_response(parts, bytes.into()), decoded),
Err(error) => {
dlg.response_json_decode_error(&encoded, &error);
return Err(common::Error::JsonDecodeError(
encoded.to_string(),
error,
));
}
}
};
dlg.finished(true);
return Ok(response);
}
}
}
}
pub fn url(mut self, new_value: &str) -> PagespeedapiRunpagespeedCall<'a, C> {
self._url = new_value.to_string();
self
}
pub fn strategy(mut self, new_value: &str) -> PagespeedapiRunpagespeedCall<'a, C> {
self._strategy = Some(new_value.to_string());
self
}
pub fn screenshot(mut self, new_value: bool) -> PagespeedapiRunpagespeedCall<'a, C> {
self._screenshot = Some(new_value);
self
}
pub fn add_rule(mut self, new_value: &str) -> PagespeedapiRunpagespeedCall<'a, C> {
self._rule.push(new_value.to_string());
self
}
pub fn locale(mut self, new_value: &str) -> PagespeedapiRunpagespeedCall<'a, C> {
self._locale = Some(new_value.to_string());
self
}
pub fn filter_third_party_resources(
mut self,
new_value: bool,
) -> PagespeedapiRunpagespeedCall<'a, C> {
self._filter_third_party_resources = Some(new_value);
self
}
pub fn delegate(
mut self,
new_value: &'a mut dyn common::Delegate,
) -> PagespeedapiRunpagespeedCall<'a, C> {
self._delegate = Some(new_value);
self
}
pub fn param<T>(mut self, name: T, value: T) -> PagespeedapiRunpagespeedCall<'a, C>
where
T: AsRef<str>,
{
self._additional_params
.insert(name.as_ref().to_string(), value.as_ref().to_string());
self
}
}