Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

refactor(desktop-gpui): let authed requests reuse a caller's client

Richie McIlroy65ee812e569f
diff --git a/apps/desktop-gpui/src/auth.rs b/apps/desktop-gpui/src/auth.rs
index 072cdab5c..11b920c00 100644
--- a/apps/desktop-gpui/src/auth.rs
+++ b/apps/desktop-gpui/src/auth.rs
@@ -225,10 +225,27 @@ impl UploadRequestContext {
}
}
+pub fn api_client() -> Result<reqwest::Client, AuthApiError> {
+ reqwest::Client::builder()
+ .connect_timeout(Duration::from_secs(10))
+ .timeout(Duration::from_secs(45))
+ .build()
+ .map_err(|error| AuthApiError::Other(error.to_string()))
+}
+
pub async fn authed_request(
method: reqwest::Method,
path: &str,
body: Option<Value>,
+) -> Result<reqwest::Response, AuthApiError> {
+ authed_request_with(&api_client()?, method, path, body).await
+}
+
+pub async fn authed_request_with(
+ client: &reqwest::Client,
+ method: reqwest::Method,
+ path: &str,
+ body: Option<Value>,
) -> Result<reqwest::Response, AuthApiError> {
let (server, token) = if let Ok(context) = UPLOAD_REQUEST.try_with(Clone::clone) {
let token = context.credentials()?;
@@ -241,11 +258,6 @@ pub async fn authed_request(
)
};
let url = format!("{server}{path}");
- let client = reqwest::Client::builder()
- .connect_timeout(Duration::from_secs(10))
- .timeout(Duration::from_secs(45))
- .build()
- .map_err(|error| AuthApiError::Other(error.to_string()))?;
let mut request = client.request(method, url);
request = apply_desktop_headers(request).bearer_auth(token);
if let Some(body) = body {