Skip to content

Commit 788e74f

Browse files
committed
Set content type posted by tedge http post
Signed-off-by: Didier Wenzek <[email protected]>
1 parent 09517fa commit 788e74f

File tree

2 files changed

+80
-19
lines changed

2 files changed

+80
-19
lines changed

crates/core/tedge/src/cli/http/cli.rs

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ pub enum TEdgeHttpCli {
2626
#[command(flatten)]
2727
content: Content,
2828

29+
/// MIME type of the content
30+
#[clap(long, default_value = "application/json")]
31+
#[arg(value_parser = parse_mime_type)]
32+
content_type: String,
33+
34+
/// MIME type of the expected content
35+
#[clap(long, default_value = "application/json")]
36+
#[arg(value_parser = parse_mime_type)]
37+
accept_type: String,
38+
2939
/// Optional c8y cloud profile
3040
#[clap(long)]
3141
profile: Option<ProfileName>,
@@ -40,6 +50,11 @@ pub enum TEdgeHttpCli {
4050
#[command(flatten)]
4151
content: Content,
4252

53+
/// MIME type of the content
54+
#[clap(long, default_value = "application/json")]
55+
#[arg(value_parser = parse_mime_type)]
56+
content_type: String,
57+
4358
/// Optional c8y cloud profile
4459
#[clap(long)]
4560
profile: Option<ProfileName>,
@@ -50,6 +65,11 @@ pub enum TEdgeHttpCli {
5065
/// Source URI
5166
uri: String,
5267

68+
/// MIME type of the expected content
69+
#[clap(long, default_value = "application/json")]
70+
#[arg(value_parser = parse_mime_type)]
71+
accept_type: String,
72+
5373
/// Optional c8y cloud profile
5474
#[clap(long)]
5575
profile: Option<ProfileName>,
@@ -82,6 +102,10 @@ pub struct Content {
82102
file: Option<Utf8PathBuf>,
83103
}
84104

105+
fn parse_mime_type(input: &str) -> Result<String, Error> {
106+
Ok(input.parse::<mime_guess::mime::Mime>()?.to_string())
107+
}
108+
85109
impl TryFrom<Content> for blocking::Body {
86110
type Error = std::io::Error;
87111

@@ -121,13 +145,7 @@ impl BuildCommand for TEdgeHttpCli {
121145
let url = format!("{protocol}://{host}:{port}{uri}");
122146
let identity = config.http.client.auth.identity()?;
123147
let client = http_client(config.cloud_root_certs(), identity.as_ref())?;
124-
125-
let action = match self {
126-
TEdgeHttpCli::Post { content, .. } => HttpAction::Post(content),
127-
TEdgeHttpCli::Put { content, .. } => HttpAction::Put(content),
128-
TEdgeHttpCli::Get { .. } => HttpAction::Get,
129-
TEdgeHttpCli::Delete { .. } => HttpAction::Delete,
130-
};
148+
let action = self.into();
131149

132150
Ok(HttpCommand {
133151
client,
@@ -138,6 +156,33 @@ impl BuildCommand for TEdgeHttpCli {
138156
}
139157
}
140158

159+
impl From<TEdgeHttpCli> for HttpAction {
160+
fn from(value: TEdgeHttpCli) -> Self {
161+
match value {
162+
TEdgeHttpCli::Post {
163+
content,
164+
content_type,
165+
accept_type,
166+
..
167+
} => HttpAction::Post {
168+
content,
169+
content_type,
170+
accept_type,
171+
},
172+
TEdgeHttpCli::Put {
173+
content,
174+
content_type,
175+
..
176+
} => HttpAction::Put {
177+
content,
178+
content_type,
179+
},
180+
TEdgeHttpCli::Get { accept_type, .. } => HttpAction::Get { accept_type },
181+
TEdgeHttpCli::Delete { .. } => HttpAction::Delete,
182+
}
183+
}
184+
}
185+
141186
impl TEdgeHttpCli {
142187
fn uri(&self) -> &str {
143188
match self {

crates/core/tedge/src/cli/http/command.rs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,27 @@ pub struct HttpCommand {
1616
}
1717

1818
pub enum HttpAction {
19-
Post(Content),
20-
Put(Content),
21-
Get,
19+
Post {
20+
content: Content,
21+
content_type: String,
22+
accept_type: String,
23+
},
24+
Put {
25+
content: Content,
26+
content_type: String,
27+
},
28+
Get {
29+
accept_type: String,
30+
},
2231
Delete,
2332
}
2433

2534
impl Command for HttpCommand {
2635
fn description(&self) -> String {
2736
let verb = match self.action {
28-
HttpAction::Post(_) => "POST",
29-
HttpAction::Put(_) => "PUT",
30-
HttpAction::Get => "GET",
37+
HttpAction::Post { .. } => "POST",
38+
HttpAction::Put { .. } => "PUT",
39+
HttpAction::Get { .. } => "GET",
3140
HttpAction::Delete => "DELETE",
3241
};
3342
format!("{verb} {}", self.url)
@@ -45,16 +54,23 @@ impl HttpCommand {
4554
let client = &self.client;
4655
let url = &self.url;
4756
let request = match &self.action {
48-
HttpAction::Post(content) => client
57+
HttpAction::Post {
58+
content,
59+
content_type,
60+
accept_type,
61+
} => client
4962
.post(url)
50-
.header("Accept", "application/json")
51-
.header("Content-Type", "application/json")
63+
.header("Accept", accept_type)
64+
.header("Content-Type", content_type)
5265
.body(blocking::Body::try_from(content.clone())?),
53-
HttpAction::Put(content) => client
66+
HttpAction::Put {
67+
content,
68+
content_type,
69+
} => client
5470
.put(url)
55-
.header("Content-Type", "application/json")
71+
.header("Content-Type", content_type)
5672
.body(blocking::Body::try_from(content.clone())?),
57-
HttpAction::Get => client.get(url).header("Accept", "application/json"),
73+
HttpAction::Get { accept_type } => client.get(url).header("Accept", accept_type),
5874
HttpAction::Delete => client.delete(url),
5975
};
6076

0 commit comments

Comments
 (0)