http: extend HttpClient trait
to allow get requests with extra headers (such as `Authorization`) and a generic `request` fn to increase flexibility even more. this is a breaking change. Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
This commit is contained in:
parent
ab5d5b39f6
commit
f429fcb592
@ -159,11 +159,23 @@ impl Default for SimpleHttp {
|
||||
|
||||
#[cfg(all(feature = "client-trait", feature = "proxmox-async"))]
|
||||
impl crate::HttpClient<Body> for SimpleHttp {
|
||||
fn get(&self, uri: &str) -> Result<Response<Body>, Error> {
|
||||
let req = Request::builder()
|
||||
fn get(
|
||||
&self,
|
||||
uri: &str,
|
||||
extra_headers: Option<&HashMap<String, String>>,
|
||||
) -> Result<Response<Body>, Error> {
|
||||
let mut req = Request::builder()
|
||||
.method("GET")
|
||||
.uri(uri)
|
||||
.body(Body::empty())?;
|
||||
|
||||
if let Some(extra_headers) = extra_headers {
|
||||
let headers = req.headers_mut();
|
||||
for (header, value) in extra_headers {
|
||||
headers.insert(HeaderName::from_str(header)?, HeaderValue::from_str(value)?);
|
||||
}
|
||||
}
|
||||
|
||||
proxmox_async::runtime::block_on(self.request(req))
|
||||
}
|
||||
|
||||
@ -175,15 +187,31 @@ impl crate::HttpClient<Body> for SimpleHttp {
|
||||
) -> Result<Response<Body>, Error> {
|
||||
proxmox_async::runtime::block_on(self.post(uri, body.map(|s| s.to_owned()), content_type))
|
||||
}
|
||||
|
||||
fn request(&self, request: Request<Body>) -> Result<Response<Body>, Error> {
|
||||
proxmox_async::runtime::block_on(async move { self.request(request).await })
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(feature = "client-trait", feature = "proxmox-async"))]
|
||||
impl crate::HttpClient<String> for SimpleHttp {
|
||||
fn get(&self, uri: &str) -> Result<Response<String>, Error> {
|
||||
let req = Request::builder()
|
||||
fn get(
|
||||
&self,
|
||||
uri: &str,
|
||||
extra_headers: Option<&HashMap<String, String>>,
|
||||
) -> Result<Response<String>, Error> {
|
||||
let mut req = Request::builder()
|
||||
.method("GET")
|
||||
.uri(uri)
|
||||
.body(Body::empty())?;
|
||||
|
||||
if let Some(extra_headers) = extra_headers {
|
||||
let headers = req.headers_mut();
|
||||
for (header, value) in extra_headers {
|
||||
headers.insert(HeaderName::from_str(header)?, HeaderValue::from_str(value)?);
|
||||
}
|
||||
}
|
||||
|
||||
proxmox_async::runtime::block_on(async move {
|
||||
Self::convert_body_to_string(self.request(req).await).await
|
||||
})
|
||||
@ -203,4 +231,13 @@ impl crate::HttpClient<String> for SimpleHttp {
|
||||
.await
|
||||
})
|
||||
}
|
||||
|
||||
fn request(&self, request: Request<String>) -> Result<Response<String>, Error> {
|
||||
proxmox_async::runtime::block_on(async move {
|
||||
let (parts, body) = request.into_parts();
|
||||
let body = Body::from(body);
|
||||
let request = Request::from_parts(parts, body);
|
||||
Self::convert_body_to_string(self.request(request).await).await
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,14 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use anyhow::Error;
|
||||
use http::Response;
|
||||
use http::{Request, Response};
|
||||
|
||||
pub trait HttpClient<T> {
|
||||
fn get(&self, uri: &str) -> Result<Response<T>, Error>;
|
||||
fn get(
|
||||
&self,
|
||||
uri: &str,
|
||||
extra_headers: Option<&HashMap<String, String>>,
|
||||
) -> Result<Response<T>, Error>;
|
||||
|
||||
fn post(
|
||||
&self,
|
||||
@ -10,4 +16,6 @@ pub trait HttpClient<T> {
|
||||
body: Option<&str>,
|
||||
content_type: Option<&str>,
|
||||
) -> Result<Response<T>, Error>;
|
||||
|
||||
fn request(&self, request: Request<T>) -> Result<Response<T>, Error>;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user