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
use serde_json::{json, Value};
use url::Url;

use crate::QQMusicApi;

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

/// set cookie to server with POST
///
/// Content-type: application/json
///
/// { "data": "xxx=xxx; xx=xxxx; ..."}
pub trait SetCookie {
    fn set_cookie(&self, cookie: &str) -> (Url, Value);
}

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

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

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

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