#![allow(clippy::ptr_arg)]
use std::collections::{BTreeSet, HashMap};
use tokio::time::sleep;
#[derive(PartialEq, Eq, Ord, PartialOrd, Hash, Debug, Clone, Copy)]
pub enum Scope {
Full,
}
impl AsRef<str> for Scope {
fn as_ref(&self) -> &str {
match *self {
Scope::Full => "https://www.googleapis.com/auth/indexing",
}
}
}
#[allow(clippy::derivable_impls)]
impl Default for Scope {
fn default() -> Scope {
Scope::Full
}
}
#[derive(Clone)]
pub struct Indexing<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 Indexing<C> {}
impl<'a, C> Indexing<C> {
pub fn new<A: 'static + common::GetToken>(client: common::Client<C>, auth: A) -> Indexing<C> {
Indexing {
client,
auth: Box::new(auth),
_user_agent: "google-api-rust-client/7.0.0".to_string(),
_base_url: "https://indexing.googleapis.com/".to_string(),
_root_url: "https://indexing.googleapis.com/".to_string(),
}
}
pub fn url_notifications(&'a self) -> UrlNotificationMethods<'a, C> {
UrlNotificationMethods { 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 PublishUrlNotificationResponse {
#[serde(rename = "urlNotificationMetadata")]
pub url_notification_metadata: Option<UrlNotificationMetadata>,
}
impl common::ResponseResult for PublishUrlNotificationResponse {}
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[serde_with::serde_as]
#[derive(Default, Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct UrlNotification {
#[serde(rename = "notifyTime")]
pub notify_time: Option<chrono::DateTime<chrono::offset::Utc>>,
#[serde(rename = "type")]
pub type_: Option<String>,
pub url: Option<String>,
}
impl common::RequestValue for UrlNotification {}
impl common::Resource for UrlNotification {}
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[serde_with::serde_as]
#[derive(Default, Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct UrlNotificationMetadata {
#[serde(rename = "latestRemove")]
pub latest_remove: Option<UrlNotification>,
#[serde(rename = "latestUpdate")]
pub latest_update: Option<UrlNotification>,
pub url: Option<String>,
}
impl common::ResponseResult for UrlNotificationMetadata {}
pub struct UrlNotificationMethods<'a, C>
where
C: 'a,
{
hub: &'a Indexing<C>,
}
impl<'a, C> common::MethodsBuilder for UrlNotificationMethods<'a, C> {}
impl<'a, C> UrlNotificationMethods<'a, C> {
pub fn get_metadata(&self) -> UrlNotificationGetMetadataCall<'a, C> {
UrlNotificationGetMetadataCall {
hub: self.hub,
_url: Default::default(),
_delegate: Default::default(),
_additional_params: Default::default(),
_scopes: Default::default(),
}
}
pub fn publish(&self, request: UrlNotification) -> UrlNotificationPublishCall<'a, C> {
UrlNotificationPublishCall {
hub: self.hub,
_request: request,
_delegate: Default::default(),
_additional_params: Default::default(),
_scopes: Default::default(),
}
}
}
pub struct UrlNotificationGetMetadataCall<'a, C>
where
C: 'a,
{
hub: &'a Indexing<C>,
_url: Option<String>,
_delegate: Option<&'a mut dyn common::Delegate>,
_additional_params: HashMap<String, String>,
_scopes: BTreeSet<String>,
}
impl<'a, C> common::CallBuilder for UrlNotificationGetMetadataCall<'a, C> {}
impl<'a, C> UrlNotificationGetMetadataCall<'a, C>
where
C: common::Connector,
{
pub async fn doit(mut self) -> common::Result<(common::Response, UrlNotificationMetadata)> {
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: "indexing.urlNotifications.getMetadata",
http_method: hyper::Method::GET,
});
for &field in ["alt", "url"].iter() {
if self._additional_params.contains_key(field) {
dlg.finished(false);
return Err(common::Error::FieldClash(field));
}
}
let mut params = Params::with_capacity(3 + self._additional_params.len());
if let Some(value) = self._url.as_ref() {
params.push("url", value);
}
params.extend(self._additional_params.iter());
params.push("alt", "json");
let mut url = self.hub._base_url.clone() + "v3/urlNotifications/metadata";
if self._scopes.is_empty() {
self._scopes.insert(Scope::Full.as_ref().to_string());
}
let url = params.parse_with_url(&url);
loop {
let token = match self
.hub
.auth
.get_token(&self._scopes.iter().map(String::as_str).collect::<Vec<_>>()[..])
.await
{
Ok(token) => token,
Err(e) => match dlg.token(e) {
Ok(token) => token,
Err(e) => {
dlg.finished(false);
return Err(common::Error::MissingToken(e));
}
},
};
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());
if let Some(token) = token.as_ref() {
req_builder = req_builder.header(AUTHORIZATION, format!("Bearer {}", token));
}
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) -> UrlNotificationGetMetadataCall<'a, C> {
self._url = Some(new_value.to_string());
self
}
pub fn delegate(
mut self,
new_value: &'a mut dyn common::Delegate,
) -> UrlNotificationGetMetadataCall<'a, C> {
self._delegate = Some(new_value);
self
}
pub fn param<T>(mut self, name: T, value: T) -> UrlNotificationGetMetadataCall<'a, C>
where
T: AsRef<str>,
{
self._additional_params
.insert(name.as_ref().to_string(), value.as_ref().to_string());
self
}
pub fn add_scope<St>(mut self, scope: St) -> UrlNotificationGetMetadataCall<'a, C>
where
St: AsRef<str>,
{
self._scopes.insert(String::from(scope.as_ref()));
self
}
pub fn add_scopes<I, St>(mut self, scopes: I) -> UrlNotificationGetMetadataCall<'a, C>
where
I: IntoIterator<Item = St>,
St: AsRef<str>,
{
self._scopes
.extend(scopes.into_iter().map(|s| String::from(s.as_ref())));
self
}
pub fn clear_scopes(mut self) -> UrlNotificationGetMetadataCall<'a, C> {
self._scopes.clear();
self
}
}
pub struct UrlNotificationPublishCall<'a, C>
where
C: 'a,
{
hub: &'a Indexing<C>,
_request: UrlNotification,
_delegate: Option<&'a mut dyn common::Delegate>,
_additional_params: HashMap<String, String>,
_scopes: BTreeSet<String>,
}
impl<'a, C> common::CallBuilder for UrlNotificationPublishCall<'a, C> {}
impl<'a, C> UrlNotificationPublishCall<'a, C>
where
C: common::Connector,
{
pub async fn doit(
mut self,
) -> common::Result<(common::Response, PublishUrlNotificationResponse)> {
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: "indexing.urlNotifications.publish",
http_method: hyper::Method::POST,
});
for &field in ["alt"].iter() {
if self._additional_params.contains_key(field) {
dlg.finished(false);
return Err(common::Error::FieldClash(field));
}
}
let mut params = Params::with_capacity(3 + self._additional_params.len());
params.extend(self._additional_params.iter());
params.push("alt", "json");
let mut url = self.hub._base_url.clone() + "v3/urlNotifications:publish";
if self._scopes.is_empty() {
self._scopes.insert(Scope::Full.as_ref().to_string());
}
let url = params.parse_with_url(&url);
let mut json_mime_type = mime::APPLICATION_JSON;
let mut request_value_reader = {
let mut value = serde_json::value::to_value(&self._request).expect("serde to work");
common::remove_json_null_values(&mut value);
let mut dst = std::io::Cursor::new(Vec::with_capacity(128));
serde_json::to_writer(&mut dst, &value).unwrap();
dst
};
let request_size = request_value_reader
.seek(std::io::SeekFrom::End(0))
.unwrap();
request_value_reader
.seek(std::io::SeekFrom::Start(0))
.unwrap();
loop {
let token = match self
.hub
.auth
.get_token(&self._scopes.iter().map(String::as_str).collect::<Vec<_>>()[..])
.await
{
Ok(token) => token,
Err(e) => match dlg.token(e) {
Ok(token) => token,
Err(e) => {
dlg.finished(false);
return Err(common::Error::MissingToken(e));
}
},
};
request_value_reader
.seek(std::io::SeekFrom::Start(0))
.unwrap();
let mut req_result = {
let client = &self.hub.client;
dlg.pre_request();
let mut req_builder = hyper::Request::builder()
.method(hyper::Method::POST)
.uri(url.as_str())
.header(USER_AGENT, self.hub._user_agent.clone());
if let Some(token) = token.as_ref() {
req_builder = req_builder.header(AUTHORIZATION, format!("Bearer {}", token));
}
let request = req_builder
.header(CONTENT_TYPE, json_mime_type.to_string())
.header(CONTENT_LENGTH, request_size as u64)
.body(common::to_body(
request_value_reader.get_ref().clone().into(),
));
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 request(mut self, new_value: UrlNotification) -> UrlNotificationPublishCall<'a, C> {
self._request = new_value;
self
}
pub fn delegate(
mut self,
new_value: &'a mut dyn common::Delegate,
) -> UrlNotificationPublishCall<'a, C> {
self._delegate = Some(new_value);
self
}
pub fn param<T>(mut self, name: T, value: T) -> UrlNotificationPublishCall<'a, C>
where
T: AsRef<str>,
{
self._additional_params
.insert(name.as_ref().to_string(), value.as_ref().to_string());
self
}
pub fn add_scope<St>(mut self, scope: St) -> UrlNotificationPublishCall<'a, C>
where
St: AsRef<str>,
{
self._scopes.insert(String::from(scope.as_ref()));
self
}
pub fn add_scopes<I, St>(mut self, scopes: I) -> UrlNotificationPublishCall<'a, C>
where
I: IntoIterator<Item = St>,
St: AsRef<str>,
{
self._scopes
.extend(scopes.into_iter().map(|s| String::from(s.as_ref())));
self
}
pub fn clear_scopes(mut self) -> UrlNotificationPublishCall<'a, C> {
self._scopes.clear();
self
}
}