pinterest_api/api/
post_boards.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    ad_account_id: Option<String>,
25    body: Body,
26}
27
28impl Api {
29    pub fn new(options: Option<ApiOptions>, body: Body) -> Self {
30        Self {
31            options,
32            body,
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            .post(make_url(URL_PATH, &self.options))
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<Board>, 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 -- --nocapture --test-threads=1
65
66    #[tokio::test]
67    async fn test_post_boards() {
68        let bearer_code = std::env::var("BEARER_CODE").unwrap_or_default();
69        let body = Body {
70            name: "test".to_owned(),
71            description: Some("テストです".to_owned()),
72            privacy: PrivacyPost::Public,
73        };
74        let response = Api::new(None, body)
75            .execute(bearer_code.as_str())
76            .await
77            .unwrap();
78        println!("{:?}", response);
79    }
80}