Support request header usage struct
Currently #[form] supports struct So can #[header] also support it? Because the request headers of some similar interfaces are basically the same, and there are many request headers for some interfaces with verification, so it will be very elegant if the use of struct is supported.
for example:
#[derive(Debug, Serialize)]
pub struct Header {
pub user_agent: String,
pub access_token: String,
}
#[post(url = "http://127.0.0.1")]
pub async fn helloWorld(&self, #[header] header: Header) -> feignhttp::Result<String> {}
You can see here.
This is an example:
#[derive(Feign)]
pub struct Header {
#[header]
pub user_agent: String,
#[header]
pub access_token: String,
}
#[feign(url = "http://127.0.0.1")]
impl Header {
#[post]
pub async fn helloWorld(&self) -> feignhttp::Result<String> {}
}
I saw this usage and I currently use it this way, but I prefer not to rely entirely on impl's struct. Suppose there are 2 methods under the Header. Only a few of their properties are public and the rest are different. If they are all put together So most of the book attributes for a certain method are redundant, so I hope #[header]#[query] can be as flexible as #[form]#[body]
for example:
#[derive(Feign)]
pub struct HttpClient {
#[url_path("owner")]
user: String,
#[url_path]
repo: String,
#[param]
accept: String,
}
#[derive(Feign)]
pub struct Header {
#[serde(rename = "userAgent")]
pub user_agent: String,
pub access_token: String,
}
#[derive(Feign)]
pub struct Query {
#[serde(rename = "query1")]
pub query_1: String,
pub query_2: String,
}
#[feign(url = "http://127.0.0.1")]
impl HttpClient {
#[post]
pub async fn post(&self, #[header] header: Header)) -> feignhttp::Result<String> {}
#[get]
pub async fn get(&self, #[query] query: Query)) -> feignhttp::Result<String> {}
}
This commit already supports that use structures in parameter of header and query.
Usage:
use serde::Serialize;
// Example for header.
#[derive(Serialize)]
pub struct Header {
pub accept: &'static str,
pub token: String,
}
#[get("https://httpbin.org/headers")]
async fn struct_headers(#[header] head: Header) -> feignhttp::Result<String> {}
// Example for query.
#[derive(Serialize)]
pub struct Query {
pub id: i32,
pub name: String
}
#[get("https://httpbin.org/anything")]
async fn anything_struct(#[query] q: Query) -> feignhttp::Result<String> {}
This is available in version 0.5.2.