neto 0.0.4

Enhanced HTTP client API wrapper for reqwest with improved ergonomics
Documentation

neto

Crates.io Docs.rs License GitHub top language GitHub stars GitHub forks Tests Crates.io downloads GitHub last commit

neto is a Rust crate providing a flexible HTTP client abstraction with a focus on builder patterns, header management, and easy configuration of reqwest::Client. It supports both manual client injection and automatic client configuration with default headers.


โœจ Features

  • ๐Ÿ—๏ธ Fluent builder API for Http client with support for base URL, headers, and client.
  • โš™๏ธ Automatic creation of reqwest::Client with default headers via .config().
  • ๐Ÿ” Easy injection of authorization tokens and other headers.
  • ๐Ÿ”„ Support for both manual Client passing and automatic client configuration.
  • ๐Ÿš€ Async HTTP request helpers using reqwest.
  • ๐Ÿงช Comprehensive testing with real API calls (e.g., PokeAPI).

โš™๏ธ Installation

Add it to your Cargo.toml:

cargo add neto

๐Ÿš€ Usage

Manual Client injection

Create an Http instance by passing a manually created reqwest::Client. Headers are stored but not applied automatically to the client.

use neto::components::data::Http;
use reqwest::{Client, header::{HeaderValue, USER_AGENT}};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let headers = vec![(USER_AGENT, HeaderValue::from_static("neto-http-test/1.0"))];

    let http = Http::new()
        .base_url("https://pokeapi.co/api/v2")
        .headers(headers)
        .client(Client::new())
        .build()
        .expect("Should build Http");

    let response = http.get("/pokemon/ditto", Vec::new()).await?;

    assert!(response.status().is_success());

    let json: serde_json::Value = response.json().await?;
    println!("Name: {}", json["name"]);

    Ok(())
}

Automatic client creation with .config()

Build the Http struct without passing a client, then call .config() to create the reqwest::Client internally with all headers applied as default headers.

use neto::components::data::Http;
use reqwest::header::{HeaderValue, USER_AGENT};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let headers = vec![(USER_AGENT, HeaderValue::from_static("neto-http-test/1.0"))];

    let mut http = Http::new()
        .base_url("https://pokeapi.co/api/v2")
        .headers(headers)
        .build()
        .expect("Should build Http");

    http.config().expect("Failed to configure Http");

    let response = http.get("/pokemon/ditto", Vec::new()).await?;

    assert!(response.status().is_success());

    let json: serde_json::Value = response.json().await?;
    println!("Name: {}", json["name"]);

    Ok(())
}

โค๏ธ Donate

Monero Bitcoin