Deboa

A very simple and straightforward HTTP client written in Rust.

View the Project on GitHub ararog/deboa

Deboa Core

The core HTTP client library for Rust, providing a simple yet powerful interface for making HTTP requests.

Features

Installation

Add to your Cargo.toml:

[dependencies]
deboa = { version = "0.0.7", features = ["http1", "tokio-rt"] }

Basic Usage

use deboa::{Deboa, request::get};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Deboa::new();
    
    // Make a GET request
    let response = get("https://httpbin.org/get")
        .send_with(&client)
        .await?;
        
    println!("Status: {}", response.status());
    println!("Body: {}", response.text().await?);
    
    Ok(())
}

Making Requests

GET Request

let response = deboa::get("https://api.example.com/data")
    .header("Accept", "application/json")
    .send_with(&client)
    .await?;

POST Request with JSON

use serde_json::json;

let data = json!({ "name": "John Doe", "age": 30 });

let response = deboa::post("https://api.example.com/users")
    .json(&data)?
    .send_with(&client)
    .await?;

Handling Responses

#[derive(serde::Deserialize)]
struct User {
    id: u64,
    name: String,
    email: String,
}

// Parse JSON response into a struct
let user: User = deboa::get("https://api.example.com/users/1")
    .send_with(&client)
    .await?
    .body_as_json()?;

// Get response as text
let text = response.text().await?;

// Get response as bytes
let bytes = response.bytes().await?;

Middleware

Deoba supports middleware for request/response processing:

use deboa::middleware::{Middleware, Next};

struct LoggingMiddleware;

#[async_trait::async_trait]
impl Middleware for LoggingMiddleware {
    async fn handle(&self, req: deboa::Request, next: Next<'_>) -> Result<deboa::Response, deboa::Error> {
        println!("Sending request to {} {}", req.method(), req.uri());
        let start = std::time::Instant::now();
        let res = next.run(req).await?;
        let duration = start.elapsed();
        println!("Received response in {:?} - {}", duration, res.status());
        Ok(res)
    }
}

// Create a client with middleware
let client = deboa::Deboa::builder()
    .with(LoggingMiddleware)
    .build();

Error Handling

Deoba provides comprehensive error handling through the deboa::Error type:

match deboa::get("https://api.example.com/data").send_with(&client).await {
    Ok(response) => {
        // Handle successful response
    }
    Err(deboa::Error::Http(e)) => {
        // Handle HTTP errors
    }
    Err(deboa::Error::Json(e)) => {
        // Handle JSON parsing errors
    }
    Err(e) => {
        // Handle other errors
    }
}

Features

Examples

See the examples directory for more usage examples.

API Reference

For detailed API documentation, see the docs.rs page.