mirror of
https://github.com/zed-industries/zed.git
synced 2025-01-27 12:54:42 +00:00
8ba2f77148
Co-authored-by: Mikayla <mikayla@zed.dev>
51 lines
1.3 KiB
Rust
51 lines
1.3 KiB
Rust
pub use anyhow::{anyhow, Result};
|
|
use futures::future::BoxFuture;
|
|
use isahc::{
|
|
config::{Configurable, RedirectPolicy},
|
|
AsyncBody,
|
|
};
|
|
pub use isahc::{
|
|
http::{Method, Uri},
|
|
Error,
|
|
};
|
|
use smol::future::FutureExt;
|
|
use std::sync::Arc;
|
|
pub use url::Url;
|
|
|
|
pub type Request = isahc::Request<AsyncBody>;
|
|
pub type Response = isahc::Response<AsyncBody>;
|
|
|
|
pub trait HttpClient: Send + Sync {
|
|
fn send(&self, req: Request) -> BoxFuture<Result<Response, Error>>;
|
|
|
|
fn get<'a>(
|
|
&'a self,
|
|
uri: &str,
|
|
body: AsyncBody,
|
|
follow_redirects: bool,
|
|
) -> BoxFuture<'a, Result<Response, Error>> {
|
|
let request = isahc::Request::builder()
|
|
.redirect_policy(if follow_redirects {
|
|
RedirectPolicy::Follow
|
|
} else {
|
|
RedirectPolicy::None
|
|
})
|
|
.method(Method::GET)
|
|
.uri(uri)
|
|
.body(body);
|
|
match request {
|
|
Ok(request) => self.send(request),
|
|
Err(error) => async move { Err(error.into()) }.boxed(),
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn client() -> Arc<dyn HttpClient> {
|
|
Arc::new(isahc::HttpClient::builder().build().unwrap())
|
|
}
|
|
|
|
impl HttpClient for isahc::HttpClient {
|
|
fn send(&self, req: Request) -> BoxFuture<Result<Response, Error>> {
|
|
Box::pin(async move { self.send_async(req).await })
|
|
}
|
|
}
|