1pub use anyhow::{anyhow, Result};
2use futures::future::BoxFuture;
3use isahc::{
4 config::{Configurable, RedirectPolicy},
5 AsyncBody,
6};
7pub use isahc::{
8 http::{Method, Uri},
9 Error,
10};
11use std::sync::Arc;
12pub use url::Url;
13
14pub type Request = isahc::Request<AsyncBody>;
15pub type Response = isahc::Response<AsyncBody>;
16
17pub trait HttpClient: Send + Sync {
18 fn send<'a>(&'a self, req: Request) -> BoxFuture<'a, Result<Response, Error>>;
19
20 fn get<'a>(
21 &'a self,
22 uri: &str,
23 body: AsyncBody,
24 follow_redirects: bool,
25 ) -> BoxFuture<'a, Result<Response, Error>> {
26 self.send(
27 isahc::Request::builder()
28 .redirect_policy(if follow_redirects {
29 RedirectPolicy::Follow
30 } else {
31 RedirectPolicy::None
32 })
33 .method(Method::GET)
34 .uri(uri)
35 .body(body)
36 .unwrap(),
37 )
38 }
39}
40
41pub fn client() -> Arc<dyn HttpClient> {
42 Arc::new(isahc::HttpClient::builder().build().unwrap())
43}
44
45impl HttpClient for isahc::HttpClient {
46 fn send<'a>(&'a self, req: Request) -> BoxFuture<'a, Result<Response, Error>> {
47 Box::pin(async move { self.send_async(req).await })
48 }
49}