mirror of
https://github.com/librespot-org/librespot.git
synced 2024-12-18 17:11:53 +00:00
35 lines
1.1 KiB
Rust
35 lines
1.1 KiB
Rust
|
use hyper::client::HttpConnector;
|
||
|
use hyper::{Body, Client, Request, Response};
|
||
|
use hyper_proxy::{Intercept, Proxy, ProxyConnector};
|
||
|
use url::Url;
|
||
|
|
||
|
pub struct HttpClient {
|
||
|
proxy: Option<Url>,
|
||
|
}
|
||
|
|
||
|
impl HttpClient {
|
||
|
pub fn new(proxy: Option<&Url>) -> Self {
|
||
|
Self {
|
||
|
proxy: proxy.cloned(),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub async fn request(&self, req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
|
||
|
if let Some(url) = &self.proxy {
|
||
|
// Panic safety: all URLs are valid URIs
|
||
|
let uri = url.to_string().parse().unwrap();
|
||
|
let proxy = Proxy::new(Intercept::All, uri);
|
||
|
let connector = HttpConnector::new();
|
||
|
let proxy_connector = ProxyConnector::from_proxy_unsecured(connector, proxy);
|
||
|
Client::builder().build(proxy_connector).request(req).await
|
||
|
} else {
|
||
|
Client::new().request(req).await
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub async fn request_body(&self, req: Request<Body>) -> Result<bytes::Bytes, hyper::Error> {
|
||
|
let response = self.request(req).await?;
|
||
|
hyper::body::to_bytes(response.into_body()).await
|
||
|
}
|
||
|
}
|