pinterest_api/
api.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use reqwest::{RequestBuilder, StatusCode};
use serde::de::DeserializeOwned;
use std::collections::HashMap;

use crate::error::{Error, ErrorResponse};

pub mod delete_boards_board_id;
pub mod delete_boards_board_id_sections_section_id;
pub mod delete_pins_pin_id;
pub mod get_boards;
pub mod get_boards_board_id;
pub mod get_boards_board_id_pins;
pub mod get_boards_board_id_sections;
pub mod get_boards_board_id_sections_section_id_pins;
pub mod get_media;
pub mod get_media_media_id;
pub mod get_pins;
pub mod get_pins_pin_id;
pub mod get_pins_pin_id_analytics;
pub mod get_user_account;
pub mod patch_boards_board_id;
pub mod patch_boards_board_id_sections_section_id;
pub mod patch_pins_pid_id;
pub mod post_boards;
pub mod post_boards_board_id_sections;
pub mod post_media;
pub mod post_pins;
pub mod post_pins_pid_id_save;

#[derive(Debug)]
pub struct ApiResponse<T> {
    pub body: T,
    pub status_code: StatusCode,
    pub header: HashMap<String, String>,
}

pub async fn execute_api<T>(builder: RequestBuilder) -> Result<ApiResponse<T>, Error>
where
    T: DeserializeOwned,
{
    let response = builder.send().await?;
    let status_code = response.status();
    let header = response
        .headers()
        .iter()
        .map(|(k, v)| (k.to_string(), v.to_str().unwrap().to_string()))
        .collect();
    let text = match response.text().await {
        Ok(text) => text,
        Err(err) => return Err(Error::Other(format!("{:?}", err), status_code)),
    };
    let json = match serde_json::from_str::<serde_json::Value>(&text) {
        Ok(json) => json,
        Err(_err) => return Err(Error::Other(text, status_code)),
    };
    if let Some(code) = json["code"].as_i64() {
        return Err(Error::Api(
            ErrorResponse {
                code,
                message: json["message"].as_str().unwrap_or_default().to_owned(),
            },
            status_code,
        ));
    }
    match serde_json::from_value::<T>(json) {
        Ok(json) => Ok(ApiResponse {
            body: json,
            status_code,
            header,
        }),
        Err(err) => Err(Error::Other(format!("{:?},{}", err, text), status_code)),
    }
}

#[derive(Debug)]
pub struct ApiEmptyResponse {
    pub status_code: StatusCode,
    pub header: HashMap<String, String>,
}

pub async fn execute_empty_api(builder: RequestBuilder) -> Result<ApiEmptyResponse, Error> {
    let response = builder.send().await?;
    let status_code = response.status();
    let header = response
        .headers()
        .iter()
        .map(|(k, v)| (k.to_string(), v.to_str().unwrap().to_string()))
        .collect();
    let text = match response.text().await {
        Ok(text) => text,
        Err(err) => return Err(Error::Other(format!("{:?}", err), status_code)),
    };
    if text.is_empty() {
        return Ok(ApiEmptyResponse {
            status_code,
            header,
        });
    }
    let json = match serde_json::from_str::<serde_json::Value>(&text) {
        Ok(json) => json,
        Err(_err) => return Err(Error::Other(text, status_code)),
    };
    if let Some(code) = json["code"].as_i64() {
        return Err(Error::Api(
            ErrorResponse {
                code,
                message: json["message"].as_str().unwrap_or_default().to_owned(),
            },
            status_code,
        ));
    }
    Err(Error::Other(text, status_code))
}