Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support request header usage struct #10

Open
liyang-hg opened this issue Apr 9, 2024 · 3 comments
Open

Support request header usage struct #10

liyang-hg opened this issue Apr 9, 2024 · 3 comments

Comments

@liyang-hg
Copy link

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> {}
@liyang-hg liyang-hg changed the title Support request header usage structu Support request header usage struct Apr 9, 2024
@dxx
Copy link
Owner

dxx commented May 10, 2024

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> {}
}

@liyang-hg
Copy link
Author

liyang-hg commented May 21, 2024

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> {}
}

@dxx
Copy link
Owner

dxx commented May 24, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants