jellyfin_sdk_rust/api/
jellyfin_api.rs

1use reqwest::header::{HeaderMap, CONNECTION};
2
3/// Structure managing all API interactions.
4pub struct JellyfinAPI {
5    async_client: reqwest::Client,
6    #[cfg(feature = "sync")]
7    sync_client: reqwest::blocking::Client,
8    base_addr: String,
9}
10
11impl JellyfinAPI {
12    pub(crate) fn new<S: Into<String>>(base_addr: S, token: Option<&str>) -> Self {
13        static USER_AGENT: &str = concat!("Jellyfin SDK - ", env!("CARGO_PKG_VERSION"));
14        // TODO
15        // - Version to get dynamically
16        // - Test unauthenticated routes
17
18        let mut headers = HeaderMap::new();
19
20        headers.insert(CONNECTION, "keep-alive".parse().unwrap());
21
22        match token {
23            Some(t) => {
24                headers.insert("X-Emby-Token", t.parse().unwrap());
25            }
26            None => {
27                headers.insert(
28                    "X-Emby-Authorization",
29                    r#"MediaBrowser Client="Jellyfin SDK", Version="10.7.7"#
30                        .parse()
31                        .unwrap(),
32                );
33            }
34        }
35
36        let async_client = reqwest::Client::builder()
37            .user_agent(USER_AGENT)
38            .default_headers(headers)
39            .build()
40            .unwrap();
41
42        #[cfg(feature = "sync")]
43        let sync_client = reqwest::blocking::Client::builder()
44            .user_agent(USER_AGENT)
45            .default_headers(headers)
46            .build()
47            .unwrap();
48
49        Self {
50            async_client,
51            #[cfg(feature = "sync")]
52            sync_client,
53            base_addr: base_addr.into(),
54        }
55    }
56
57    pub(crate) fn async_client(&self) -> &reqwest::Client {
58        &self.async_client
59    }
60
61    #[cfg(feature = "sync")]
62    pub(crate) fn sync_client(&self) -> &reqwest::blocking::Client {
63        &self.sync_client
64    }
65
66    pub(crate) fn base_addr(&self) -> &String {
67        &self.base_addr
68    }
69}