apresolve.rs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. const AP_FALLBACK : &'static str = "ap.spotify.com:80";
  2. const APRESOLVE_ENDPOINT : &'static str = "http://apresolve.spotify.com/";
  3. use futures::{Future, Stream};
  4. use hyper::{self, Url, Client};
  5. use serde_json;
  6. use tokio_core::reactor::Handle;
  7. error_chain! { }
  8. #[derive(Clone, Debug, Serialize, Deserialize)]
  9. pub struct APResolveData {
  10. ap_list: Vec<String>
  11. }
  12. pub fn apresolve(handle: &Handle) -> Box<Future<Item=String, Error=Error>> {
  13. let url = Url::parse(APRESOLVE_ENDPOINT).expect("invalid AP resolve URL");
  14. let client = Client::new(&handle);
  15. let response = client.get(url);
  16. let body = response.and_then(|response| {
  17. response.body().fold(Vec::new(), |mut acc, chunk| {
  18. acc.extend_from_slice(chunk.as_ref());
  19. Ok::<_, hyper::Error>(acc)
  20. })
  21. });
  22. let body = body.then(|result| result.chain_err(|| "HTTP error"));
  23. let body = body.and_then(|body| {
  24. String::from_utf8(body).chain_err(|| "invalid UTF8 in response")
  25. });
  26. let data = body.and_then(|body| {
  27. serde_json::from_str::<APResolveData>(&body)
  28. .chain_err(|| "invalid JSON")
  29. });
  30. let ap = data.and_then(|data| {
  31. let ap = data.ap_list.first().ok_or("empty AP List")?;
  32. Ok(ap.clone())
  33. });
  34. Box::new(ap)
  35. }
  36. pub fn apresolve_or_fallback<E>(handle: &Handle)
  37. -> Box<Future<Item=String, Error=E>>
  38. where E: 'static
  39. {
  40. let ap = apresolve(handle).or_else(|e| {
  41. warn!("Failed to resolve Access Point: {}", e.description());
  42. warn!("Using fallback \"{}\"", AP_FALLBACK);
  43. Ok(AP_FALLBACK.into())
  44. });
  45. Box::new(ap)
  46. }