mod error;
mod http;
pub mod mobile_friendly_test;
pub mod search_analytics;
pub mod sitemaps;
pub mod sites;
pub mod types;
pub mod url_inspection;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use urlencoding::encode;
pub use error::*;
use crate::http::HttpClient;
use crate::mobile_friendly_test::{RequestMobileFriendlyTest, ResponseMobileFriendlyTest};
use crate::search_analytics::query::{SearchAnalyticsQueryRequest, SearchAnalyticsQueryResponse};
use crate::sitemaps::ResponseSitemapApiList;
use crate::sites::{ResponseSiteApi, ResponseSiteListApi};
use crate::url_inspection::{RequestUrlInspection, ResponseInspectionResult};
pub struct SearchConsoleApi;
impl SearchConsoleApi {
pub fn search_analytics() -> SearchAnalyticsApi {
SearchAnalyticsApi
}
pub fn sitemaps() -> SitemapsApi {
SitemapsApi
}
pub fn sites() -> SitesApi {
SitesApi
}
pub fn url_inspection() -> UrlInspectionApi {
UrlInspectionApi
}
pub fn mobile_friendly_test() -> MobileFriendlyTestApi {
MobileFriendlyTestApi
}
}
#[derive(Default)]
pub struct SearchAnalyticsApi;
impl SearchAnalyticsApi {
pub async fn query(
&self,
token: &str,
url: &str,
request: SearchAnalyticsQueryRequest,
) -> Result<SearchAnalyticsQueryResponse, GoogleApiError> {
HttpClient::post(
token,
&format!(
"https://www.googleapis.com/webmasters/v3/sites/{}/searchAnalytics/query",
encode(url)
),
request,
)
.await
}
}
#[derive(Default, Debug, Serialize, Deserialize, Clone)]
pub(crate) struct DummyRequest {}
#[derive(Default)]
pub struct SitemapsApi;
impl SitemapsApi {
pub async fn delete(
&self,
token: &str,
site_url: &str,
feed_path: &str,
) -> Result<Value, GoogleApiError> {
HttpClient::delete(
token,
&format!(
"https://www.googleapis.com/webmasters/v3/sites/{}/sitemaps/{}",
encode(site_url),
encode(feed_path)
),
json!({}),
)
.await
}
pub async fn get(
&self,
token: &str,
site_url: &str,
feed: &str,
) -> Result<Value, GoogleApiError> {
HttpClient::get(
token,
&format!(
"https://www.googleapis.com/webmasters/v3/sites/{}/sitemaps/{}",
encode(site_url),
encode(feed)
),
json!({}),
)
.await
}
pub async fn list(
&self,
token: &str,
site_url: &str,
) -> Result<ResponseSitemapApiList, GoogleApiError> {
HttpClient::get(
token,
&format!(
"https://www.googleapis.com/webmasters/v3/sites/{}/sitemaps",
encode(site_url)
),
DummyRequest::default(),
)
.await
}
pub async fn put(
&self,
token: &str,
site_url: &str,
feed: &str,
) -> Result<Value, GoogleApiError> {
HttpClient::post(
token,
&format!(
"https://www.googleapis.com/webmasters/v3/sites/{}/sitemaps/{}",
encode(site_url),
encode(feed)
),
json!({}),
)
.await
}
pub async fn submit(
&self,
token: &str,
site_url: &str,
feed: &str,
) -> Result<Value, GoogleApiError> {
HttpClient::put(
token,
&format!(
"https://www.googleapis.com/webmasters/v3/sites/{}/sitemaps/{}",
encode(site_url),
encode(feed)
),
json!({}),
)
.await
}
}
#[derive(Default)]
pub struct SitesApi;
impl SitesApi {
pub async fn add(&self, token: &str, site_url: &str) -> Result<Value, GoogleApiError> {
HttpClient::put(
token,
&format!(
"https://www.googleapis.com/webmasters/v3/sites/{}",
encode(site_url)
),
DummyRequest::default(),
)
.await
}
pub async fn delete(&self, token: &str, site_url: &str) -> Result<Value, GoogleApiError> {
HttpClient::delete(
token,
&format!(
"https://www.googleapis.com/webmasters/v3/sites/{}",
encode(site_url)
),
DummyRequest::default(),
)
.await
}
pub async fn get(
&self,
token: &str,
site_url: &str,
) -> Result<ResponseSiteApi, GoogleApiError> {
HttpClient::get(
token,
&format!(
"https://www.googleapis.com/webmasters/v3/sites/{}",
encode(site_url)
),
DummyRequest::default(),
)
.await
}
pub async fn list(&self, token: &str) -> Result<ResponseSiteListApi, GoogleApiError> {
HttpClient::get(
token,
"https://www.googleapis.com/webmasters/v3/sites",
DummyRequest::default(),
)
.await
}
}
#[derive(Default)]
pub struct UrlInspectionApi;
impl UrlInspectionApi {
pub async fn inspect(
&self,
token: &str,
request: &RequestUrlInspection,
) -> Result<ResponseInspectionResult, GoogleApiError> {
HttpClient::post(
token,
"https://searchconsole.googleapis.com/v1/urlInspection/index:inspect",
request,
)
.await
}
}
#[derive(Default)]
pub struct MobileFriendlyTestApi;
impl MobileFriendlyTestApi {
pub async fn run(
&self,
api_key: &str,
request: &RequestMobileFriendlyTest,
) -> Result<ResponseMobileFriendlyTest, GoogleApiError> {
HttpClient::post(
"",
&format!(
"https://searchconsole.googleapis.com/v1/urlTestingTools/mobileFriendlyTest:run?key={}",
api_key
),
request,
)
.await
}
}