indexnow_api/
lib.rs

1mod error;
2mod http;
3
4use crate::http::HttpClient;
5use crate::GoogleApiError;
6pub use error::*;
7use serde::{Deserialize, Serialize};
8
9/// API Access Endpoint
10pub struct IndexNowApi {
11    search_engine: String,
12    host: String,
13    key: String,
14    key_location: Option<String>,
15}
16
17impl IndexNowApi {
18    pub fn new<T: ToString, U: ToString>(host: T, key: U) -> IndexNowApi {
19        IndexNowApi {
20            search_engine: "https://api.indexnow.org".to_string(),
21            host: host.to_string(),
22            key: key.to_string(),
23            key_location: None,
24        }
25    }
26    pub fn set_search_engine<T: ToString>(&mut self, search_engine: T) {
27        self.search_engine = search_engine.to_string()
28    }
29    pub fn set_key_location<T: ToString>(&mut self, key_location: T) {
30        self.key_location = Some(key_location.to_string())
31    }
32
33
34    pub async fn send_urls<T:ToString>(&self, urls: Vec<T>) -> Result<(), GoogleApiError> {
35        HttpClient::post(
36            format!("{}/IndexNow", self.search_engine).as_str(),
37            SendData {
38                url_list: urls.iter().map(|q|q.to_string()).collect(),
39                host: self.host.to_string(),
40                key: self.key.to_string(),
41                key_location: self.key_location.clone(),
42            },
43        )
44        .await
45    }
46
47}
48
49/// URL Notification Type
50/// UPDATE or DELETE
51#[derive(Default, Debug, Serialize, Deserialize, Clone)]
52pub struct SendData {
53    #[serde(rename = "urlList")]
54    url_list: Vec<String>,
55    host: String,
56    key: String,
57    #[serde(rename = "keyLocation")]
58    key_location: Option<String>,
59}