mirror of
https://github.com/librespot-org/librespot.git
synced 2025-10-03 01:39:28 +02:00
Introduce HTTP client
This commit is contained in:
parent
ce4f8dc288
commit
15628842af
5 changed files with 58 additions and 33 deletions
34
core/src/http_client.rs
Normal file
34
core/src/http_client.rs
Normal file
|
@ -0,0 +1,34 @@
|
|||
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
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue