pinterest_api/api/
get_boards.rs

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