Skip to content

Commit

Permalink
feat(jsonrpc): custom request headers (#579)
Browse files Browse the repository at this point in the history
  • Loading branch information
xJonathanLEI authored Apr 4, 2024
1 parent a6ff645 commit fb481ee
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions starknet-providers/src/jsonrpc/transports/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::jsonrpc::{transports::JsonRpcTransport, JsonRpcMethod, JsonRpcRespons
pub struct HttpTransport {
client: Client,
url: Url,
headers: Vec<(String, String)>,
}

#[derive(Debug, thiserror::Error)]
Expand All @@ -35,8 +36,27 @@ impl HttpTransport {
Self {
client,
url: url.into(),
headers: vec![],
}
}

/// Consumes the current [HttpTransport] instance and returns a new one with the header
/// appended. Same as calling [add_header].
pub fn with_header(self, name: String, value: String) -> Self {
let mut headers = self.headers;
headers.push((name, value));

Self {
client: self.client,
url: self.url,
headers,
}
}

/// Adds a custom HTTP header to be sent for requests.
pub fn add_header(&mut self, name: String, value: String) {
self.headers.push((name, value))
}
}

#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
Expand All @@ -63,14 +83,16 @@ impl JsonRpcTransport for HttpTransport {
let request_body = serde_json::to_string(&request_body).map_err(Self::Error::Json)?;
trace!("Sending request via JSON-RPC: {}", request_body);

let response = self
let mut request = self
.client
.post(self.url.clone())
.body(request_body)
.header("Content-Type", "application/json")
.send()
.await
.map_err(Self::Error::Reqwest)?;
.header("Content-Type", "application/json");
for (name, value) in self.headers.iter() {
request = request.header(name, value);
}

let response = request.send().await.map_err(Self::Error::Reqwest)?;

let response_body = response.text().await.map_err(Self::Error::Reqwest)?;
trace!("Response from JSON-RPC: {}", response_body);
Expand Down

0 comments on commit fb481ee

Please sign in to comment.