Make following redirects explicit in HttpClient::get

This commit is contained in:
Antonio Scandurra 2022-04-27 13:14:45 +02:00
parent 78afbb3599
commit 0e1e5b7d55
7 changed files with 21 additions and 15 deletions

View file

@ -1,15 +1,14 @@
pub use anyhow::{anyhow, Result};
use futures::future::BoxFuture;
use isahc::{
config::{Configurable, RedirectPolicy},
AsyncBody,
};
use std::sync::Arc;
pub use anyhow::{anyhow, Result};
pub use isahc::{
http::{Method, Uri},
Error,
};
use std::sync::Arc;
pub use url::Url;
pub type Request = isahc::Request<AsyncBody>;
@ -18,9 +17,19 @@ pub type Response = isahc::Response<AsyncBody>;
pub trait HttpClient: Send + Sync {
fn send<'a>(&'a self, req: Request) -> BoxFuture<'a, Result<Response, Error>>;
fn get<'a>(&'a self, uri: &str, body: AsyncBody) -> BoxFuture<'a, Result<Response, Error>> {
fn get<'a>(
&'a self,
uri: &str,
body: AsyncBody,
follow_redirects: bool,
) -> BoxFuture<'a, Result<Response, Error>> {
self.send(
isahc::Request::builder()
.redirect_policy(if follow_redirects {
RedirectPolicy::Follow
} else {
RedirectPolicy::None
})
.method(Method::GET)
.uri(uri)
.body(body)
@ -30,12 +39,7 @@ pub trait HttpClient: Send + Sync {
}
pub fn client() -> Arc<dyn HttpClient> {
Arc::new(
isahc::HttpClient::builder()
.redirect_policy(RedirectPolicy::Follow)
.build()
.unwrap(),
)
Arc::new(isahc::HttpClient::builder().build().unwrap())
}
impl HttpClient for isahc::HttpClient {