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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use crate::{
    media_container::player::ResourcesMediaContainer,
    url::{CLIENT_RESOURCES, SERVER_SYSTEM_PROXY},
    HttpClient, HttpClientBuilder, MyPlex, Result, Server,
};
use http::{uri::PathAndQuery, Uri};
use std::{
    convert::TryFrom,
    fmt::{Debug, Display},
};

#[derive(Debug, Clone)]
pub struct Player {
    client: HttpClient,
    _media_container: ResourcesMediaContainer,
    _last_command_id: u64,
    pub myplex_api_url: Uri,
}

impl Player {
    #[tracing::instrument(level = "debug", skip(client))]
    pub async fn new<U>(url: U, client: HttpClient) -> Result<Self>
    where
        U: Debug,
        Uri: TryFrom<U>,
        <Uri as TryFrom<U>>::Error: Into<http::Error>,
    {
        let myplex_api_url = client.api_url.clone();
        Ok(Self {
            _media_container: client
                .get(CLIENT_RESOURCES)
                .header("Accept", "application/xml")
                .xml()
                .await?,
            client,
            myplex_api_url,
            _last_command_id: 0,
        })
    }

    #[tracing::instrument(level = "debug", skip(server))]
    pub async fn via_proxy<U>(url: U, server: &Server) -> Result<Self>
    where
        U: Display + Debug,
        Uri: TryFrom<U>,
        <Uri as TryFrom<U>>::Error: Into<http::Error>,
    {
        let mut client = server.client().clone();

        let path_and_query =
            PathAndQuery::try_from(CLIENT_RESOURCES).map_err(Into::<http::Error>::into)?;
        let mut uri_parts = Uri::try_from(url)
            .map_err(Into::<http::Error>::into)?
            .into_parts();
        uri_parts.path_and_query = Some(path_and_query);
        let uri = Uri::from_parts(uri_parts).map_err(Into::<http::Error>::into)?;

        let media_container: ResourcesMediaContainer = client
            .get(SERVER_SYSTEM_PROXY)
            .header("X-Plex-Url", dbg!(format!("{uri}")))
            .xml()
            .await?;
        client.x_plex_target_client_identifier = media_container.player.machine_identifier.clone();
        Ok(Self {
            _media_container: media_container,
            client,
            myplex_api_url: server.myplex_api_url.clone(),
            _last_command_id: 0,
        })
    }

    pub fn myplex(&self) -> Result<MyPlex> {
        self.myplex_with_api_url(self.myplex_api_url.clone())
    }

    pub fn myplex_with_api_url<U>(&self, api_url: U) -> Result<MyPlex>
    where
        Uri: TryFrom<U>,
        <Uri as TryFrom<U>>::Error: Into<http::Error>,
    {
        Ok(MyPlex::new(
            HttpClientBuilder::from(self.client.clone())
                .set_api_url(api_url)
                .build()?,
        ))
    }

    pub fn client(&self) -> &HttpClient {
        &self.client
    }
}