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
use crate::media_container::MediaContainer;
use crate::server::Server;
use crate::{base_headers, get_http_client};
impl Server {
pub fn connect(url: &str) -> crate::Result<Self> {
let mut response = get_http_client()?.get(url).headers(base_headers()).send()?;
if response.status() == reqwest::StatusCode::OK {
let mc: MediaContainer = serde_xml_rs::from_str(response.text()?.as_str())?;
Ok(Server {
info: mc,
url: String::from(url),
auth_token: String::from(""),
})
} else {
eprintln!("{:?}", response.text()?);
Err(crate::error::PlexApiError {})
}
}
pub fn login(url: &str, auth_token: &str) -> crate::Result<Self> {
let mut response = get_http_client()?
.get(url)
.headers(base_headers())
.header("X-Plex-Token", auth_token)
.send()?;
if response.status() == reqwest::StatusCode::OK {
let mc: MediaContainer = serde_xml_rs::from_str(response.text()?.as_str())?;
Ok(Server {
info: mc,
url: String::from(url),
auth_token: String::from(auth_token),
})
} else {
eprintln!("{:?}", response.text()?);
Err(crate::error::PlexApiError {})
}
}
}