From cd0208fe0a92d0ee695f72c7e8c0010afb5cebc8 Mon Sep 17 00:00:00 2001 From: "James O. D. Hunt" Date: Wed, 28 Sep 2022 14:10:38 +0100 Subject: [PATCH] api-client: Allow response to be captured Previously, the API response was always written to stdout, but that may not be appropriate for all clients. The client can now control this behaviour as the client API returns the response in the `Result`. Fixes: #4703. Signed-off-by: James O. D. Hunt --- api_client/src/lib.rs | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/api_client/src/lib.rs b/api_client/src/lib.rs index ff85aad33..b2bd4d9d6 100644 --- a/api_client/src/lib.rs +++ b/api_client/src/lib.rs @@ -143,13 +143,13 @@ fn parse_http_response(socket: &mut dyn Read) -> Result, Error> { /// Make an API request using the fully qualified command name. /// For example, full_command could be "vm.create" or "vmm.ping". -pub fn simple_api_full_command_with_fds( +pub fn simple_api_full_command_with_fds_and_response( socket: &mut T, method: &str, full_command: &str, request_body: Option<&str>, request_fds: Vec, -) -> Result<(), Error> { +) -> Result, Error> { socket .send_with_fds( &[format!( @@ -177,9 +177,28 @@ pub fn simple_api_full_command_with_fds( socket.flush().map_err(Error::Socket)?; - if let Some(body) = parse_http_response(socket)? { - println!("{}", body); + parse_http_response(socket) +} + +pub fn simple_api_full_command_with_fds( + socket: &mut T, + method: &str, + full_command: &str, + request_body: Option<&str>, + request_fds: Vec, +) -> Result<(), Error> { + let response = simple_api_full_command_with_fds_and_response( + socket, + method, + full_command, + request_body, + request_fds, + )?; + + if response.is_some() { + println!("{}", response.unwrap()); } + Ok(()) } @@ -192,6 +211,21 @@ pub fn simple_api_full_command( simple_api_full_command_with_fds(socket, method, full_command, request_body, Vec::new()) } +pub fn simple_api_full_command_and_response( + socket: &mut T, + method: &str, + full_command: &str, + request_body: Option<&str>, +) -> Result, Error> { + simple_api_full_command_with_fds_and_response( + socket, + method, + full_command, + request_body, + Vec::new(), + ) +} + pub fn simple_api_command_with_fds( socket: &mut T, method: &str,