pinterest_api/api/
post_pins_pid_id_save.rs

1use reqwest::RequestBuilder;
2use serde::{Deserialize, Serialize};
3
4use crate::{
5    api::{execute_api, ApiResponse},
6    error::Error,
7    options::{apply_options, make_url, ApiOptions},
8    response::pin::Pin,
9};
10
11const URL_PATH: &str = "/pins";
12
13#[derive(Serialize, Deserialize, Debug, Default, Clone)]
14pub struct Body {
15    pub board_id: Option<String>,
16    pub board_section_id: Option<String>,
17}
18
19#[derive(Debug, Clone, Default)]
20pub struct Api {
21    options: Option<ApiOptions>,
22    pin_id: String,
23    ad_account_id: Option<String>,
24    body: Body,
25}
26
27impl Api {
28    pub fn new(options: Option<ApiOptions>, body: Body, pin_id: &str) -> Self {
29        Self {
30            options,
31            body,
32            pin_id: pin_id.to_string(),
33            ..Default::default()
34        }
35    }
36
37    pub fn ad_account_id(mut self, ad_account_id: &str) -> Self {
38        self.ad_account_id = Some(ad_account_id.to_string());
39        self
40    }
41
42    pub fn build(self, bearer_code: &str) -> RequestBuilder {
43        let mut query_parameters = vec![];
44        if let Some(ad_account_id) = self.ad_account_id {
45            query_parameters.push(("ad_account_id", ad_account_id));
46        }
47        let client = reqwest::Client::new()
48            .patch(make_url(
49                &format!("{}/{}/save", URL_PATH, self.pin_id),
50                &self.options,
51            ))
52            .query(&query_parameters)
53            .json(&self.body)
54            .bearer_auth(bearer_code);
55        apply_options(client, &self.options)
56    }
57
58    pub async fn execute(self, bearer_code: &str) -> Result<ApiResponse<Pin>, Error> {
59        execute_api(self.build(bearer_code)).await
60    }
61}
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66
67    // BEARER_CODE=xxx cargo test test_post_pins_pin_id_save -- --nocapture --test-threads=1
68
69    #[tokio::test]
70    async fn test_post_pins_pin_id_save() {
71        let bearer_code: String = std::env::var("BEARER_CODE").unwrap_or_default();
72        let pin_id = std::env::var("PIN_ID").unwrap_or_default();
73        let body = Body {
74            ..Default::default()
75        };
76        let option = ApiOptions {
77            prefix_url: Some("https://api-sandbox.pinterest.com/v5".to_string()),
78            ..Default::default()
79        };
80        let response = Api::new(Some(option), body, &pin_id)
81            .execute(bearer_code.as_str())
82            .await
83            .unwrap();
84        println!("{:?}", response);
85    }
86}