1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
mod error;
mod http;


use serde_derive::{Deserialize, Serialize};
use serde_json::{json, Value};
use urlencoding::encode;
use crate::{GoogleApiError};
pub use error::*;
use crate::http::HttpClient;


pub struct GoogleIndexingApi {}

impl GoogleIndexingApi {
    pub fn url_notifications() -> UrlNotificationsApi {
        UrlNotificationsApi::default()
    }
}

#[derive(Default, Debug, Serialize, Deserialize, Clone)]
pub enum UrlNotificationsType {
    #[serde(rename = "URL_NOTIFICATION_TYPE_UNSPECIFIED")]
    #[default]
    UrlNotificationTypeUnspecified,
    #[serde(rename = "URL_UPDATED")]
    UPDATED,
    #[serde(rename = "URL_DELETED")]
    DELETED,
}

impl ToString for UrlNotificationsType {
    fn to_string(&self) -> String {
        match self {
            UrlNotificationsType::UPDATED => { "URL_UPDATED".to_string() }
            UrlNotificationsType::DELETED => { "URL_DELETED".to_string() }
            UrlNotificationsType::UrlNotificationTypeUnspecified => { "URL_NOTIFICATION_TYPE_UNSPECIFIED".to_string() }
        }
    }
}

/// https://developers.google.com/webmaster-tools/v1/searchanalytics
#[derive(Default)]
pub struct UrlNotificationsApi {}


impl UrlNotificationsApi {
    pub async fn publish(&self, token: &str, url: &str, url_type: UrlNotificationsType) -> Result<Value, GoogleApiError> {
        Ok(HttpClient::post(
            token,
            format!(r#"https://indexing.googleapis.com/v3/urlNotifications:publish"#, ).as_str(),
            json!({
                "url": url,
                "type": url_type.to_string(),
            }),
        ).await?)
    }
    pub async fn get_metadata(&self, token: &str, url: &str) -> Result<ResponseUrlNotificationMetadata, GoogleApiError> {
        Ok(HttpClient::get(
            token,
            format!(r#"https://indexing.googleapis.com/v3/urlNotifications/metadata?url={}"#, encode(url), ).as_str(),
        ).await?)
    }
    pub async fn batch(&self, token: &str, urls: Vec<String>, url_type: UrlNotificationsType) -> Result<Vec<GoogleIndexingBatch>, GoogleApiError> {
        Ok(HttpClient::execute(
            token,
            urls,
            url_type,
        ).await?)
    }
}

#[derive(Default, Debug, Serialize, Deserialize, Clone)]
pub struct ResponseUrlNotificationMetadata {
    pub url: String,
    #[serde(rename = "latestUpdate")]
    pub latest_update: Option<ResponseUrlNotification>,
    #[serde(rename = "latestRemove")]
    pub latest_remove: Option<ResponseUrlNotification>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ResponseUrlNotification {
    pub url: String,
    #[serde(rename = "type")]
    pub url_type: UrlNotificationsType,
    #[serde(rename = "notifyTime")]
    pub notify_time: String,
}


#[derive(Debug, Default)]
pub struct GoogleIndexingBatch {
    url: String,
    status_code: u16,
    value: String,
}

impl GoogleIndexingBatch {
    pub fn url(&self) -> &str {
        self.url.as_str()
    }
    pub fn status_code(&self) -> u16 {
        self.status_code
    }
    pub fn value(&self) -> &str {
        self.value.as_str()
    }
    pub fn json(&self) -> Value {
        let v = serde_json::from_str(self.value.as_str());
        if v.is_ok() {
            return v.unwrap();
        }
        Value::default()
    }
}