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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use futures::future::Future;
use reqwest::multipart::Form;
use crate::longpoll::Longpoll;
use crate::params::Params;
use crate::types::destination::Destination;
use crate::types::file::File;
type Request = String;
pub struct VK<'a> {
access_token: Option<String>,
api_version: &'a str,
language: &'a str,
}
impl<'a> VK<'a> {
pub fn new(api_version: &'a str, language: &'a str) -> Self {
Self {
access_token: None,
api_version,
language,
}
}
pub fn set_access_token(&mut self, token: String) {
self.access_token = Some(token);
}
pub fn get_access_token(&self) -> &String {
self.access_token.as_ref().unwrap()
}
pub async fn start_longpoll(
&self,
group_id: u32,
wait: u16,
callback: Box<dyn Fn(&json::JsonValue) -> ()>,
) {
let access_token = self.access_token.clone();
let longpoll = Longpoll::new(
group_id,
wait,
access_token,
self.api_version.to_string().clone(),
self.language.to_string().clone(),
);
longpoll.start(callback).await;
}
pub async fn upload(&self, file: File, params: Params) -> Result<json::JsonValue, String> {
let mut form = Form::new();
let mut params = Params::new();
let upload = self
.request(file.destination.pick_method_load(), &mut params)
.await?;
let upload_url = upload["response"]["upload_url"]
.as_str()
.unwrap()
.to_owned();
let param_for_sending = file.destination.pick_param();
form = form.part(
param_for_sending,
crate::utils::file_to_part(file.path.clone()).await,
);
let method_for_saving = file.destination.pick_method_save();
let client = reqwest::Client::new();
let res = client
.post(&upload_url)
.multipart(form)
.send()
.await
.unwrap()
.text()
.await
.unwrap();
let mut upload_info = json::parse(&res).unwrap();
if method_for_saving != "none" {
let mut params = Params::new();
crate::utils::make_params_for_saving_photo(&mut params, upload_info);
let mut saved_file = self.request(method_for_saving, &mut params).await?;
return Ok(saved_file.remove("response"));
}
Ok(upload_info.remove("response"))
}
pub async fn request(
&self,
method: &str,
params: &mut Params,
) -> std::result::Result<json::JsonValue, String> {
let request_url = self.build_request(method, params);
let response = reqwest::get(&request_url)
.await
.unwrap()
.text()
.await
.unwrap();
let parsed = json::parse(&response).unwrap();
if !parsed["error"].is_null() {
return Err(parsed["error"]["error_msg"].as_str().unwrap().to_owned());
}
Ok(parsed)
}
pub async fn request_public(
method: &str,
params: &mut Params,
access_token: &Option<String>,
api_version: &str,
language: &str,
) -> std::result::Result<json::JsonValue, String> {
let request_url = {
if access_token.is_none() {
panic!("Access token is empty! Did you forget to call set_access_token() ?");
}
let access_token = access_token.as_ref().unwrap();
let result = format!(
"https://api.vk.com/method/{}?{}access_token={}&v={}&lang={}",
method,
¶ms.concat(),
&access_token,
api_version,
language
);
result
};
let response = reqwest::get(&request_url)
.await
.unwrap()
.text()
.await
.unwrap();
let parsed = json::parse(&response).unwrap();
if !parsed["error"].is_null() {
return Err(parsed["error"]["error_msg"].as_str().unwrap().to_owned());
}
Ok(parsed)
}
pub async fn direct_auth(
&mut self,
login: &str,
password: &str,
) -> Result<(), json::JsonValue> {
let url = format!("https://api.vk.com/oauth/token?grant_type=password&client_id=2274003&scope=notify,photos,friends,audio,video,notes,pages,docs,status,questions,offers,wall,groups,messages,notifications,stats,ads,offline&client_secret=hHbZxrka2uZ6jB1inYsH&username={}&password={}", login, password);
let response = reqwest::get(&url).await.unwrap().text().await.unwrap();
let parsed = json::parse(&response).unwrap();
if parsed["access_token"].is_null() {
panic!("Authencication has failed! We doesnt support 2fa right now");
}
let access_token = parsed["access_token"].as_str().unwrap();
let access_token = access_token.to_string();
self.set_access_token(access_token);
Ok(())
}
fn build_request(&self, method: &str, params: &mut Params) -> Request {
let access_token = self.access_token.as_ref();
if access_token.is_none() {
panic!("Access token is empty! Did you forget to call set_access_token() ?");
}
let access_token = access_token.unwrap();
let result = format!(
"https://api.vk.com/method/{}?{}access_token={}&v={}&lang={}",
method,
¶ms.concat(),
&access_token,
&self.api_version,
&self.language
);
result
}
}