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

feat: Allow user to send custom headers in http client #756

Closed
wants to merge 1 commit into from

Conversation

semtexzv
Copy link

@semtexzv semtexzv commented May 7, 2022

As a user, I want to use authorization for some requests, or implement cache-control on top of my RPCs to bitcoin nodes.

This seems like a simplest way to achieve that.

@semtexzv semtexzv requested a review from a team as a code owner May 7, 2022 17:53
@@ -77,10 +79,17 @@ impl HttpClientBuilder {
self
}

///
pub fn custom_headers(mut self, headers: HeaderMap) -> Self {
Copy link
Member

@niklasad1 niklasad1 May 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather have a more unified API for the WS client and HTTP client, so if you could add a similar API as https://github.com/paritytech/jsonrpsee/blob/master/client/ws-client/src/lib.rs#L125-#L128 I would be happy with it.

The reason why is to avoid to require users to use http::HeaderMap however we could also re-export HeaderMap but I'm in favor of the WS client API for configuring custom headers.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about now?

Copy link
Member

@niklasad1 niklasad1 May 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, I think it would make more sense to return an error instead of unwrap these might fail for example if HeaderName is uppercase and similar.

in addition an already existing header will just be overwritten with append.

so might be better what you did initially with HeaderMap for simplicity and change the WS client API instead.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

//cc @jsdw any opinions?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I Added a custom error variant, and made the method fallible. I think this is the nicest we can make this without exposing extraneous APIs.

I also would like to keep the ability to overwrite default headers, what if user wants to send Content-Type: application/json+jsonrpc instead of the default Content-type: application/json

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense,

I think the docs should explain that you can overwrite the default headers too perhaps the doc tests/examples regarding that would awesome :)

Copy link
Member

@niklasad1 niklasad1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks overall good,

Some suggestions on the custom_headers API to discuss further though.

@semtexzv semtexzv force-pushed the master branch 2 times, most recently from 9670a7c to 397a71f Compare May 8, 2022 05:08
@semtexzv semtexzv requested a review from niklasad1 May 8, 2022 05:12
@@ -77,10 +82,20 @@ impl HttpClientBuilder {
self
}

/// Adds a custom header, which is sent with every request
Copy link
Member

@niklasad1 niklasad1 May 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add some additional docs that explains that "dupllicate" keys will be overwritten by this method?

in addition the docs should explain that you can overwrite the default headers too perhaps the doc tests/examples regarding that would awesome :)

Copy link
Member

@niklasad1 niklasad1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, please run rustfmt to make the CI happy

Comment on lines +133 to +134
#[error("Invalid http headers provided")]
InvalidHeaders,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#[error("Invalid http headers provided")]
InvalidHeaders,
#[error("Invalid http header provided")]
InvalidHeader,

(just because this error is emitted when one attempt to set a single header)

@@ -77,10 +82,20 @@ impl HttpClientBuilder {
self
}

/// Adds a custom header, which is sent with every request
pub fn add_header(mut self, key: &str, value: &str) -> Result<Self, Error> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@niklasad1 in the WS client, we accept &str header names and values and just return Self. Is there any reason why we shouldn't have the same API here do you think?

Copy link
Member

@niklasad1 niklasad1 May 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's because two different http libraries are used, soketto is using to httparse::Header which isn't as pedantic as hyper::HeaderMap is.

that's why I was leaning into exposing fn add_headers(header: HeaderMap) -> Self for both instead which makes things more uniform and quite obvious how it works.

Copy link
Collaborator

@jsdw jsdw May 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see! I guess the issue there is that the ws-client doesn't (afaict) depend on hyper, so it would be a shame to add it as a required dependency just for that type?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like https://docs.rs/http/latest/http/index.html is sufficient which doesn't really have much deps at all.

However, the better fix is probably to use http directly in soketto otherwise we need to convert it to httparse::Header anyway.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmm that makes sense!

So regardless of whether we merge this, perhaps we should create another issue/PR to migrate soketto to using http so that we can standardise on this interface (and on what we're using for headers across the two clients, which would be nice)

Copy link
Collaborator

@jsdw jsdw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy with this once fmt and such are fixed, but as @niklasad1 suggested I think it'd be good to move to using something like https://docs.rs/http/latest/http/header/struct.HeaderMap.html consistently across clients and then we can expose a more consistent API for doing this, too.

@niklasad1
Copy link
Member

@semtexzv are you planning on finishing this or shall we take it over?

It's a useful PR for sure.

@semtexzv
Copy link
Author

Hey, sorry. I moved on to custom implementation of jsonrpc, feel free to take over.

@niklasad1
Copy link
Member

Superseeded by #814

@niklasad1 niklasad1 closed this Jul 18, 2022
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 this pull request may close these issues.

3 participants