pinterest_api/api/
get_user_account.rs

1use crate::{
2    api::{execute_api, ApiResponse},
3    error::Error,
4    options::{apply_options, make_url, ApiOptions},
5    response::user_account::UserAccount,
6};
7use reqwest::RequestBuilder;
8
9const URL_PATH: &str = "/user_account";
10
11#[derive(Debug, Clone, Default)]
12pub struct Api {
13    options: Option<ApiOptions>,
14    ad_account_id: Option<String>,
15}
16
17impl Api {
18    pub fn new(options: Option<ApiOptions>) -> Self {
19        Self {
20            options,
21            ..Default::default()
22        }
23    }
24
25    pub fn ad_account_id(mut self, ad_account_id: &str) -> Self {
26        self.ad_account_id = Some(ad_account_id.to_string());
27        self
28    }
29
30    pub fn build(self, bearer_code: &str) -> RequestBuilder {
31        let mut query_parameters = vec![];
32        if let Some(ad_account_id) = self.ad_account_id {
33            query_parameters.push(("ad_account_id", ad_account_id));
34        }
35        let client = reqwest::Client::new()
36            .get(make_url(URL_PATH, &self.options))
37            .query(&query_parameters)
38            .bearer_auth(bearer_code);
39        apply_options(client, &self.options)
40    }
41
42    pub async fn execute(self, bearer_code: &str) -> Result<ApiResponse<UserAccount>, Error> {
43        execute_api(self.build(bearer_code)).await
44    }
45}
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50
51    // BEARER_CODE=xxx cargo test test_get_user_account -- --nocapture --test-threads=1
52
53    #[tokio::test]
54    async fn test_get_user_account() {
55        let bearer_code = std::env::var("BEARER_CODE").unwrap_or_default();
56        let response = Api::new(None).execute(bearer_code.as_str()).await.unwrap();
57        println!("{:?}", response);
58    }
59}