Crates.io | api-request-utils-rs |
lib.rs | api-request-utils-rs |
version | 0.2.5 |
source | src |
created_at | 2023-06-20 16:29:11.221327 |
updated_at | 2023-09-01 07:06:17.957491 |
description | This library aims to provide a straightforward and efficient solution for making api requests It is designed to be user-friendly, customizable, and extensible, allowing developers to easily integrate and interact with APIs in their Rust applications. |
homepage | https://github.com/Deaths-Door/api-request-utils-rs |
repository | https://github.com/Deaths-Door/api-request-utils-rs |
max_upload_size | |
id | 895339 |
size | 18,255 |
This library aims to provide a straightforward and efficient solution for making API requests. It is designed to be user-friendly, customizable, and extensible, allowing developers to easily integrate and interact with APIs in their Rust applications.
Add the following line to your Cargo.toml
file:
api-request-utils = "0.2.4" # Note : Latest version at time of writing
Here are some projects that are using api-request-utils-rs
:
If you're using api-request-utils-rs
in your project, feel free to contact me to add it to this list!
Before you can start making API requests, you need to create an API client that implements the necessary traits. Here's an example of how you can define and implement the API client struct:
use api_request_utils::*;
struct MyAPIClient {
// Define your API client fields here such as the client
}
impl RequestInfo for MyAPIClient {
const BASE_URL: &'static str = "https://api.example.com"; // Replace with the base url
fn client(&self) -> &reqwest::Client {
// Return your reqwest::Client instance here
}
}
// Note : In most cases the default implementations are enough
impl RequestModifiers for MyAPIClient {} // Implement methods for adding headers, modifying requests, etc.
impl RequestDefaults for MyAPIClient {} // Implement default headers, parameters, and request builders
impl RequestHandler for MyAPIClient {} // Default settings should be enought
To make a GET request, you can use the get_request_handler
method provided by the RequestHandler
trait. Here's an example:
#[tokio::main]
async fn main() {
let api_client = MyAPIClient::new();
let parameters: HashMap<&str, serde_json::Value> = /* Define your request parameters */;
let result = api_client.get_request_handler("endpoint", ¶meters, |response| response, |error| {
// Handle error cases
}).await;
match result {
Some(response_data) => {
// Process the response data
}
None => {
// Handle the error case
}
}
}
For making a POST request, you can utilize the post_request_handler
method similarly. Here's an example:
#[tokio::main]
async fn main() {
let api_client = MyAPIClient::new();
let json_payload : String = /* Define your JSON payload */;
let result = api_client.post_request_handler("endpoint", json_payload, |response| response, |error| {
// Handle error cases
}).await;
match result {
Some(response_data) => {
// Process the response data
}
None => {
// Handle the error case
}
}
}
The library provides an RequestError
enum to handle different types of request errors. You can pattern match on this enum to handle specific error scenarios:
use api_request_utils::RequestError;
match error {
RequestError::RequestError(reqwest_error) => {
// Handle request sending errors
}
RequestError::InvalidJsonBody(json_error) => {
// Handle invalid JSON response body errors
}
RequestError::ErrorPayload(custom_error) => {
// Handle custom error payloads from unsuccessful requests
}
RequestError::InvalidJsonBody(serde_json_error) => {
// Handle invalid josn errors
}
}
Please note that the examples provided here are simplified and serve as a starting point. For comprehensive documentation of the crate, please visit the crate documentation for a better understanding of the crate's functionalities and APIs.
Contributions are welcome! If you find any issues or have suggestions for improvement, please open an issue or submit a pull request.