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
/// Request builder and longpoll connection
use super::{extract::Update, responses::LongPollSession, Error, Result};
use serde::Serialize;
use serde_json::Value;

pub struct RequestBuilder {
    client: reqwest::Client,
    access_token: String,
    group_id: u32,
}

pub const VK: &'static str = "https://api.vk.com/method";
pub const VERSION: &'static str = "5.199";

macro_rules! request {
    ($fn1:ident) => {
        pub async fn $fn1<T: Serialize + Send, A: Serialize + Send + Sized>(
            &self,
            url: &str,
            method: &str,
            query: A,
            body: T,
        ) -> std::result::Result<Value, reqwest::Error> {
            let response = self
                .client
                .post(&if method.is_empty() {
                    format!("{}?v={}", url, VERSION)
                } else {
                    format!("{}/{}?v={}", url, method, VERSION)
                })
                .query(&query)
                .bearer_auth(&self.access_token)
                .json(&body)
                .send()
                .await?;

            let json: Value = response.json().await?;

            Ok(json)
        }
    };
}

impl RequestBuilder {
    pub fn new(access_token: &str, group_id: u32) -> Self {
        RequestBuilder {
            client: reqwest::Client::new(),
            access_token: access_token.to_string(),
            group_id,
        }
    }

    pub async fn build_long_poll_request(&self) -> Result<Update> {
        let response = self
            .post(
                VK,
                "groups.getLongPollServer",
                &[("group_id", self.group_id)],
                {},
            )
            .await
            .map_err(|e| Error::Reqwest(e))?;

        let parsed_response =
            crate::parse_response!(response, LongPollSession).map_err(|e| Error::SerdeJson(e))?;

        let response = self
            .post(
                &parsed_response.server,
                "",
                &[
                    ("act", String::from("a_check")),
                    ("key", parsed_response.key),
                    ("ts", parsed_response.ts),
                    ("wait", String::from("25")),
                ],
                {},
            )
            .await
            .map_err(|e| Error::Reqwest(e))?;

        let parsed_response =
            crate::parse_response!(response, Update).map_err(|e| Error::SerdeJson(e))?;

        match parsed_response.failed {
            Some(1) => Err(Error::EventsOutdated {
                new_ts: parsed_response.ts.unwrap(),
            }),
            Some(2) => Err(Error::KeyExpired),
            Some(3) => Err(Error::InformationLost),
            _ => Ok(parsed_response),
        }
    }

    request!(post);
    request!(get);
}