vsock_client: support authorization header
Pass in an optional auth tag, which will be passed as an Authorization header on every subsequent call. Signed-off-by: Stefan Reiter <s.reiter@proxmox.com>
This commit is contained in:
parent
971bc6f94b
commit
4876393562
@ -137,22 +137,28 @@ pub struct VsockClient {
|
|||||||
client: Client<VsockConnector>,
|
client: Client<VsockConnector>,
|
||||||
cid: i32,
|
cid: i32,
|
||||||
port: u16,
|
port: u16,
|
||||||
|
auth: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl VsockClient {
|
impl VsockClient {
|
||||||
pub fn new(cid: i32, port: u16) -> Self {
|
pub fn new(cid: i32, port: u16, auth: Option<String>) -> Self {
|
||||||
let conn = VsockConnector {};
|
let conn = VsockConnector {};
|
||||||
let client = Client::builder().build::<_, Body>(conn);
|
let client = Client::builder().build::<_, Body>(conn);
|
||||||
Self { client, cid, port }
|
Self {
|
||||||
|
client,
|
||||||
|
cid,
|
||||||
|
port,
|
||||||
|
auth,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get(&self, path: &str, data: Option<Value>) -> Result<Value, Error> {
|
pub async fn get(&self, path: &str, data: Option<Value>) -> Result<Value, Error> {
|
||||||
let req = Self::request_builder(self.cid, self.port, "GET", path, data)?;
|
let req = self.request_builder("GET", path, data)?;
|
||||||
self.api_request(req).await
|
self.api_request(req).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn post(&self, path: &str, data: Option<Value>) -> Result<Value, Error> {
|
pub async fn post(&self, path: &str, data: Option<Value>) -> Result<Value, Error> {
|
||||||
let req = Self::request_builder(self.cid, self.port, "POST", path, data)?;
|
let req = self.request_builder("POST", path, data)?;
|
||||||
self.api_request(req).await
|
self.api_request(req).await
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -162,7 +168,7 @@ impl VsockClient {
|
|||||||
data: Option<Value>,
|
data: Option<Value>,
|
||||||
output: &mut (dyn AsyncWrite + Send + Unpin),
|
output: &mut (dyn AsyncWrite + Send + Unpin),
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
let req = Self::request_builder(self.cid, self.port, "GET", path, data)?;
|
let req = self.request_builder("GET", path, data)?;
|
||||||
|
|
||||||
let client = self.client.clone();
|
let client = self.client.clone();
|
||||||
|
|
||||||
@ -210,47 +216,43 @@ impl VsockClient {
|
|||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn request_builder(
|
fn request_builder(
|
||||||
cid: i32,
|
&self,
|
||||||
port: u16,
|
|
||||||
method: &str,
|
method: &str,
|
||||||
path: &str,
|
path: &str,
|
||||||
data: Option<Value>,
|
data: Option<Value>,
|
||||||
) -> Result<Request<Body>, Error> {
|
) -> Result<Request<Body>, Error> {
|
||||||
let path = path.trim_matches('/');
|
let path = path.trim_matches('/');
|
||||||
let url: Uri = format!("vsock://{}:{}/{}", cid, port, path).parse()?;
|
let url: Uri = format!("vsock://{}:{}/{}", self.cid, self.port, path).parse()?;
|
||||||
|
|
||||||
|
let make_builder = |content_type: &str, url: &Uri| {
|
||||||
|
let mut builder = Request::builder()
|
||||||
|
.method(method)
|
||||||
|
.uri(url)
|
||||||
|
.header(hyper::header::CONTENT_TYPE, content_type);
|
||||||
|
if let Some(auth) = &self.auth {
|
||||||
|
builder = builder.header(hyper::header::AUTHORIZATION, auth);
|
||||||
|
}
|
||||||
|
builder
|
||||||
|
};
|
||||||
|
|
||||||
if let Some(data) = data {
|
if let Some(data) = data {
|
||||||
if method == "POST" {
|
if method == "POST" {
|
||||||
let request = Request::builder()
|
let builder = make_builder("application/json", &url);
|
||||||
.method(method)
|
let request = builder.body(Body::from(data.to_string()))?;
|
||||||
.uri(url)
|
|
||||||
.header(hyper::header::CONTENT_TYPE, "application/json")
|
|
||||||
.body(Body::from(data.to_string()))?;
|
|
||||||
return Ok(request);
|
return Ok(request);
|
||||||
} else {
|
} else {
|
||||||
let query = tools::json_object_to_query(data)?;
|
let query = tools::json_object_to_query(data)?;
|
||||||
let url: Uri = format!("vsock://{}:{}/{}?{}", cid, port, path, query).parse()?;
|
let url: Uri =
|
||||||
let request = Request::builder()
|
format!("vsock://{}:{}/{}?{}", self.cid, self.port, path, query).parse()?;
|
||||||
.method(method)
|
let builder = make_builder("application/x-www-form-urlencoded", &url);
|
||||||
.uri(url)
|
let request = builder.body(Body::empty())?;
|
||||||
.header(
|
|
||||||
hyper::header::CONTENT_TYPE,
|
|
||||||
"application/x-www-form-urlencoded",
|
|
||||||
)
|
|
||||||
.body(Body::empty())?;
|
|
||||||
return Ok(request);
|
return Ok(request);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let request = Request::builder()
|
let builder = make_builder("application/x-www-form-urlencoded", &url);
|
||||||
.method(method)
|
let request = builder.body(Body::empty())?;
|
||||||
.uri(url)
|
|
||||||
.header(
|
|
||||||
hyper::header::CONTENT_TYPE,
|
|
||||||
"application/x-www-form-urlencoded",
|
|
||||||
)
|
|
||||||
.body(Body::empty())?;
|
|
||||||
|
|
||||||
Ok(request)
|
Ok(request)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user