pinterest_api/api/
post_media.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::media_response::{MediaPostResponse, MediaType},
9};
10
11const URL_PATH: &str = "/media";
12
13#[derive(Serialize, Deserialize, Debug, Default, Clone)]
14pub struct Body {
15    pub media_type: MediaType,
16}
17
18#[derive(Debug, Clone, Default)]
19pub struct Api {
20    options: Option<ApiOptions>,
21    body: Body,
22}
23
24impl Api {
25    pub fn new(options: Option<ApiOptions>, body: Body) -> Self {
26        Self { options, body }
27    }
28
29    pub fn build(self, bearer_code: &str) -> RequestBuilder {
30        let client = reqwest::Client::new()
31            .post(make_url(URL_PATH, &self.options))
32            .json(&self.body)
33            .bearer_auth(bearer_code);
34        apply_options(client, &self.options)
35    }
36
37    pub async fn execute(self, bearer_code: &str) -> Result<ApiResponse<MediaPostResponse>, Error> {
38        execute_api(self.build(bearer_code)).await
39    }
40}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45
46    // BEARER_CODE=xxx cargo test test_post_media -- --nocapture --test-threads=1
47
48    #[tokio::test]
49    async fn test_post_media() {
50        let bearer_code = std::env::var("BEARER_CODE").unwrap_or_default();
51        let body = Body::default();
52        let response = Api::new(None, body)
53            .execute(bearer_code.as_str())
54            .await
55            .unwrap();
56        println!("{:?}", response);
57    }
58}