google_search_console_api/
lib.rs

1mod error;
2mod http;
3pub mod types;
4
5pub mod search_analytics;
6pub mod sitemaps;
7pub mod sites;
8pub mod url_inspection;
9pub mod mobile_friendly_test;
10
11use serde_json::{json, Value};
12use urlencoding::encode;
13use crate::{GoogleApiError};
14pub use error::*;
15use crate::http::HttpClient;
16use crate::search_analytics::query::{SearchAnalyticsQueryRequest, SearchAnalyticsQueryResponse};
17use crate::sitemaps::ResponseSitemapApiList;
18
19
20pub struct SearchConsoleApi {}
21
22impl SearchConsoleApi {
23    pub fn search_analytics() -> SearchAnalyticsApi {
24        SearchAnalyticsApi::default()
25    }
26    pub fn sitemaps() -> SitemapsApi {
27        SitemapsApi::default()
28    }
29    pub fn sites() -> SitesApi {
30        SitesApi::default()
31    }
32    pub fn url_inspection() -> UrlInspectionApi {
33        UrlInspectionApi::default()
34    }
35    pub fn mobile_friendly_test() -> MobileFriendlyTestApi {
36        MobileFriendlyTestApi::default()
37    }
38}
39
40
41/// https://developers.google.com/webmaster-tools/v1/searchanalytics
42#[derive(Default)]
43pub struct SearchAnalyticsApi {}
44
45impl SearchAnalyticsApi {
46    pub async fn query(&self, token: &str, url: &str, requests: SearchAnalyticsQueryRequest) -> Result<SearchAnalyticsQueryResponse, GoogleApiError> {
47        Ok(HttpClient::post(
48            token,
49            format!(r#" https://www.googleapis.com/webmasters/v3/sites/{}/searchAnalytics/query"#, encode(url), ).as_str(),
50            requests,
51        ).await?)
52    }
53}
54
55use serde::{Deserialize, Serialize};
56use crate::mobile_friendly_test::{RequestMobileFriendlyTest, ResponseMobileFriendlyTest};
57use crate::sites::ResponseSiteApi;
58use crate::url_inspection::{RequestUrlInspection, ResponseInspectionResult};
59
60#[derive(Default, Debug, Serialize, Deserialize, Clone)]
61pub struct DummyRequest {}
62
63/// https://developers.google.com/webmaster-tools/v1/sitemaps
64#[derive(Default)]
65pub struct SitemapsApi {}
66
67impl SitemapsApi {
68    pub async fn delete(&self, token: &str, site_url: &str, feed_path: &str) -> Result<Value, GoogleApiError> {
69        Ok(HttpClient::delete(
70            token,
71            format!("https://www.googleapis.com/webmasters/v3/sites/{}/sitemaps/{}", encode(site_url), encode(feed_path)).as_str(),
72            json!({}),
73        ).await?)
74    }
75    pub async fn get(&self, token: &str, site_url: &str, feed: &str) -> Result<Value, GoogleApiError> {
76        Ok(HttpClient::get(
77            token,
78            format!("https://www.googleapis.com/webmasters/v3/sites/{}/sitemaps/{}", encode(site_url), encode(feed)).as_str(),
79            json!({}),
80        ).await?)
81    }
82    pub async fn list(&self, token: &str, site_url: &str) -> Result<ResponseSitemapApiList, GoogleApiError> {
83        Ok(HttpClient::get(
84            token,
85            format!("https://www.googleapis.com/webmasters/v3/sites/{}/sitemaps", encode(site_url)).as_str(),
86            DummyRequest::default(),
87        ).await?)
88    }
89    pub async fn put(&self, token: &str, site_url: &str, feed: &str) -> Result<Value, GoogleApiError> {
90        Ok(HttpClient::post(
91            token,
92            format!("https://www.googleapis.com/webmasters/v3/sites/{}/sitemaps/{}", encode(site_url), encode(feed)).as_str(),
93            json!({}),
94        ).await?)
95    }
96    pub async fn submit(&self, token: &str, site_url: &str, feed: &str) -> Result<Value, GoogleApiError> {
97        Ok(HttpClient::put(
98            token,
99            format!("https://www.googleapis.com/webmasters/v3/sites/{}/sitemaps/{}", encode(site_url), encode(feed)).as_str(),
100            json!({}),
101        ).await?)
102    }
103}
104
105
106/// https://developers.google.com/webmaster-tools/v1/sites?
107#[derive(Default)]
108pub struct SitesApi {}
109
110impl SitesApi {
111    pub async fn add(&self, token: &str, site_url: &str) -> Result<Value, GoogleApiError> {
112        Ok(HttpClient::put(
113            token,
114            format!("https://www.googleapis.com/webmasters/v3/sites/{}", encode(site_url)).as_str(),
115            DummyRequest::default(),
116        ).await?)
117    }
118    pub async fn delete(&self, token: &str, site_url: &str) -> Result<Value, GoogleApiError> {
119        Ok(HttpClient::delete(
120            token,
121            format!("https://www.googleapis.com/webmasters/v3/sites/{}", encode(site_url)).as_str(),
122            DummyRequest::default(),
123        ).await?)
124    }
125    pub async fn get(&self, token: &str, site_url: &str) -> Result<ResponseSiteApi, GoogleApiError> {
126        Ok(HttpClient::get(
127            token,
128            format!("https://www.googleapis.com/webmasters/v3/sites/{}", encode(site_url)).as_str(),
129            DummyRequest::default(),
130        ).await?)
131    }
132    pub async fn list(&self, token: &str) -> Result<Value, GoogleApiError> {
133        Ok(HttpClient::get(
134            token,
135            format!("https://www.googleapis.com/webmasters/v3/sites").as_str(),
136            DummyRequest::default(),
137        ).await?)
138    }
139}
140
141/// https://developers.google.com/webmaster-tools/v1/sites?
142#[derive(Default)]
143pub struct UrlInspectionApi {}
144
145impl UrlInspectionApi {
146    pub async fn inspect(&self, token: &str, request: &RequestUrlInspection) -> Result<ResponseInspectionResult, GoogleApiError> {
147        Ok(HttpClient::post(
148            token,
149            format!("https://searchconsole.googleapis.com/v1/urlInspection/index:inspect").as_str(),
150            request,
151        ).await?)
152    }
153}
154
155/// https://developers.google.com/webmaster-tools/v1/sites?
156#[derive(Default)]
157pub struct MobileFriendlyTestApi {}
158
159impl MobileFriendlyTestApi {
160    pub async fn run(&self, api_key: &str, request: &RequestMobileFriendlyTest) -> Result<ResponseMobileFriendlyTest, GoogleApiError> {
161        Ok(HttpClient::post(
162            "",
163            format!("https://searchconsole.googleapis.com/v1/urlTestingTools/mobileFriendlyTest:run?key={}",api_key).as_str(),
164            request,
165        ).await?)
166    }
167}