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

Access to Request from RequestBuilder #1212

Closed
Ten0 opened this issue Mar 9, 2021 · 3 comments · Fixed by #1770
Closed

Access to Request from RequestBuilder #1212

Ten0 opened this issue Mar 9, 2021 · 3 comments · Fixed by #1770

Comments

@Ten0
Copy link

Ten0 commented Mar 9, 2021

We have a use-case where, while building a request through a RequestBuilder, we need to access the currently generated url of the request to generate a signature.
e.g.

fn prepare_request() -> Result<RequestBuilder, /* ... */> {
    Ok(client
        .get(url)
        .query(
            &[("lang", lang)]), // or any other serializable structure, possibly a generic type parameter...
        )
        .header("signature", hmac(/*need full URL here*/, secret)))
 }

For that use-case, it would be useful to be able to access the current Request from the RequestBuilder: we could then write this function in the following way:

fn prepare_request() -> Result<RequestBuilder, /* ... */> {
    let request_builder = client
        .get(url)
        .query(
            &[("lang", lang)]), // or any other serializable structure, possibly a generic type parameter...
        );
    Ok(request_builder.header("signature", hmac(request_builder.request()?.url(), secret)))
 }
@huacnlee
Copy link

I have a same problem, please make RequestBuilder.request as public.

@ducaale
Copy link
Sponsor Contributor

ducaale commented Feb 12, 2023

@Ten0 what is the reason behind returning RequestBuilder instead of Request? You can always add the signature later by mutating your request object via Request::headers_mut().

@Ten0
Copy link
Author

Ten0 commented Feb 13, 2023

It's part of a request building framework where other operands may be chained behind, so it is required that the return of each operand is a RequestBuilder.

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

Successfully merging a pull request may close this issue.

3 participants