diff --git a/crates/core/tedge/src/cli/http/cli.rs b/crates/core/tedge/src/cli/http/cli.rs index 6ae0c22c52..680edc4d2b 100644 --- a/crates/core/tedge/src/cli/http/cli.rs +++ b/crates/core/tedge/src/cli/http/cli.rs @@ -1,3 +1,4 @@ +use crate::cli::http::command::HttpAction; use crate::cli::http::command::HttpCommand; use crate::command::BuildCommand; use crate::command::BuildContext; @@ -117,27 +118,20 @@ impl BuildCommand for TEdgeHttpCli { }; let url = format!("{protocol}://{host}:{port}{uri}"); - let verb_url = format!("{} {url}", self.verb()); let identity = config.http.client.auth.identity()?; let client = http_client(config.cloud_root_certs(), identity.as_ref())?; - let request = match self { - TEdgeHttpCli::Post { content, .. } => client - .post(url) - .header("Accept", "application/json") - .header("Content-Type", "application/json") - .body(blocking::Body::try_from(content)?), - TEdgeHttpCli::Put { content, .. } => client - .put(url) - .header("Content-Type", "application/json") - .body(blocking::Body::try_from(content)?), - TEdgeHttpCli::Get { .. } => client.get(url).header("Accept", "application/json"), - TEdgeHttpCli::Delete { .. } => client.delete(url), + let action = match self { + TEdgeHttpCli::Post { content, .. } => HttpAction::Post(content), + TEdgeHttpCli::Put { content, .. } => HttpAction::Put(content), + TEdgeHttpCli::Get { .. } => HttpAction::Get, + TEdgeHttpCli::Delete { .. } => HttpAction::Delete, }; Ok(HttpCommand { - url: verb_url, - request, + client, + url, + action, } .into_boxed()) } @@ -153,15 +147,6 @@ impl TEdgeHttpCli { } } - fn verb(&self) -> &str { - match self { - TEdgeHttpCli::Post { .. } => "POST", - TEdgeHttpCli::Put { .. } => "PUT", - TEdgeHttpCli::Get { .. } => "GET", - TEdgeHttpCli::Delete { .. } => "DELETE", - } - } - fn c8y_profile(&self) -> Option<&ProfileName> { match self { TEdgeHttpCli::Post { profile, .. } diff --git a/crates/core/tedge/src/cli/http/command.rs b/crates/core/tedge/src/cli/http/command.rs index d09b522f49..a1dcbc13f2 100644 --- a/crates/core/tedge/src/cli/http/command.rs +++ b/crates/core/tedge/src/cli/http/command.rs @@ -1,35 +1,73 @@ +use crate::cli::http::cli::Content; use crate::command::Command; use crate::log::MaybeFancy; use anyhow::Error; use reqwest::blocking; pub struct HttpCommand { + /// HTTP client + pub client: blocking::Client, + /// Target url pub url: String, - /// HTTP request - pub request: blocking::RequestBuilder, + /// Action + pub action: HttpAction, +} + +pub enum HttpAction { + Post(Content), + Put(Content), + Get, + Delete, } impl Command for HttpCommand { fn description(&self) -> String { - self.url.clone() + let verb = match self.action { + HttpAction::Post(_) => "POST", + HttpAction::Put(_) => "PUT", + HttpAction::Get => "GET", + HttpAction::Delete => "DELETE", + }; + format!("{verb} {}", self.url) } fn execute(&self) -> Result<(), MaybeFancy> { - Ok(self.send()?) + let request = self.request()?; + HttpCommand::send(request)?; + Ok(()) } } impl HttpCommand { - fn send(&self) -> Result<(), Error> { - if let Some(request) = self.request.try_clone() { - let http_result = request.send()?; - let http_response = http_result.error_for_status()?; - let bytes = http_response.bytes()?.to_vec(); - let content = String::from_utf8(bytes)?; - println!("{content}"); - } + fn request(&self) -> Result { + let client = &self.client; + let url = &self.url; + let request = match &self.action { + HttpAction::Post(content) => client + .post(url) + .header("Accept", "application/json") + .header("Content-Type", "application/json") + .body(blocking::Body::try_from(content.clone())?), + HttpAction::Put(content) => client + .put(url) + .header("Content-Type", "application/json") + .body(blocking::Body::try_from(content.clone())?), + HttpAction::Get => client.get(url).header("Accept", "application/json"), + HttpAction::Delete => client.delete(url), + }; + + Ok(request) + } + + fn send(request: blocking::RequestBuilder) -> Result<(), Error> { + let http_result = request.send()?; + let http_response = http_result.error_for_status()?; + let bytes = http_response.bytes()?.to_vec(); + let content = String::from_utf8(bytes)?; + + println!("{content}"); Ok(()) } }