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
use serde_json::json;

use crate::{GETResult, POSTResult, QQMusicApi};

/// set cookie to session
pub trait GetCookie {
    /// id: QQ number or WeChat UIN
    fn get_cookie(&self, id: &str) -> GETResult;
}

/// set cookie to server with POST
pub trait SetCookie {
    fn set_cookie(&self, cookie: &str) -> POSTResult;
}

/// view current cookie set in session
pub trait ViewCookie {
    fn cookie(&self) -> GETResult;
}

impl GetCookie for QQMusicApi {
    fn get_cookie(&self, id: &str) -> GETResult {
        let mut url = self.base_url.clone();
        url.set_path("/user/getCookie");
        url.set_query(Some(&format!("id={id}")));

        http::Request::builder()
            .method("GET")
            .uri(url.as_str())
            .body(())
    }
}

impl ViewCookie for QQMusicApi {
    fn cookie(&self) -> GETResult {
        let mut url = self.base_url.clone();
        url.set_path("/user/cookie");

        http::Request::builder()
            .method("GET")
            .uri(url.as_str())
            .body(())
    }
}

impl SetCookie for QQMusicApi {
    fn set_cookie(&self, cookie: &str) -> POSTResult {
        let mut url = self.base_url.clone();
        url.set_path("/user/setCookie");
        let payload = json!({"data": cookie});

        http::Request::builder()
            .method("POST")
            .uri(url.as_str())
            .header("Content-Type", "application/json")
            .body(payload.to_string())
    }
}