Utopian CI setup phase 2: Better HTTP abstractions (#61)

This commit is contained in:
Miles Wirht
2026-06-14 21:58:25 -04:00
committed by GitHub
parent 64318f59f3
commit bf71c690a3
17 changed files with 196 additions and 1164 deletions
+1 -24
View File
@@ -29,7 +29,7 @@ pub use entity_map::*;
use gpui_util::{ResultExt, debug_panic};
#[cfg(any(test, feature = "test-support"))]
pub use headless_app_context::*;
use http_client::{HttpClient, Url};
use crate::http_client::{HttpClient, NullHttpClient};
use smallvec::SmallVec;
#[cfg(any(test, feature = "test-support"))]
pub use test_app::*;
@@ -2710,30 +2710,7 @@ pub struct KeystrokeEvent {
pub context_stack: Vec<KeyContext>,
}
struct NullHttpClient;
impl HttpClient for NullHttpClient {
fn send(
&self,
_req: http_client::Request<http_client::AsyncBody>,
) -> futures::future::BoxFuture<
'static,
anyhow::Result<http_client::Response<http_client::AsyncBody>>,
> {
async move {
anyhow::bail!("No HttpClient available");
}
.boxed()
}
fn user_agent(&self) -> Option<&http_client::http::HeaderValue> {
None
}
fn proxy(&self) -> Option<&Url> {
None
}
}
/// A mutable reference to an entity owned by GPUI
pub struct GpuiBorrow<'a, T> {
+1 -1
View File
@@ -87,7 +87,7 @@ impl HeadlessAppContext {
);
let text_system = Arc::new(TextSystem::new(platform_text_system));
let http_client = http_client::FakeHttpClient::with_404_response();
let http_client = crate::http_client::FakeHttpClient::with_404_response();
let app = App::new_app(platform, asset_source, http_client);
app.borrow_mut().mode = GpuiMode::test();
+1 -1
View File
@@ -88,7 +88,7 @@ impl TestApp {
),
None => TestPlatform::new(background_executor.clone(), foreground_executor.clone()),
};
let http_client = http_client::FakeHttpClient::with_404_response();
let http_client = crate::http_client::FakeHttpClient::with_404_response();
let text_system = Arc::new(TextSystem::new(
platform_text_system.unwrap_or_else(|| platform.text_system.clone()),
));
+1 -1
View File
@@ -129,7 +129,7 @@ impl TestAppContext {
let foreground_executor = ForegroundExecutor::new(arc_dispatcher);
let platform = TestPlatform::new(background_executor.clone(), foreground_executor.clone());
let asset_source = Arc::new(());
let http_client = http_client::FakeHttpClient::with_404_response();
let http_client = crate::http_client::FakeHttpClient::with_404_response();
let text_system = Arc::new(TextSystem::new(platform.text_system()));
let app = App::new_app(platform.clone(), asset_source, http_client);
+1 -1
View File
@@ -71,7 +71,7 @@ impl VisualTestAppContext {
let text_system = Arc::new(TextSystem::new(platform.text_system()));
let http_client = http_client::FakeHttpClient::with_404_response();
let http_client = crate::http_client::FakeHttpClient::with_404_response();
let mut app = App::new_app(platform.clone(), asset_source, http_client);
app.borrow_mut().mode = GpuiMode::test();
+19 -21
View File
@@ -618,27 +618,25 @@ impl Asset for ImageAssetLoader {
async move {
let bytes = match source.clone() {
Resource::Path(uri) => fs::read(uri.as_ref())?,
Resource::Uri(uri) => {
use anyhow::Context as _;
use futures::AsyncReadExt as _;
Resource::Uri(uri) => {
use anyhow::Context as _;
let mut response = client
.get(uri.as_ref(), ().into(), true)
.await
.with_context(|| format!("loading image asset from {uri:?}"))?;
let mut body = Vec::new();
response.body_mut().read_to_end(&mut body).await?;
if !response.status().is_success() {
let mut body = String::from_utf8_lossy(&body).into_owned();
let first_line = body.lines().next().unwrap_or("").trim_end();
body.truncate(first_line.len());
return Err(ImageCacheError::BadStatus {
uri,
status: response.status(),
body,
});
}
body
let response = client
.get(uri.as_ref(), true)
.await
.with_context(|| format!("loading image asset from {uri:?}"))?;
if !response.status.is_success() {
let mut error_body =
String::from_utf8_lossy(&response.body).into_owned();
let first_line = error_body.lines().next().unwrap_or("").trim_end();
error_body.truncate(first_line.len());
return Err(ImageCacheError::BadStatus {
uri,
status: response.status,
body: error_body,
});
}
response.body
}
Resource::Embedded(path) => {
let data = asset_source.load(&path).ok().flatten();
@@ -763,7 +761,7 @@ pub enum ImageCacheError {
/// The URI of the image.
uri: SharedUri,
/// The HTTP status code.
status: http_client::StatusCode,
status: http::StatusCode,
/// The HTTP response body.
body: String,
},
+1 -1
View File
@@ -464,7 +464,7 @@ mod test {
let platform = TestPlatform::new(background_executor.clone(), foreground_executor);
let asset_source = Arc::new(());
let http_client = http_client::FakeHttpClient::with_404_response();
let http_client = crate::http_client::FakeHttpClient::with_404_response();
let app = App::new_app(platform, asset_source, http_client);
(dispatcher, background_executor, app)
+2 -1
View File
@@ -100,7 +100,8 @@ pub use gpui_macros::{
};
pub use gpui_shared_string::*;
pub use gpui_util::arc_cow::ArcCow;
pub use http_client;
/// HTTP client abstraction for making requests.
pub mod http_client;
pub use input::*;
pub use inspector::*;
pub use interactive::*;
+105
View File
@@ -0,0 +1,105 @@
use futures::future::BoxFuture;
use http::StatusCode;
/// A simple HTTP response.
pub struct HttpResponse {
/// The HTTP status code.
pub status: StatusCode,
/// The response body bytes.
pub body: Vec<u8>,
}
/// A trait for making HTTP requests.
pub trait HttpClient: 'static + Send + Sync {
/// Perform a GET request and return the full response.
fn get(
&self,
url: &str,
follow_redirects: bool,
) -> BoxFuture<'static, anyhow::Result<HttpResponse>>;
}
/// An HTTP client that always returns an error.
pub struct NullHttpClient;
impl HttpClient for NullHttpClient {
fn get(
&self,
_url: &str,
_follow_redirects: bool,
) -> BoxFuture<'static, anyhow::Result<HttpResponse>> {
Box::pin(async { anyhow::bail!("No HttpClient available") })
}
}
/// An HTTP client that blocks all requests.
pub struct BlockedHttpClient;
impl BlockedHttpClient {
/// Create a new `BlockedHttpClient`.
pub fn new() -> Self {
Self
}
}
impl Default for BlockedHttpClient {
fn default() -> Self {
Self
}
}
impl HttpClient for BlockedHttpClient {
fn get(
&self,
_url: &str,
_follow_redirects: bool,
) -> BoxFuture<'static, anyhow::Result<HttpResponse>> {
Box::pin(async {
Err(std::io::Error::new(
std::io::ErrorKind::PermissionDenied,
"BlockedHttpClient disallowed request",
)
.into())
})
}
}
/// A fake HTTP client for testing.
#[cfg(any(test, feature = "test-support"))]
pub struct FakeHttpClient {
status: StatusCode,
}
#[cfg(any(test, feature = "test-support"))]
impl FakeHttpClient {
/// Create a fake client that returns 404 responses.
pub fn with_404_response() -> std::sync::Arc<dyn HttpClient> {
std::sync::Arc::new(Self {
status: StatusCode::NOT_FOUND,
})
}
/// Create a fake client that returns 200 responses.
pub fn with_200_response() -> std::sync::Arc<dyn HttpClient> {
std::sync::Arc::new(Self {
status: StatusCode::OK,
})
}
}
#[cfg(any(test, feature = "test-support"))]
impl HttpClient for FakeHttpClient {
fn get(
&self,
_url: &str,
_follow_redirects: bool,
) -> BoxFuture<'static, anyhow::Result<HttpResponse>> {
let status = self.status;
Box::pin(async move {
Ok(HttpResponse {
status,
body: Vec::new(),
})
})
}
}