-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
110 additions
and
5 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,6 +1,6 @@ | ||
[package] | ||
name = "ruc" | ||
version = "7.0.1" | ||
version = "7.1.0" | ||
authors = ["[email protected]"] | ||
edition = "2021" | ||
description = "Rust Util Collections" | ||
|
@@ -24,10 +24,12 @@ hex = {version = "0.4.3", optional = true } | |
|
||
flate2 = {version = "1.0.34", optional = true } | ||
|
||
nix = { version = "0.29", features = ["socket"], optional = true } | ||
time = { version = "0.3", features = ["formatting"] } | ||
nix = { version = "0.29", features = ["socket"], optional = true } | ||
ssh2 = { version = "0.9.4", optional = true } | ||
|
||
reqwest = { version = "0.12.8", default-features = false, optional = true } | ||
|
||
serde = { version = "1", features = ["derive"], optional = true } | ||
serde-transcode = { version = "1.1.1", optional = true } | ||
serde_json = { version = "1.0.128", optional = true } | ||
|
@@ -36,14 +38,15 @@ rmp = { package = "rmp-serde", version = "1.3.0", optional = true } | |
[features] | ||
default = [ "ansi" ] | ||
|
||
full = [ "uau", "cmd", "ssh", "algo", "ende" ] | ||
full = [ "cmd", "uau", "ssh", "http", "algo", "ende" ] | ||
|
||
ansi = [] | ||
compact = [] | ||
|
||
uau = [ "nix", "rand" ] | ||
cmd = [] | ||
uau = [ "nix", "rand" ] | ||
ssh = [ "ssh2" ] | ||
http = [ "reqwest" ] | ||
|
||
algo = [ | ||
"algo_hash", | ||
|
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 |
---|---|---|
|
@@ -20,7 +20,7 @@ update: | |
cargo update --verbose | ||
|
||
fmt: | ||
cargo +nightly fmt | ||
cargo fmt | ||
|
||
fmtall: | ||
bash tools/fmt.sh | ||
|
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 |
---|---|---|
@@ -0,0 +1,91 @@ | ||
#![allow(missing_docs)] | ||
|
||
use crate::*; | ||
use reqwest::{ | ||
StatusCode, {Client, ClientBuilder}, | ||
}; | ||
use std::{env, sync::LazyLock, time::Duration}; | ||
|
||
static TIME_OUT: LazyLock<Duration> = LazyLock::new(|| { | ||
let secs = if let Ok(t) = env::var("RUC_HTTP_TIMEOUT") { | ||
pnk!(t.parse::<u64>()) | ||
} else { | ||
3 | ||
}; | ||
Duration::from_secs(secs) | ||
}); | ||
|
||
pub async fn http_get( | ||
url: &str, | ||
headers: Option<&[(&'static str, &'static str)]>, | ||
) -> Result<(StatusCode, Vec<u8>)> { | ||
let mut builder = http_cli().get(url); | ||
if let Some(headers) = headers { | ||
for (h, v) in headers.iter().copied() { | ||
builder = builder.header(h, v); | ||
} | ||
} | ||
let resp = builder.send().await.c(d!(url))?; | ||
let code = resp.status(); | ||
let msg = resp.bytes().await.c(d!())?; | ||
Ok((code, msg.into())) | ||
} | ||
|
||
pub async fn http_get_ret_string( | ||
url: &str, | ||
headers: Option<&[(&'static str, &'static str)]>, | ||
) -> Result<(StatusCode, String)> { | ||
http_get(url, headers) | ||
.await | ||
.c(d!()) | ||
.map(|(code, msg)| (code, String::from_utf8_lossy(&msg).into_owned())) | ||
} | ||
|
||
pub async fn http_post( | ||
url: &str, | ||
body: &[u8], | ||
headers: Option<&[(&'static str, &'static str)]>, | ||
) -> Result<(StatusCode, Vec<u8>)> { | ||
let mut builder = http_cli().post(url); | ||
if let Some(headers) = headers { | ||
for (h, v) in headers.iter().copied() { | ||
builder = builder.header(h, v); | ||
} | ||
} | ||
let resp = builder.body(body.to_owned()).send().await.c(d!(url))?; | ||
let code = resp.status(); | ||
let msg = resp.bytes().await.c(d!())?; | ||
Ok((code, msg.into())) | ||
} | ||
|
||
/// # Example | ||
/// | ||
/// ```ignore | ||
/// let url = "http://......"; | ||
/// let req = "...".as_bytes(); | ||
/// http_post_ret_string( | ||
/// url, | ||
/// req, | ||
/// Some(&[("Content-Type", "application/json")]), | ||
/// ) | ||
/// .await | ||
/// .c(d!(url)) | ||
/// ``` | ||
pub async fn http_post_ret_string( | ||
url: &str, | ||
body: &[u8], | ||
headers: Option<&[(&'static str, &'static str)]>, | ||
) -> Result<(StatusCode, String)> { | ||
http_post(url, body, headers) | ||
.await | ||
.c(d!()) | ||
.map(|(code, msg)| (code, String::from_utf8_lossy(&msg).into_owned())) | ||
} | ||
|
||
fn http_cli() -> Client { | ||
ClientBuilder::new() | ||
.timeout(*TIME_OUT) | ||
.http1_only() | ||
.build() | ||
.unwrap() | ||
} |
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