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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use http_api_client_endpoint::{
http::{
header::{ACCEPT, USER_AGENT},
Method,
},
Body, Endpoint, Request, Response,
};
use percent_encoding::percent_encode;
use serde::{Deserialize, Serialize};
use crate::objects::User;
use super::{
common::{
endpoint_parse_response, EndpointError, EndpointRet, URL_PERCENT_ENCODE_ASCII_SET,
URL_PREFIX,
},
user_medias::{UserMediasResponseBody, MEDIA_FIELDS},
};
#[derive(Debug, Clone)]
pub struct UserEndpoint {
user_id: String,
access_token: String,
with_media: bool,
}
impl UserEndpoint {
pub fn new(user_id: u64, access_token: String, with_media: bool) -> Self {
Self {
user_id: user_id.to_string(),
access_token,
with_media,
}
}
pub fn me(access_token: String, with_media: bool) -> Self {
Self {
user_id: "me".to_owned(),
access_token,
with_media,
}
}
}
impl Endpoint for UserEndpoint {
type RenderRequestError = EndpointError;
type ParseResponseOutput = EndpointRet<UserResponseBody>;
type ParseResponseError = EndpointError;
#[allow(clippy::vec_init_then_push)]
fn render_request(&self) -> Result<Request<Body>, Self::RenderRequestError> {
let media_fields = format!("media.fields({})", MEDIA_FIELDS.join(","));
let mut fields = vec!["account_type", "id", "username"];
if self.with_media {
fields.push("media_count");
fields.push(media_fields.as_str());
}
let fields = fields.join(",");
let mut query_pairs = vec![];
query_pairs.push((
"fields",
percent_encode(fields.as_bytes(), URL_PERCENT_ENCODE_ASCII_SET).to_string(),
));
query_pairs.push(("access_token", self.access_token.to_owned()));
let url = format!(
"{}/{}?{}",
URL_PREFIX,
self.user_id,
query_pairs
.into_iter()
.map(|x| format!("{}={}", x.0, x.1))
.collect::<Vec<String>>()
.join("&"),
);
let request = Request::builder()
.method(Method::GET)
.uri(url.as_str())
.header(USER_AGENT, "curl/7.72.0")
.header(ACCEPT, "application/json")
.body(vec![])
.map_err(EndpointError::MakeRequestFailed)?;
Ok(request)
}
fn parse_response(
&self,
response: Response<Body>,
) -> Result<Self::ParseResponseOutput, Self::ParseResponseError> {
endpoint_parse_response(response)
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct UserResponseBody {
#[serde(flatten)]
pub basic: User,
#[serde(default)]
pub media: Option<UserMediasResponseBody>,
}
#[cfg(test)]
mod tests {
use super::*;
use crate::objects::AccountType;
#[test]
fn test_render_request() {
let req = UserEndpoint::new(123, "TOKEN".to_owned(), false)
.render_request()
.unwrap();
assert_eq!(req.method(), Method::GET);
assert_eq!(req.uri(), "https://graph.instagram.com/v12.0/123?fields=account_type%2Cid%2Cusername&access_token=TOKEN");
let req = UserEndpoint::me("TOKEN".to_owned(), false)
.render_request()
.unwrap();
assert_eq!(req.method(), Method::GET);
assert_eq!(req.uri(), "https://graph.instagram.com/v12.0/me?fields=account_type%2Cid%2Cusername&access_token=TOKEN");
}
#[test]
fn test_de_response_body() {
let body = serde_json::from_str::<UserResponseBody>(include_str!(
"../../tests/response_body_files/me_with_media_ok.json"
))
.unwrap();
assert_eq!(body.basic.account_type, AccountType::Business);
assert_eq!(body.basic.id, 6489782497758472);
assert_eq!(body.media.unwrap().data.len(), 11);
let body = serde_json::from_str::<UserResponseBody>(include_str!(
"../../tests/response_body_files/me_without_media_ok.json"
))
.unwrap();
assert_eq!(body.basic.account_type, AccountType::Business);
assert_eq!(body.basic.id, 6489782497758472);
assert!(body.basic.media_count.is_none());
assert!(body.media.is_none());
}
}