use std::collections::HashMap;
use std::cell::RefCell;
use std::default::Default;
use std::collections::BTreeSet;
use std::error::Error as StdError;
use serde_json as json;
use std::io;
use std::fs;
use std::mem;
use hyper::client::connect;
use tokio::io::{AsyncRead, AsyncWrite};
use tokio::time::sleep;
use tower_service;
use serde::{Serialize, Deserialize};
use crate::{client, client::GetToken, client::serde_with};
#[derive(Clone)]
pub struct Pagespeedonline<S> {
pub client: hyper::Client<S, hyper::body::Body>,
pub auth: Box<dyn client::GetToken>,
_user_agent: String,
_base_url: String,
_root_url: String,
}
impl<'a, S> client::Hub for Pagespeedonline<S> {}
impl<'a, S> Pagespeedonline<S> {
pub fn new<A: 'static + client::GetToken>(client: hyper::Client<S, hyper::body::Body>, auth: A) -> Pagespeedonline<S> {
Pagespeedonline {
client,
auth: Box::new(auth),
_user_agent: "google-api-rust-client/5.0.4".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, S> {
PagespeedapiMethods { hub: &self }
}
pub fn user_agent(&mut self, agent_name: String) -> String {
mem::replace(&mut self._user_agent, agent_name)
}
pub fn base_url(&mut self, new_base_url: String) -> String {
mem::replace(&mut self._base_url, new_base_url)
}
pub fn root_url(&mut self, new_root_url: String) -> String {
mem::replace(&mut self._root_url, new_root_url)
}
}
#[serde_with::serde_as(crate = "::client::serde_with")]
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct PagespeedApiFormatStringV2 {
pub args: Option<Vec<PagespeedApiFormatStringV2Args>>,
pub format: Option<String>,
}
impl client::Part for PagespeedApiFormatStringV2 {}
#[serde_with::serde_as(crate = "::client::serde_with")]
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct PagespeedApiImageV2 {
#[serde_as(as = "Option<::client::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 client::Part for PagespeedApiImageV2 {}
#[serde_with::serde_as(crate = "::client::serde_with")]
#[derive(Default, Clone, Debug, Serialize, 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 client::ResponseResult for Result {}
#[serde_with::serde_as(crate = "::client::serde_with")]
#[derive(Default, Clone, Debug, Serialize, 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 client::NestedType for PagespeedApiFormatStringV2Args {}
impl client::Part for PagespeedApiFormatStringV2Args {}
#[serde_with::serde_as(crate = "::client::serde_with")]
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct PagespeedApiFormatStringV2ArgsRects {
pub height: Option<i32>,
pub left: Option<i32>,
pub top: Option<i32>,
pub width: Option<i32>,
}
impl client::NestedType for PagespeedApiFormatStringV2ArgsRects {}
impl client::Part for PagespeedApiFormatStringV2ArgsRects {}
#[serde_with::serde_as(crate = "::client::serde_with")]
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct PagespeedApiFormatStringV2ArgsSecondaryRects {
pub height: Option<i32>,
pub left: Option<i32>,
pub top: Option<i32>,
pub width: Option<i32>,
}
impl client::NestedType for PagespeedApiFormatStringV2ArgsSecondaryRects {}
impl client::Part for PagespeedApiFormatStringV2ArgsSecondaryRects {}
#[serde_with::serde_as(crate = "::client::serde_with")]
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct PagespeedApiImageV2PageRect {
pub height: Option<i32>,
pub left: Option<i32>,
pub top: Option<i32>,
pub width: Option<i32>,
}
impl client::NestedType for PagespeedApiImageV2PageRect {}
impl client::Part for PagespeedApiImageV2PageRect {}
#[serde_with::serde_as(crate = "::client::serde_with")]
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct ResultFormattedResults {
pub locale: Option<String>,
#[serde(rename="ruleResults")]
pub rule_results: Option<HashMap<String, ResultFormattedResultsRuleResults>>,
}
impl client::NestedType for ResultFormattedResults {}
impl client::Part for ResultFormattedResults {}
#[serde_with::serde_as(crate = "::client::serde_with")]
#[derive(Default, Clone, Debug, Serialize, 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 client::NestedType for ResultFormattedResultsRuleResults {}
impl client::Part for ResultFormattedResultsRuleResults {}
#[serde_with::serde_as(crate = "::client::serde_with")]
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct ResultFormattedResultsRuleResultsUrlBlocks {
pub header: Option<PagespeedApiFormatStringV2>,
pub urls: Option<Vec<ResultFormattedResultsRuleResultsUrlBlocksUrls>>,
}
impl client::NestedType for ResultFormattedResultsRuleResultsUrlBlocks {}
impl client::Part for ResultFormattedResultsRuleResultsUrlBlocks {}
#[serde_with::serde_as(crate = "::client::serde_with")]
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct ResultFormattedResultsRuleResultsUrlBlocksUrls {
pub details: Option<Vec<PagespeedApiFormatStringV2>>,
pub result: Option<PagespeedApiFormatStringV2>,
}
impl client::NestedType for ResultFormattedResultsRuleResultsUrlBlocksUrls {}
impl client::Part for ResultFormattedResultsRuleResultsUrlBlocksUrls {}
#[serde_with::serde_as(crate = "::client::serde_with")]
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct ResultPageStats {
#[serde(rename="cssResponseBytes")]
#[serde_as(as = "Option<::client::serde_with::DisplayFromStr>")]
pub css_response_bytes: Option<i64>,
#[serde(rename="flashResponseBytes")]
#[serde_as(as = "Option<::client::serde_with::DisplayFromStr>")]
pub flash_response_bytes: Option<i64>,
#[serde(rename="htmlResponseBytes")]
#[serde_as(as = "Option<::client::serde_with::DisplayFromStr>")]
pub html_response_bytes: Option<i64>,
#[serde(rename="imageResponseBytes")]
#[serde_as(as = "Option<::client::serde_with::DisplayFromStr>")]
pub image_response_bytes: Option<i64>,
#[serde(rename="javascriptResponseBytes")]
#[serde_as(as = "Option<::client::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<::client::serde_with::DisplayFromStr>")]
pub other_response_bytes: Option<i64>,
#[serde(rename="textResponseBytes")]
#[serde_as(as = "Option<::client::serde_with::DisplayFromStr>")]
pub text_response_bytes: Option<i64>,
#[serde(rename="totalRequestBytes")]
#[serde_as(as = "Option<::client::serde_with::DisplayFromStr>")]
pub total_request_bytes: Option<i64>,
}
impl client::NestedType for ResultPageStats {}
impl client::Part for ResultPageStats {}
#[serde_with::serde_as(crate = "::client::serde_with")]
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct ResultRuleGroups {
pub score: Option<i32>,
}
impl client::NestedType for ResultRuleGroups {}
impl client::Part for ResultRuleGroups {}
#[serde_with::serde_as(crate = "::client::serde_with")]
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct ResultVersion {
pub major: Option<i32>,
pub minor: Option<i32>,
}
impl client::NestedType for ResultVersion {}
impl client::Part for ResultVersion {}
pub struct PagespeedapiMethods<'a, S>
where S: 'a {
hub: &'a Pagespeedonline<S>,
}
impl<'a, S> client::MethodsBuilder for PagespeedapiMethods<'a, S> {}
impl<'a, S> PagespeedapiMethods<'a, S> {
pub fn runpagespeed(&self, url: &str) -> PagespeedapiRunpagespeedCall<'a, S> {
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, S>
where S: 'a {
hub: &'a Pagespeedonline<S>,
_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 client::Delegate>,
_additional_params: HashMap<String, String>,
}
impl<'a, S> client::CallBuilder for PagespeedapiRunpagespeedCall<'a, S> {}
impl<'a, S> PagespeedapiRunpagespeedCall<'a, S>
where
S: tower_service::Service<http::Uri> + Clone + Send + Sync + 'static,
S::Response: hyper::client::connect::Connection + AsyncRead + AsyncWrite + Send + Unpin + 'static,
S::Future: Send + Unpin + 'static,
S::Error: Into<Box<dyn StdError + Send + Sync>>,
{
pub async fn doit(mut self) -> client::Result<(hyper::Response<hyper::body::Body>, Result)> {
use std::io::{Read, Seek};
use hyper::header::{CONTENT_TYPE, CONTENT_LENGTH, AUTHORIZATION, USER_AGENT, LOCATION};
use client::{ToParts, url::Params};
use std::borrow::Cow;
let mut dd = client::DefaultDelegate;
let mut dlg: &mut dyn client::Delegate = self._delegate.unwrap_or(&mut dd);
dlg.begin(client::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(client::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.len() > 0 {
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(client::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
.body(hyper::body::Body::empty());
client.request(request.unwrap()).await
};
match req_result {
Err(err) => {
if let client::Retry::After(d) = dlg.http_error(&err) {
sleep(d).await;
continue;
}
dlg.finished(false);
return Err(client::Error::HttpError(err))
}
Ok(mut res) => {
if !res.status().is_success() {
let res_body_string = client::get_body_as_string(res.body_mut()).await;
let (parts, _) = res.into_parts();
let body = hyper::Body::from(res_body_string.clone());
let restored_response = hyper::Response::from_parts(parts, body);
let server_response = json::from_str::<serde_json::Value>(&res_body_string).ok();
if let client::Retry::After(d) = dlg.http_failure(&restored_response, server_response.clone()) {
sleep(d).await;
continue;
}
dlg.finished(false);
return match server_response {
Some(error_value) => Err(client::Error::BadRequest(error_value)),
None => Err(client::Error::Failure(restored_response)),
}
}
let result_value = {
let res_body_string = client::get_body_as_string(res.body_mut()).await;
match json::from_str(&res_body_string) {
Ok(decoded) => (res, decoded),
Err(err) => {
dlg.response_json_decode_error(&res_body_string, &err);
return Err(client::Error::JsonDecodeError(res_body_string, err));
}
}
};
dlg.finished(true);
return Ok(result_value)
}
}
}
}
pub fn url(mut self, new_value: &str) -> PagespeedapiRunpagespeedCall<'a, S> {
self._url = new_value.to_string();
self
}
pub fn strategy(mut self, new_value: &str) -> PagespeedapiRunpagespeedCall<'a, S> {
self._strategy = Some(new_value.to_string());
self
}
pub fn screenshot(mut self, new_value: bool) -> PagespeedapiRunpagespeedCall<'a, S> {
self._screenshot = Some(new_value);
self
}
pub fn add_rule(mut self, new_value: &str) -> PagespeedapiRunpagespeedCall<'a, S> {
self._rule.push(new_value.to_string());
self
}
pub fn locale(mut self, new_value: &str) -> PagespeedapiRunpagespeedCall<'a, S> {
self._locale = Some(new_value.to_string());
self
}
pub fn filter_third_party_resources(mut self, new_value: bool) -> PagespeedapiRunpagespeedCall<'a, S> {
self._filter_third_party_resources = Some(new_value);
self
}
pub fn delegate(mut self, new_value: &'a mut dyn client::Delegate) -> PagespeedapiRunpagespeedCall<'a, S> {
self._delegate = Some(new_value);
self
}
pub fn param<T>(mut self, name: T, value: T) -> PagespeedapiRunpagespeedCall<'a, S>
where T: AsRef<str> {
self._additional_params.insert(name.as_ref().to_string(), value.as_ref().to_string());
self
}
}