Compare commits
No commits in common. "f4ab2a327a3242a4e2ebb0ba2785682b5bc7a4f1" and "2f8920eefeb708ba5c7d4fa1608495ee8ff2f294" have entirely different histories.
f4ab2a327a
...
2f8920eefe
4 changed files with 12 additions and 24 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -813,7 +813,6 @@ checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964"
|
|||
name = "postwoman"
|
||||
version = "0.2.0"
|
||||
dependencies = [
|
||||
"base64",
|
||||
"chrono",
|
||||
"clap",
|
||||
"http",
|
||||
|
|
|
@ -6,7 +6,6 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
base64 = "0.22.1"
|
||||
chrono = "0.4"
|
||||
clap = { version = "4.5", features = ["derive"] }
|
||||
http = "1.1.0"
|
||||
|
|
18
src/main.rs
18
src/main.rs
|
@ -52,11 +52,11 @@ pub enum PostWomanActions {
|
|||
|
||||
const TIMESTAMP_FMT: &str = "%H:%M:%S%.6f";
|
||||
|
||||
fn print_results(res: String, name: String, before: chrono::DateTime<chrono::Local>, num: u32) {
|
||||
let after = chrono::Local::now();
|
||||
fn print_results(res: String, name: String, before: chrono::DateTime<chrono::Utc>) {
|
||||
let after = chrono::Utc::now();
|
||||
let elapsed = (after - before).num_milliseconds();
|
||||
let timestamp = after.format(TIMESTAMP_FMT);
|
||||
eprintln!(" + [{timestamp}] {name} #{num} done in {elapsed}ms", );
|
||||
eprintln!(" + [{timestamp}] {name} done in {elapsed}ms", );
|
||||
print!("{}", res);
|
||||
}
|
||||
|
||||
|
@ -79,26 +79,26 @@ async fn main() -> Result<(), PostWomanError> {
|
|||
let _endpoint = endpoint.clone();
|
||||
let _name = name.clone();
|
||||
let task = async move {
|
||||
let before = chrono::Local::now();
|
||||
let before = chrono::Utc::now();
|
||||
eprintln!(" : [{}] sending {_name} #{}...", before.format(TIMESTAMP_FMT), i+1);
|
||||
let res = _endpoint
|
||||
.fill()
|
||||
.execute(&_client)
|
||||
.await;
|
||||
(res, _name, before, i)
|
||||
(res, _name, before)
|
||||
};
|
||||
if parallel {
|
||||
joinset.spawn(task);
|
||||
} else {
|
||||
let (res, name, before, num) = task.await;
|
||||
print_results(res?, name, before, num);
|
||||
let (res, name, before) = task.await;
|
||||
print_results(res?, name, before);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
while let Some(j) = joinset.join_next().await {
|
||||
match j {
|
||||
Ok((res, name, before, num)) => print_results(res?, name, before, num),
|
||||
Ok((res, name, before)) => print_results(res?, name, before),
|
||||
Err(e) => eprintln!("! error joining task: {e}"),
|
||||
}
|
||||
}
|
||||
|
@ -109,5 +109,7 @@ async fn main() -> Result<(), PostWomanError> {
|
|||
// },
|
||||
}
|
||||
|
||||
eprintln!();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
16
src/model.rs
16
src/model.rs
|
@ -1,6 +1,5 @@
|
|||
use std::str::FromStr;
|
||||
|
||||
use base64::{prelude::BASE64_STANDARD, Engine};
|
||||
use reqwest::header::{HeaderMap, HeaderName, HeaderValue};
|
||||
|
||||
use crate::APP_USER_AGENT;
|
||||
|
@ -152,8 +151,8 @@ impl Endpoint {
|
|||
Ok(match self.extract.unwrap_or_default() {
|
||||
StringOr::Str(_query) => todo!(),
|
||||
StringOr::T(Extractor::Discard) => "".to_string(),
|
||||
StringOr::T(Extractor::Debug) => format!("{res:#?}\nBody: ") + &format_body(res).await? + "\n", // ughhh
|
||||
StringOr::T(Extractor::Body) => format_body(res).await?,
|
||||
StringOr::T(Extractor::Debug) => format!("{res:#?}\n"),
|
||||
StringOr::T(Extractor::Body) => res.text().await? + "\n",
|
||||
StringOr::T(Extractor::Header { key }) => res
|
||||
.headers()
|
||||
.get(&key)
|
||||
|
@ -165,17 +164,6 @@ impl Endpoint {
|
|||
}
|
||||
}
|
||||
|
||||
async fn format_body(res: reqwest::Response) -> Result<String, PostWomanError> {
|
||||
match res.headers().get("Content-Type") {
|
||||
None => Ok(res.text().await? + "\n"),
|
||||
Some(v) => match v.to_str()? {
|
||||
"application/json" => Ok(serde_json::to_string_pretty(&res.json::<serde_json::Value>().await?)? + "\n"),
|
||||
"text/plain" => Ok(res.text().await? + "\n"),
|
||||
_ => Ok(format!("base64({})\n", BASE64_STANDARD.encode(res.bytes().await?))),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum StringOr<T> {
|
||||
|
|
Loading…
Reference in a new issue