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
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// This mod is almost completely copied from https://github.com/danielrs/pandora-rs/. I've simply replaced the use of hyper with reqwest and updated syntax slightly.
#[macro_use]
extern crate serde_derive;
extern crate serde;
extern crate serde_json;

use reqwest::Client;

pub mod auth;
pub mod crypt;
pub mod error;
pub mod method;
pub mod music;
pub mod playlist;
pub mod request;
pub mod response;
pub mod stations;

pub use auth::Credentials;
pub use playlist::Track;
pub use stations::Stations;

use serde::de::DeserializeOwned;
use serde_json::value::Value;

use error::{Error, Result};
use method::Method;
use request::request;

use std::cell::RefCell;
use std::sync::Mutex;

#[derive(Debug)]
pub struct Pandora {
    client: Client,
    endpoint: Endpoint<'static>,
    credentials: Mutex<RefCell<Credentials>>,
}

impl Pandora {
    pub fn new(username: &str, password: &str) -> Result<Self> {
        let creds = Credentials::new(username, password)?;
        Ok(Pandora::with_credentials(creds))
    }

    pub fn with_credentials(credentials: Credentials) -> Self {
        Pandora {
            client: Client::new(),
            endpoint: DEFAULT_ENDPOINT,
            credentials: Mutex::new(RefCell::new(credentials)),
        }
    }

    pub fn stations(&self) -> Stations {
        Stations::new(self)
    }

    pub fn request<T>(&self, method: Method, body: Option<Value>) -> Result<T>
    where
        T: DeserializeOwned,
    {
        let credentials = self.credentials.lock().unwrap();
        let req = request(
            &self.client,
            self.endpoint,
            method.clone(),
            body.clone(),
            Some(&credentials.borrow()),
        );

        match req {
            Ok(res) => Ok(res),
            Err(err) => {
                if credentials.borrow_mut().refresh().is_err() {
                    return Err(err);
                }
                request(
                    &self.client,
                    self.endpoint,
                    method,
                    body,
                    Some(&credentials.borrow()),
                )
            }
        }
    }

    pub fn request_noop(&self, method: Method, body: Option<Value>) -> Result<()> {
        let credentials = self.credentials.lock().unwrap();

        let req = request::<()>(
            &self.client,
            self.endpoint,
            method.clone(),
            body.clone(),
            Some(&credentials.borrow()),
        );

        match req {
            Ok(_) | Err(Error::Codec(_)) => Ok(()),
            Err(err) => {
                if credentials.borrow_mut().refresh().is_err() {
                    return Err(err);
                }
                let req = request::<()>(
                    &self.client,
                    self.endpoint,
                    method,
                    body,
                    Some(&credentials.borrow()),
                );
                match req {
                    Ok(_) | Err(Error::Codec(_)) => Ok(()),
                    Err(err) => Err(err),
                }
            }
        }
    }
}

#[derive(Debug, Copy, Clone)]
pub struct Endpoint<'a>(&'a str);

impl<'a> ToString for Endpoint<'a> {
    fn to_string(&self) -> String {
        let Endpoint(url) = *self;
        url.to_owned()
    }
}

pub const ENDPOINTS: [Endpoint<'static>; 4] = [
    Endpoint("http://tuner.pandora.com/services/json/"),
    Endpoint("https://tuner.pandora.com/services/json/"),
    Endpoint("http://internal-tuner.pandora.com/services/json/"),
    Endpoint("https://internal-tuner.pandora.com/services/json/"),
];
pub const DEFAULT_ENDPOINT: Endpoint<'static> = ENDPOINTS[1];