librespot/core/src/keymaster.rs

33 lines
834 B
Rust
Raw Normal View History

use futures::Future;
2017-05-10 15:26:48 +00:00
use serde_json;
2017-08-03 18:58:44 +00:00
2018-02-06 18:54:28 +00:00
use mercury::MercuryError;
use session::Session;
2017-05-10 15:26:48 +00:00
#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Token {
pub access_token: String,
pub expires_in: u32,
pub token_type: String,
pub scope: Vec<String>,
}
2018-02-11 11:37:08 +00:00
pub fn get_token(
session: &Session,
client_id: &str,
scopes: &str,
) -> Box<Future<Item = Token, Error = MercuryError>> {
let url = format!(
"hm://keymaster/token/authenticated?client_id={}&scope={}",
client_id, scopes
);
Box::new(session.mercury().get(url).map(move |response| {
2017-05-10 15:26:48 +00:00
let data = response.payload.first().expect("Empty payload");
let data = String::from_utf8(data.clone()).unwrap();
2018-02-11 11:37:08 +00:00
let token: Token = serde_json::from_str(&data).unwrap();
2017-05-10 15:26:48 +00:00
token
}))
2017-05-10 15:26:48 +00:00
}