Skip to content

Commit

Permalink
add the http module
Browse files Browse the repository at this point in the history
  • Loading branch information
haxjump committed Oct 7, 2024
1 parent c57f600 commit 0f659ec
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 5 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Change log

#### v7.x

- Reorganize modules
- Add new toolkits
- http(no https support)
- compress/uncompress: zlib
- encode/decode: message pack, json, etc.

#### v6.x

- Remove the trie related functions
Expand Down
11 changes: 7 additions & 4 deletions Cargo.toml
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"
Expand All @@ -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 }
Expand All @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ update:
cargo update --verbose

fmt:
cargo +nightly fmt
cargo fmt

fmtall:
bash tools/fmt.sh
Expand Down
91 changes: 91 additions & 0 deletions src/http.rs
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()
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ pub mod ssh;
#[cfg(target_os = "linux")]
pub mod uau;

#[cfg(feature = "http")]
pub mod http;

pub mod algo;

pub mod ende;
Expand Down

0 comments on commit 0f659ec

Please sign in to comment.