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
#[cfg(feature = "headers")]
use reqwest::header::HeaderMap;

use reqwest::{Response, Url};
use serde::de::DeserializeOwned;

use crate::{JellyfinSDKError, JellyfinSDKResult};

/// Structure containing the request status code and a `Result` that may contain the body.
pub struct JellyfinResponse<T> {
    status: u16,
    url: Url,
    #[cfg(feature = "headers")]
    headers: HeaderMap,
    body: Result<T, reqwest::Error>,
}

impl<T> JellyfinResponse<T> {
    pub fn status(&self) -> u16 {
        self.status
    }

    pub fn url(&self) -> &Url {
        &self.url
    }

    pub fn body(&self) -> &Result<T, reqwest::Error> {
        &self.body
    }

    #[cfg(feature = "headers")]
    pub fn headers(&self) -> &HeaderMap {
        &self.headers
    }

    pub(crate) async fn async_from(response: Response) -> JellyfinSDKResult<Self>
    where
        T: DeserializeOwned,
        T: 'static,
    {
        let status = response.status().as_u16();

        if response.status().is_success() {
            Ok(JellyfinResponse {
                status,
                url: response.url().clone(),
                #[cfg(feature = "headers")]
                headers: response.headers().clone(),
                body: response.json::<T>().await,
            })
        } else {
            Err(JellyfinSDKError::HttpResponseError(status))
        }
    }
}