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