2016-03-13 20:03:09 +00:00
|
|
|
const APRESOLVE_ENDPOINT : &'static str = "http://apresolve.spotify.com/";
|
2016-12-30 11:01:43 +00:00
|
|
|
const AP_FALLBACK : &'static str = "ap.spotify.com:80";
|
2016-03-13 20:03:09 +00:00
|
|
|
|
|
|
|
use hyper;
|
|
|
|
use std::io::Read;
|
2016-07-06 01:29:38 +00:00
|
|
|
use serde_json;
|
2016-03-13 20:03:09 +00:00
|
|
|
|
2016-07-06 01:29:38 +00:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
2016-03-13 20:03:09 +00:00
|
|
|
pub struct APResolveData {
|
|
|
|
ap_list: Vec<String>
|
|
|
|
}
|
|
|
|
|
2016-12-30 11:01:43 +00:00
|
|
|
pub fn apresolve() -> String {
|
2016-03-13 20:03:09 +00:00
|
|
|
let client = hyper::client::Client::new();
|
2016-12-30 11:01:43 +00:00
|
|
|
(|| {
|
|
|
|
let mut response = client.get(APRESOLVE_ENDPOINT).send().map_err(|_| ())?;
|
|
|
|
let mut data = String::new();
|
|
|
|
response.read_to_string(&mut data).map_err(|_| ())?;
|
2016-03-13 20:03:09 +00:00
|
|
|
|
2016-12-30 11:01:43 +00:00
|
|
|
let data : APResolveData = serde_json::from_str(&data).map_err(|_| ())?;
|
|
|
|
data.ap_list.first().map(Clone::clone).ok_or(())
|
|
|
|
})().unwrap_or_else(|_| {
|
|
|
|
warn!("failed to resolve AP, using fallback");
|
|
|
|
AP_FALLBACK.into()
|
|
|
|
})
|
2016-03-13 20:03:09 +00:00
|
|
|
}
|