Skip to content

Commit 02d5307

Browse files
committed
feat(http-client): support custom request headers and a prpc bearer token
1 parent f0d0e06 commit 02d5307

2 files changed

Lines changed: 43 additions & 9 deletions

File tree

dstack/http-client/src/lib.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ pub async fn http_request(
3434
base: &str,
3535
path: &str,
3636
body: &[u8],
37+
) -> Result<(u16, Vec<u8>)> {
38+
http_request_with_headers(method, base, path, body, &[]).await
39+
}
40+
41+
/// Same as [`http_request`], with extra request headers (e.g. `Authorization`).
42+
pub async fn http_request_with_headers(
43+
method: &str,
44+
base: &str,
45+
path: &str,
46+
body: &[u8],
47+
headers: &[(&str, &str)],
3748
) -> Result<(u16, Vec<u8>)> {
3849
debug!("Sending HTTP request to {base}, path={path}");
3950
let mut response = if let Some(uds) = base.strip_prefix("unix:") {
@@ -44,24 +55,29 @@ pub async fn http_request(
4455
};
4556
let client: Client<UnixConnector, Full<Bytes>> = Client::unix();
4657
let unix_uri: hyper::Uri = Uri::new(uds, &path).into();
47-
let req = Request::builder()
48-
.method(method)
49-
.uri(unix_uri)
50-
.body(Full::new(Bytes::copy_from_slice(body)))?;
58+
let mut builder = Request::builder().method(method).uri(unix_uri);
59+
for (name, value) in headers {
60+
builder = builder.header(*name, *value);
61+
}
62+
let req = builder.body(Full::new(Bytes::copy_from_slice(body)))?;
5163
client.request(req).await?
5264
} else if base.starts_with("vsock:") {
5365
let client = Client::vsock();
5466
let uri = mk_url(base, path).parse::<hyper::Uri>()?;
55-
let req = Request::builder()
56-
.method(method)
57-
.uri(uri)
58-
.body(Full::new(Bytes::copy_from_slice(body)))?;
67+
let mut builder = Request::builder().method(method).uri(uri);
68+
for (name, value) in headers {
69+
builder = builder.header(*name, *value);
70+
}
71+
let req = builder.body(Full::new(Bytes::copy_from_slice(body)))?;
5972
client.request(req).await?
6073
} else {
6174
let uri = mk_url(base, path);
6275
let client = reqwest::Client::builder().build()?;
6376
let method = reqwest::Method::from_bytes(method.as_bytes())?;
6477
let mut request = client.request(method, uri);
78+
for (name, value) in headers {
79+
request = request.header(*name, *value);
80+
}
6581
if !body.is_empty() {
6682
request = request.body(body.to_vec());
6783
}

dstack/http-client/src/prpc.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ use serde::{de::DeserializeOwned, Serialize};
1212
pub struct PrpcClient {
1313
base_url: String,
1414
path_append: String,
15+
auth_token: Option<String>,
1516
}
1617

1718
impl PrpcClient {
1819
pub fn new(base_url: String) -> Self {
1920
Self {
2021
base_url,
2122
path_append: String::new(),
23+
auth_token: None,
2224
}
2325
}
2426

@@ -29,8 +31,16 @@ impl PrpcClient {
2931
Self {
3032
base_url: format!("unix:{socket_path}"),
3133
path_append: path,
34+
auth_token: None,
3235
}
3336
}
37+
38+
/// Send `Authorization: Bearer <token>` with every request.
39+
pub fn with_bearer_token(mut self, token: impl Into<String>) -> Self {
40+
let token = token.into();
41+
self.auth_token = (!token.is_empty()).then_some(token);
42+
self
43+
}
3444
}
3545

3646
impl RequestClient for PrpcClient {
@@ -41,7 +51,15 @@ impl RequestClient for PrpcClient {
4151
{
4252
let body = serde_json::to_vec(&body).context("Failed to serialize body")?;
4353
let path = format!("{}{path}?json", self.path_append);
44-
let (status, body) = super::http_request("POST", &self.base_url, &path, &body).await?;
54+
let auth_header;
55+
let mut headers: Vec<(&str, &str)> = Vec::new();
56+
if let Some(token) = &self.auth_token {
57+
auth_header = format!("Bearer {token}");
58+
headers.push(("Authorization", auth_header.as_str()));
59+
}
60+
let (status, body) =
61+
super::http_request_with_headers("POST", &self.base_url, &path, &body, &headers)
62+
.await?;
4563
if status != 200 {
4664
anyhow::bail!("Invalid status code: {status}, path={path}");
4765
}

0 commit comments

Comments
 (0)