Line data Source code
1 : use pageserver_client::mgmt_api::{self, ResponseErrorMessageExt};
2 : use reqwest::{Method, Url};
3 : use serde::Serialize;
4 : use serde::de::DeserializeOwned;
5 :
6 : pub struct Client {
7 : base_url: Url,
8 : jwt_token: Option<String>,
9 : client: reqwest::Client,
10 : }
11 :
12 : impl Client {
13 0 : pub fn new(http_client: reqwest::Client, base_url: Url, jwt_token: Option<String>) -> Self {
14 0 : Self {
15 0 : base_url,
16 0 : jwt_token,
17 0 : client: http_client,
18 0 : }
19 0 : }
20 :
21 : /// Simple HTTP request wrapper for calling into storage controller
22 0 : pub async fn dispatch<RQ, RS>(
23 0 : &self,
24 0 : method: Method,
25 0 : path: String,
26 0 : body: Option<RQ>,
27 0 : ) -> mgmt_api::Result<RS>
28 0 : where
29 0 : RQ: Serialize + Sized,
30 0 : RS: DeserializeOwned + Sized,
31 0 : {
32 0 : let request_path = self
33 0 : .base_url
34 0 : .join(&path)
35 0 : .expect("Failed to build request path");
36 0 : let mut builder = self.client.request(method, request_path);
37 0 : if let Some(body) = body {
38 0 : builder = builder.json(&body)
39 0 : }
40 0 : if let Some(jwt_token) = &self.jwt_token {
41 0 : builder = builder.header(
42 0 : reqwest::header::AUTHORIZATION,
43 0 : format!("Bearer {jwt_token}"),
44 0 : );
45 0 : }
46 :
47 0 : let response = builder.send().await.map_err(mgmt_api::Error::ReceiveBody)?;
48 0 : let response = response.error_from_body().await?;
49 :
50 0 : response
51 0 : .json()
52 0 : .await
53 0 : .map_err(pageserver_client::mgmt_api::Error::ReceiveBody)
54 0 : }
55 : }
|