FeignHttp
FeignHttp is a declarative HTTP client. Based on rust macros.
Features
- Easy to use
- Asynchronous request
- Configurable timeout settings
- Supports form, plain text and JSON
- Selectable HTTP backends (reqwest or isahc)
Usage
FeignHttp mark macros on asynchronous functions, you need a runtime for support async/await. You can use async-std or tokio.
async-std:
[]
= { = "1", = ["attributes", "tokio1"] }
The feature tokio1
is need when use reqwest as the HTTP backend.
tokio:
[]
= { = "1", = ["full"] }
Add feignhttp in your Cargo.toml
and use default feature:
= { = "0.3" }
Then add the following code:
use get;
async
async
The get
attribute macro specifies get request, feignhttp::Result<String>
specifies the return result.
It will send get request to https://api.github.com
and receive a plain text body.
Using non-default HTTP backend:
= { = "0.3", = false, = ["isahc-client"] }
The default-features = false
option disable default reqwest.
Making a POST request
For a post request, you should use the post
attribute macro to specify request method and use a body
attribute to specify
a request body.
use post;
async
The #[body]
mark a request body. Function parameter text
is a String type, it will put in the request body as plain text.
String and &str will be put as plain text into the request body.
Paths
Using path
to specify path value:
use get;
async
async
dxx
will replace {owner}
and feignhttp
will replace {repo}
, the url to be send will be
https://api.github.com/repos/dxx/feignhttp
. You can specify a path name like #[path("owner")]
.
Query Parameters
Using query
to specify query parameter:
use get;
async
async
The page
parameter will as query parameter in the url. An url which will be send is https://api.github.com/repos/dxx/feignhttp?page=1
.
Note: A function parameter without
query
attribute will as a query parameter by default.
Headers
Using header
to specify request header:
use get;
async
async
A header accept:application/vnd.github.v3+json
will be send.
Form
Using form
to specify form parameter:
use post;
async
async
See here for more examples.
URL constant
We can use constant to maintain all urls of request:
use get;
const GITHUB_URL: &str = "https://api.github.com";
async
Url constant must be the first metadata in get attribute macro. You also can specify metadata key:
async
JSON
Serde is a framework for serializing and deserializing Rust data structures. When use json, you should add serde in Cargo.toml
:
[]
= { = "1", = ["derive"] }
Here is an example of getting json:
use get;
use Deserialize;
// Deserialize: Specifies deserialization
const GITHUB_URL: &str = "https://api.github.com";
async
async
This issues function return Vec<IssueItem>
, it is deserialized according to the content of the response.
Send a json request:
use post;
use Serialize;
async
async
See here for a complete example.
Using structure
Structure is a good way to manage requests. Define a structure and then define a large number of request methods:
use feign;
const GITHUB_URL: &str = "https://api.github.com";
;
See here for a complete example.
Timeout configuration
If you need to configure the timeout, use connect_timeout
and timeout
to specify connect timeout and read timeout.
Connect timeout:
async
Read timeout:
async
License
FeignHttp is provided under the MIT license. See LICENSE.