pinterest_api/api/
patch_boards_board_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::privacy_post::PrivacyPost,
9    response::board::Board,
10};
11
12const URL_PATH: &str = "/boards";
13
14#[derive(Serialize, Deserialize, Debug, Default, Clone)]
15pub struct Body {
16    pub name: String,
17    pub description: Option<String>,
18    pub privacy: PrivacyPost,
19}
20
21#[derive(Debug, Clone, Default)]
22pub struct Api {
23    options: Option<ApiOptions>,
24    board_id: String,
25    ad_account_id: Option<String>,
26    body: Body,
27}
28
29impl Api {
30    pub fn new(options: Option<ApiOptions>, body: Body, board_id: &str) -> Self {
31        Self {
32            options,
33            body,
34            board_id: board_id.to_string(),
35            ..Default::default()
36        }
37    }
38
39    pub fn ad_account_id(mut self, ad_account_id: &str) -> Self {
40        self.ad_account_id = Some(ad_account_id.to_string());
41        self
42    }
43
44    pub fn build(self, bearer_code: &str) -> RequestBuilder {
45        let mut query_parameters = vec![];
46        if let Some(ad_account_id) = self.ad_account_id {
47            query_parameters.push(("ad_account_id", ad_account_id));
48        }
49        let client = reqwest::Client::new()
50            .patch(make_url(
51                &format!("{}/{}", URL_PATH, self.board_id),
52                &self.options,
53            ))
54            .query(&query_parameters)
55            .json(&self.body)
56            .bearer_auth(bearer_code);
57        apply_options(client, &self.options)
58    }
59
60    pub async fn execute(self, bearer_code: &str) -> Result<ApiResponse<Board>, Error> {
61        execute_api(self.build(bearer_code)).await
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68
69    // BEARER_CODE=xxx BOARD_ID=xxx cargo test test_patch_boards_board_id -- --nocapture --test-threads=1
70
71    #[tokio::test]
72    async fn test_patch_boards_board_id() {
73        let bearer_code = std::env::var("BEARER_CODE").unwrap_or_default();
74        let board_id = std::env::var("BOARD_ID").unwrap_or_default();
75        let body = Body {
76            name: "test".to_owned(),
77            description: Some("テストです".to_owned()),
78            privacy: PrivacyPost::Public,
79        };
80        let response = Api::new(None, body, &board_id)
81            .execute(bearer_code.as_str())
82            .await
83            .unwrap();
84        println!("{:?}", response);
85    }
86}