Crate api_tools

Source
Expand description

§Api Tools - A toolkit for API in Rust

Toolkit for API in Rust

API Tools is a Rust library providing utilities for developing robust, consistent, and secure APIs. It offers ready-to-use layers, extractors, error handling, and helpers designed to simplify API development, especially with the Axum framework. The toolkit aims to standardize common API patterns and reduce boilerplate in your Rust projects.

§Features list

NameDescriptionDefault
axumEnable Axum feature
prometheusEnable Prometheus metrics feature
fullEnable all features

§Components

§Value objects

NameDescription
UtcDateTimeA wrapper around chrono::DateTime to handle date and time values in UTC
TimezoneA wrapper around chrono_tz::Tz to handle time zones
PaginationA struct to handle pagination parameters, including page number, page size and total count
QuerySortA struct to handle sorting query parameters, including field and direction

§Axum

§Layers
NameDescription
BasicAuthLayerProvides HTTP Basic Authentication middleware for protecting routes with username and password
CorsLayerAdds Cross-Origin Resource Sharing (CORS) headers to responses, allowing or restricting resource sharing between different origins
HttpErrorsLayerMiddleware for intercepting and customizing HTTP error responses, enabling standardized error handling across your API
LoggerLayerLogs incoming requests and outgoing responses, useful for debugging and monitoring API activity
RequestIdMiddleware that generates and attaches a unique request identifier (UUID) to each incoming request for traceability
TimeLimiterLayerMiddleware that restricts API usage to specific time slots. Outside of these allowed periods, it returns a 503 Service Unavailable error
PrometheusLayerMiddleware that collects and exposes Prometheus-compatible metrics for monitoring API performance and usage
§Utility functions
NameDescription
body_from_partsConstruct a response body from Parts, status code, message and headers
header_value_to_strConvert HeaderValue to &str
§Extractors
NameDescription
ExtractRequestIdExtracts the unique request identifier (UUID) from the request headers
PathExtracts and deserializes path parameters from the request URL
QueryExtracts and deserializes query string parameters from the request URL
§Response helpers
NameDescription
ApiSuccessRepresents a successful API response (Status code and data in JSON). It implements the IntoResponse trait
ApiErrorRepresents a list of HTTP errors
ApiErrorResponseEncapsulates the details of an API error response, including the status code and the error message
§Handlers
NameDescription
PrometheusHandlerHandler that exposes Prometheus metrics endpoint, allowing metrics scraping by Prometheus servers

Modules§

security
Security module
server
Server module
value_objects
Value objects list