pinterest_api/api/
get_pins.rs

1use reqwest::RequestBuilder;
2
3use crate::{
4    api::{execute_api, ApiResponse},
5    error::Error,
6    options::{apply_options, make_url, ApiOptions},
7    parameter::{creative_type::CreativeType, pin_filter::PinFilter, pin_type::PinType},
8    response::{list_response::ListResponse, pin::Pin},
9};
10
11const URL_PATH: &str = "/pins";
12
13#[derive(Debug, Clone, Default)]
14pub struct Api {
15    options: Option<ApiOptions>,
16    bookmark: Option<String>,
17    page_size: Option<u64>,
18    pin_filter: Option<PinFilter>,
19    include_protected_pins: Option<bool>,
20    pin_type: Option<PinType>,
21    cretive_types: Option<Vec<CreativeType>>,
22    ad_account_id: Option<String>,
23    pin_metrics: Option<bool>,
24}
25
26impl Api {
27    pub fn new(options: Option<ApiOptions>) -> Self {
28        Self {
29            options,
30            ..Default::default()
31        }
32    }
33
34    pub fn bookmark(mut self, bookmark: &str) -> Self {
35        self.bookmark = Some(bookmark.to_string());
36        self
37    }
38
39    pub fn page_size(mut self, page_size: u64) -> Self {
40        self.page_size = Some(page_size);
41        self
42    }
43
44    pub fn pin_filter(mut self, pin_filter: PinFilter) -> Self {
45        self.pin_filter = Some(pin_filter);
46        self
47    }
48
49    pub fn include_protected_pins(mut self, include_protected_pins: bool) -> Self {
50        self.include_protected_pins = Some(include_protected_pins);
51        self
52    }
53
54    pub fn pin_type(mut self, pin_type: PinType) -> Self {
55        self.pin_type = Some(pin_type);
56        self
57    }
58
59    pub fn creative_types(mut self, creative_types: Vec<CreativeType>) -> Self {
60        self.cretive_types = Some(creative_types);
61        self
62    }
63
64    pub fn ad_account_id(mut self, ad_account_id: &str) -> Self {
65        self.ad_account_id = Some(ad_account_id.to_string());
66        self
67    }
68
69    pub fn pin_metrics(mut self, pin_metrics: bool) -> Self {
70        self.pin_metrics = Some(pin_metrics);
71        self
72    }
73
74    pub fn build(self, bearer_code: &str) -> RequestBuilder {
75        let mut query_parameters = vec![];
76        if let Some(ad_account_id) = self.ad_account_id {
77            query_parameters.push(("ad_account_id", ad_account_id));
78        }
79        if let Some(bookmark) = self.bookmark {
80            query_parameters.push(("bookmark", bookmark));
81        }
82        if let Some(page_size) = self.page_size {
83            query_parameters.push(("page_size", page_size.to_string()));
84        }
85        if let Some(pin_filter) = self.pin_filter {
86            query_parameters.push(("pin_filter", pin_filter.to_string()));
87        }
88        if let Some(include_protected_pins) = self.include_protected_pins {
89            query_parameters.push(("include_protected_pins", include_protected_pins.to_string()));
90        }
91        if let Some(pin_type) = self.pin_type {
92            query_parameters.push(("pin_type", pin_type.to_string()));
93        }
94        if let Some(creative_types) = self.cretive_types {
95            query_parameters.push((
96                "creative_types",
97                creative_types
98                    .iter()
99                    .map(|x| x.to_string())
100                    .collect::<Vec<String>>()
101                    .join(","),
102            ));
103        }
104        if let Some(pin_metrics) = self.pin_metrics {
105            query_parameters.push(("pin_metrics", pin_metrics.to_string()));
106        }
107        let client = reqwest::Client::new()
108            .get(make_url(URL_PATH, &self.options))
109            .query(&query_parameters)
110            .bearer_auth(bearer_code);
111        apply_options(client, &self.options)
112    }
113
114    pub async fn execute(self, bearer_code: &str) -> Result<ApiResponse<ListResponse<Pin>>, Error> {
115        execute_api(self.build(bearer_code)).await
116    }
117}
118
119#[cfg(test)]
120mod tests {
121    use super::*;
122
123    // BEARER_CODE=xxx cargo test test_get_pins -- --nocapture --test-threads=1
124
125    #[tokio::test]
126    async fn test_get_pins() {
127        let bearer_code = std::env::var("BEARER_CODE").unwrap_or_default();
128        let response = Api::new(None).execute(bearer_code.as_str()).await.unwrap();
129        println!("{:?}", response);
130    }
131}