Skip to content

Commit

Permalink
fixup! Posting a file with tedge http post --file
Browse files Browse the repository at this point in the history
  • Loading branch information
didier-wenzek committed Feb 4, 2025
1 parent 951cc55 commit ac42116
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 36 deletions.
33 changes: 9 additions & 24 deletions crates/core/tedge/src/cli/http/cli.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::cli::http::command::HttpAction;
use crate::cli::http::command::HttpCommand;
use crate::command::BuildCommand;
use crate::command::BuildContext;
Expand Down Expand Up @@ -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())
}
Expand All @@ -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, .. }
Expand Down
62 changes: 50 additions & 12 deletions crates/core/tedge/src/cli/http/command.rs
Original file line number Diff line number Diff line change
@@ -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<Error>> {
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<blocking::RequestBuilder, Error> {
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(())
}
}

0 comments on commit ac42116

Please sign in to comment.