-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixup! Posting a file with tedge http post --file
- Loading branch information
1 parent
951cc55
commit ac42116
Showing
2 changed files
with
59 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} | ||
} |