object_storage/oss/util/
request.rs1use anyhow::{anyhow, Result};
2use reqwest::{header::HeaderMap, Method};
3
4pub async fn request(
5 method: Method,
6 url: &str,
7 headers: HeaderMap,
8 body: Option<Vec<u8>>,
9) -> Result<String> {
10 let client = reqwest::Client::new();
11 let response = match body {
12 Some(body) => {
13 client
14 .request(method, url)
15 .headers(headers)
16 .body(body)
17 .send()
18 .await?
19 }
20 None => client.request(method, url).headers(headers).send().await?,
21 };
22
23 let status = response.status();
24 let text = response.text().await?;
25 if !status.is_success() {
26 return Err(anyhow!(
27 "Request failed with status code {}: {}",
28 status,
29 text
30 ));
31 }
32
33 Ok(text)
34}