use pageserver_client::mgmt_api::{self, ResponseErrorMessageExt}; use reqwest::{Method, Url}; use serde::{de::DeserializeOwned, Serialize}; pub struct Client { base_url: Url, jwt_token: Option, client: reqwest::Client, } impl Client { pub fn new(base_url: Url, jwt_token: Option) -> Self { Self { base_url, jwt_token, client: reqwest::ClientBuilder::new() .build() .expect("Failed to construct http client"), } } /// Simple HTTP request wrapper for calling into storage controller pub async fn dispatch( &self, method: Method, path: String, body: Option, ) -> mgmt_api::Result where RQ: Serialize + Sized, RS: DeserializeOwned + Sized, { let request_path = self .base_url .join(&path) .expect("Failed to build request path"); let mut builder = self.client.request(method, request_path); if let Some(body) = body { builder = builder.json(&body) } if let Some(jwt_token) = &self.jwt_token { builder = builder.header( reqwest::header::AUTHORIZATION, format!("Bearer {jwt_token}"), ); } let response = builder.send().await.map_err(mgmt_api::Error::ReceiveBody)?; let response = response.error_from_body().await?; response .json() .await .map_err(pageserver_client::mgmt_api::Error::ReceiveBody) } }