-
Notifications
You must be signed in to change notification settings - Fork 178
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,12 +24,16 @@ | |
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
// DEALINGS IN THE SOFTWARE. | ||
|
||
use std::str::FromStr; | ||
use std::sync::Arc; | ||
use std::time::Duration; | ||
|
||
use crate::transport::HttpTransportClient; | ||
use crate::types::{ErrorResponse, Id, NotificationSer, ParamsSer, RequestSer, Response}; | ||
use async_trait::async_trait; | ||
use hyper::header::{HeaderName, InvalidHeaderName}; | ||
use hyper::http::HeaderValue; | ||
use hyper::HeaderMap; | ||
use jsonrpsee_core::client::{CertificateStore, ClientT, IdKind, RequestIdManager, Subscription, SubscriptionClientT}; | ||
use jsonrpsee_core::{Error, TEN_MB_SIZE_BYTES}; | ||
use jsonrpsee_types::error::CallError; | ||
|
@@ -44,6 +48,7 @@ pub struct HttpClientBuilder { | |
max_concurrent_requests: usize, | ||
certificate_store: CertificateStore, | ||
id_kind: IdKind, | ||
custom_headers: HeaderMap, | ||
} | ||
|
||
impl HttpClientBuilder { | ||
|
@@ -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> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @niklasad1 in the WS client, we accept There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's because two different that's why I was leaning into exposing There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
self.custom_headers.append( | ||
HeaderName::from_str(key).map_err(|_| Error::InvalidHeaders)?, | ||
HeaderValue::from_str(value).map_err(|_| Error::InvalidHeaders)?, | ||
); | ||
Ok(self) | ||
} | ||
|
||
/// Build the HTTP client with target to connect to. | ||
pub fn build(self, target: impl AsRef<str>) -> Result<HttpClient, Error> { | ||
let transport = HttpTransportClient::new(target, self.max_request_body_size, self.certificate_store) | ||
.map_err(|e| Error::Transport(e.into()))?; | ||
let transport = | ||
HttpTransportClient::new(target, self.max_request_body_size, self.certificate_store, self.custom_headers) | ||
.map_err(|e| Error::Transport(e.into()))?; | ||
Ok(HttpClient { | ||
transport, | ||
id_manager: Arc::new(RequestIdManager::new(self.max_concurrent_requests, self.id_kind)), | ||
|
@@ -97,6 +112,7 @@ impl Default for HttpClientBuilder { | |
max_concurrent_requests: 256, | ||
certificate_store: CertificateStore::Native, | ||
id_kind: IdKind::Number, | ||
custom_headers: HeaderMap::new(), | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
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 :)