http_client_unix_domain_socket/lib.rs
1//! # http-client-unix-domain-socket
2//!
3//! > A simple HTTP (json) client using UNIX domain socket in Rust
4//!
5//! This Rust crate provides a Unix socket HTTP client for asynchronous communication with local servers. It enables seamless interaction via Unix domain sockets using Tokio and Hyper, supporting raw HTTP requests and optional JSON serialization through feature flags. The client handles connection management, request building, response parsing, and error handling, including reconnection logic.
6//!
7//! ## Examples
8//!
9//! ### HTTP GET
10//! ```rust
11//! use http_client_unix_domain_socket::{ClientUnix, Method, StatusCode, ErrorAndResponse};
12//!
13//! pub async fn get_hello_world() {
14//! let mut client = ClientUnix::try_new("/tmp/unix.socket")
15//! .await
16//! .expect("ClientUnix::try_new");
17//!
18//! match client
19//! .send_request("/nolanv", Method::GET, &vec![("Host", "localhost")], None)
20//! .await
21//! {
22//! Err(ErrorAndResponse::ResponseUnsuccessful(status_code, response)) => {
23//! assert!(status_code == StatusCode::NOT_FOUND);
24//! assert!(response == "not found".as_bytes());
25//! }
26//!
27//! Ok((status_code, response)) => {
28//! assert_eq!(status_code, StatusCode::OK);
29//! assert_eq!(response, "Hello nolanv".as_bytes());
30//! }
31//!
32//! Err(_) => panic!("Something went wrong")
33//! }
34//! }
35//! ```
36//! ### HTTP POST JSON **(feature = json)**
37//! ```rust
38//! use http_client_unix_domain_socket::{ClientUnix, Method, StatusCode, ErrorAndResponseJson};
39//! use serde::{Deserialize, Serialize};
40//!
41//! #[derive(Serialize)]
42//! struct NameJson {
43//! name: String,
44//! }
45//!
46//! #[derive(Deserialize)]
47//! struct HelloJson {
48//! hello: String,
49//! }
50//!
51//! #[derive(Deserialize, Debug)]
52//! struct ErrorJson {
53//! msg: String,
54//! }
55//!
56//! pub async fn post_hello_world() {
57//! let mut client = ClientUnix::try_new("/tmp/unix.socket")
58//! .await
59//! .expect("ClientUnix::try_new");
60//!
61//! match client
62//! .send_request_json::<NameJson, HelloJson, ErrorJson>(
63//! "/nolanv",
64//! Method::POST,
65//! &vec![("Host", "localhost")],
66//! Some(&NameJson { name: "nolanv".into() }))
67//! .await
68//! {
69//! Err(ErrorAndResponseJson::ResponseUnsuccessful(status_code, response)) => {
70//! assert!(status_code == StatusCode::BAD_REQUEST);
71//! assert!(response.msg == "bad request");
72//! }
73//!
74//! Ok((status_code, response)) => {
75//! assert_eq!(status_code, StatusCode::OK);
76//! assert_eq!(response.hello, "nolanv");
77//! }
78//!
79//! Err(_) => panic!("Something went wrong")
80//! }
81//! }
82//! ```
83//! ## Feature flags
84//! - `json`(default): Add `send_request_json` which enable automatic parsing of request/response body with `serde_json` and add `Content-Type` header.
85
86mod client;
87mod error;
88#[cfg(test)]
89pub mod test_helpers;
90
91pub use axum_core::body::Body;
92pub use client::ClientUnix;
93#[cfg(feature = "json")]
94pub use error::ErrorAndResponseJson;
95pub use error::{Error, ErrorAndResponse};
96pub use hyper::Method;
97pub use hyper::StatusCode;