librespot/core/src/http_client.rs

95 lines
2.7 KiB
Rust
Raw Normal View History

2021-11-27 07:30:51 +00:00
use bytes::Bytes;
use http::header::HeaderValue;
use http::uri::InvalidUri;
use hyper::header::InvalidHeaderValue;
use hyper::{Body, Client, Request, Response, StatusCode};
2021-06-20 21:09:27 +00:00
use hyper_proxy::{Intercept, Proxy, ProxyConnector};
use hyper_rustls::HttpsConnector;
use thiserror::Error;
2021-06-20 21:09:27 +00:00
use url::Url;
pub struct HttpClient {
proxy: Option<Url>,
}
#[derive(Error, Debug)]
pub enum HttpClientError {
#[error("could not parse request: {0}")]
2021-11-27 07:30:51 +00:00
Parsing(#[from] http::Error),
#[error("could not send request: {0}")]
Request(hyper::Error),
#[error("could not read response: {0}")]
Response(hyper::Error),
#[error("status code: {0}")]
NotOK(u16),
#[error("could not build proxy connector: {0}")]
ProxyBuilder(#[from] std::io::Error),
}
2021-11-27 07:30:51 +00:00
impl From<InvalidHeaderValue> for HttpClientError {
fn from(err: InvalidHeaderValue) -> Self {
Self::Parsing(err.into())
}
}
impl From<InvalidUri> for HttpClientError {
fn from(err: InvalidUri) -> Self {
Self::Parsing(err.into())
}
}
2021-06-20 21:09:27 +00:00
impl HttpClient {
pub fn new(proxy: Option<&Url>) -> Self {
Self {
proxy: proxy.cloned(),
}
}
2021-11-27 07:30:51 +00:00
pub async fn request(&self, mut req: Request<Body>) -> Result<Response<Body>, HttpClientError> {
trace!("Requesting {:?}", req.uri().to_string());
let connector = HttpsConnector::with_native_roots();
2021-11-27 07:30:51 +00:00
let headers_mut = req.headers_mut();
headers_mut.insert(
"User-Agent",
// Some features like lyrics are version-gated and require an official version string.
2021-11-27 07:30:51 +00:00
HeaderValue::from_str("Spotify/8.6.80 iOS/13.5 (iPhone11,2)")?,
);
let response = if let Some(url) = &self.proxy {
let proxy_uri = url.to_string().parse()?;
let proxy = Proxy::new(Intercept::All, proxy_uri);
let proxy_connector = ProxyConnector::from_proxy(connector, proxy)?;
Client::builder()
.build(proxy_connector)
.request(req)
.await
.map_err(HttpClientError::Request)
2021-06-20 21:09:27 +00:00
} else {
Client::builder()
.build(connector)
.request(req)
.await
.map_err(HttpClientError::Request)
};
if let Ok(response) = &response {
let status = response.status();
if status != StatusCode::OK {
return Err(HttpClientError::NotOK(status.into()));
}
2021-06-20 21:09:27 +00:00
}
response
2021-06-20 21:09:27 +00:00
}
2021-11-27 07:30:51 +00:00
pub async fn request_body(&self, req: Request<Body>) -> Result<Bytes, HttpClientError> {
2021-06-20 21:09:27 +00:00
let response = self.request(req).await?;
hyper::body::to_bytes(response.into_body())
.await
.map_err(HttpClientError::Response)
2021-06-20 21:09:27 +00:00
}
}