API SDK
A highlevel API client framework for Rust.
- Built on top of reqwest to handle HTTP requests
- Macros to define API and send requests
- Send request as JSON / form / multipart
- Parse response by serde, and extract a part of payload as result value
- Support
X-Request-IDandX-Trace-ID/X-Span-ID - More customizations
- Rewrite
hostandportof URLs by usingApiRouter - Set
Authorizationheader by usingApiSignature - Provide middlewares by integrate reqwest-middleware
- Mock server response by using
MockServer
- Rewrite
- Changelog
Motivation
When using reqwest to send API requests to server side, we have to do some common work. Including setting authentication information, parsing request responses, handle exceptions, adding log and tracking information, etc.
For this reason, we often develop some auxiliary functions to achieve the above functions. The design purpose of this crate is to simplify this part of the development work and provide a common design implementation.
Get Start
To define a very simple API, we just need a few lines of code.
use ;
// Define an API struct
// optional
;
To use the API, just follow these steps.
use ApiResult;
async
Key Points
http_api and api_method macros
http_api- declare a struct as an API
#[http_api("https://api.site/base")]
api_method- (optional) refine an API method
customize API instance
We can use XxxApi::builder() to get an instance of ApiBuilder, and call following functions to customize API instance.
with_router- rewrite host and port
with_signature- set credentials for each request
with_initialiser&with_middleware- support all
reqwest-middlewarecomponents
- support all
with_log- enable/disable logs in processing requests
create HTTP request
The http_api defines several functions for XxxApi to create HTTP request.
- create with HTTP method
async fn request(method: Method, path: impl AsRef<str>) -> ApiResult<RequestBuilder>
- quick functions
async fn head(path: impl AsRef<str>) -> ApiResult<RequestBuilder>async fn get(path: impl AsRef<str>) -> ApiResult<RequestBuilder>async fn post(path: impl AsRef<str>) -> ApiResult<RequestBuilder>async fn put(path: impl AsRef<str>) -> ApiResult<RequestBuilder>async fn patch(path: impl AsRef<str>) -> ApiResult<RequestBuilder>async fn delete(path: impl AsRef<str>) -> ApiResult<RequestBuilder>async fn options(path: impl AsRef<str>) -> ApiResult<RequestBuilder>async fn trace(path: impl AsRef<str>) -> ApiResult<RequestBuilder>
extends RequestBuilder
This crate re-export RequestBuilder from reqwest-middleware, and provides several useful extensions. We may use req.with_extension() to apply these extensions.
RequestId- set value of
X-Request-ID
- set value of
TraceId- set value of
X-Trace-IDand/orX-Span-ID
- set value of
MockServer- mock the server response by using
serde_json::Value
- mock the server response by using
send macros
send- send request, and not detect or process the payload
send_json- send request with JSON payload
send_form- send request with urlencoded form or multipart form
send_multipart- send request with multipart form