Crates.io | tinyfetch |
lib.rs | tinyfetch |
version | 0.1.0 |
source | src |
created_at | 2023-05-21 04:04:38.114828 |
updated_at | 2023-05-21 04:04:38.114828 |
description | A lightweight library for making HTTP requests in Rust. |
homepage | |
repository | |
max_upload_size | |
id | 869747 |
size | 12,931 |
TinyFetch is a lightweight Rust library for making HTTP requests with ease. It provides a simple and coherent interface for performing GET
, POST
, PUT
, PATCH
, and DELETE
requests. It is built on top of the reqwest crate and designed to be easy to use and maintain.
Add the tinyfetch
and tokio
crates as dependencies in your Rust project's Cargo.toml
file:
[dependencies]
tinyfetch = "0.1.0"
tokio = { version = "1", features = ["full"] }
Import the TinyFetch
struct into your Rust code:
use tinyfetch::TinyFetch;
Now you can use the TinyFetch methods to perform HTTP requests. Here's an example that demonstrates how to use each method:
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// GET
let response = TinyFetch::get("https://dummyjson.com/products/").await?;
println!("GET Response: {}", response);
// POST
let body = r#"
{
"name": "Example Product",
"price": 9.99,
"quantity": 10
}
"#;
let response = TinyFetch::post("https://dummyjson.com/products/add", body).await?;
println!("POST Response: {}", response);
// PUT
let body = r#"
{
"name": "Updated Product",
"price": 19.99,
"quantity": 5
}
"#;
let response = TinyFetch::put("https://dummyjson.com/products/1", body).await?;
println!("PUT Response: {}", response);
// PATCH
let body = r#"
{
"price": 14.99
}
"#;
let response = TinyFetch::patch("https://dummyjson.com/products/1", body).await?;
println!("PATCH Response: {}", response);
// DELETE
let response = TinyFetch::delete("https://dummyjson.com/products/1").await?;
println!("DELETE Response: {}", response);
Ok(())
}
Make sure to use the #[tokio::main]
attribute on your main
function to enable asynchronous execution with Tokio.
TinyFetch supports the following HTTP methods:
TinyFetch::get(url: &str) -> Result<String, Box<dyn std::error::Error>>
TinyFetch::post(url: &str, body: &str) -> Result<String, Box<dyn std::error::Error>>
TinyFetch::put(url: &str, body: &str) -> Result<String, Box<dyn std::error::Error>>
TinyFetch::patch(url: &str, body: &str) -> Result<String, Box<dyn std::error::Error>>
TinyFetch::delete(url: &str) -> Result<String, Box<dyn std::error::Error>>
Each method takes the URL as a string parameter and returns a Result containing the response body as a string or an error.
TinyFetch uses Rust's error handling mechanism to propagate errors. If an error occurs during the HTTP request, it will be returned as a Box?
or pattern matching, to handle these errors in your code.
cargo test
Contributions to TinyFetch are welcome! If you encounter any issues or have suggestions for improvements, please open an issue on the GitHub repository. Pull requests are also appreciated.
To contribute to the project, follow these steps:
Please ensure that your code follows the established coding style and conventions. Add tests to cover new functionality and ensure that existing tests pass.
TinyFetch is open-source software licensed under the MIT License. See the LICENSE file for more details.
For any further inquiries or questions, feel free to contact the maintainers of TinyFetch via the GitHub repository.
TinyFetch is inspired by the simplicity and functionality of Ruby's HTTParty library. Special thanks to the developers of reqwest and tokio for providing the necessary tools and libraries to make this project possible.