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