Return HttpClientError for status code <> 200

This commit is contained in:
Roderick van Domburg 2021-11-27 10:41:54 +01:00
parent e1b273b8a1
commit a73e05837e
No known key found for this signature in database
GPG key ID: FE2585E713F9F30A

View file

@ -20,6 +20,8 @@ pub enum HttpClientError {
Request(hyper::Error), Request(hyper::Error),
#[error("could not read response: {0}")] #[error("could not read response: {0}")]
Response(hyper::Error), Response(hyper::Error),
#[error("status code: {0}")]
NotOK(u16),
#[error("could not build proxy connector: {0}")] #[error("could not build proxy connector: {0}")]
ProxyBuilder(#[from] std::io::Error), ProxyBuilder(#[from] std::io::Error),
} }
@ -44,19 +46,20 @@ impl HttpClient {
} }
pub async fn request(&self, mut req: Request<Body>) -> Result<Response<Body>, HttpClientError> { 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(); let connector = HttpsConnector::with_native_roots();
let uri = req.uri().clone();
let headers_mut = req.headers_mut(); let headers_mut = req.headers_mut();
headers_mut.insert( headers_mut.insert(
"User-Agent", "User-Agent",
// Some features like lyrics are version-gated and require a "real" version string. // Some features like lyrics are version-gated and require an official version string.
HeaderValue::from_str("Spotify/8.6.80 iOS/13.5 (iPhone11,2)")?, HeaderValue::from_str("Spotify/8.6.80 iOS/13.5 (iPhone11,2)")?,
); );
let response = if let Some(url) = &self.proxy { let response = if let Some(url) = &self.proxy {
let uri = url.to_string().parse()?; let proxy_uri = url.to_string().parse()?;
let proxy = Proxy::new(Intercept::All, uri); let proxy = Proxy::new(Intercept::All, proxy_uri);
let proxy_connector = ProxyConnector::from_proxy(connector, proxy)?; let proxy_connector = ProxyConnector::from_proxy(connector, proxy)?;
Client::builder() Client::builder()
@ -73,8 +76,9 @@ impl HttpClient {
}; };
if let Ok(response) = &response { if let Ok(response) = &response {
if response.status() != StatusCode::OK { let status = response.status();
debug!("{} returned status {}", uri, response.status()); if status != StatusCode::OK {
return Err(HttpClientError::NotOK(status.into()));
} }
} }