-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
267 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/// This file is a an adapted copy of [`alloy::transports::http::Http`] that can work with | ||
/// [`reqwest_middleware`]. | ||
// TODO: Consider upstreaming support to alloy | ||
use alloy::rpc::json_rpc::{RequestPacket, ResponsePacket}; | ||
use alloy::transports::{TransportError, TransportErrorKind, TransportFut}; | ||
use reqwest_middleware::ClientWithMiddleware; | ||
use std::task::{Context, Poll}; | ||
use tower::Service; | ||
|
||
#[derive(Clone)] | ||
pub struct HttpWithMiddleware { | ||
client: ClientWithMiddleware, | ||
url: reqwest::Url, | ||
} | ||
|
||
impl HttpWithMiddleware { | ||
/// Create a new [`HttpWithMiddleware`] transport with a custom client. | ||
pub const fn with_client(client: ClientWithMiddleware, url: reqwest::Url) -> Self { | ||
Self { client, url } | ||
} | ||
|
||
/// Make a request. | ||
fn request_reqwest(&self, req: RequestPacket) -> TransportFut<'static> { | ||
let this = self.clone(); | ||
Box::pin(async move { | ||
let resp = this | ||
.client | ||
.post(this.url) | ||
.json(&req) | ||
.send() | ||
.await | ||
.map_err(TransportErrorKind::custom)?; | ||
let status = resp.status(); | ||
|
||
// Unpack data from the response body. We do this regardless of | ||
// the status code, as we want to return the error in the body | ||
// if there is one. | ||
let body = resp.bytes().await.map_err(TransportErrorKind::custom)?; | ||
|
||
if status != reqwest::StatusCode::OK { | ||
return Err(TransportErrorKind::http_error( | ||
status.as_u16(), | ||
String::from_utf8_lossy(&body).into_owned(), | ||
)); | ||
} | ||
|
||
// Deserialize a Box<RawValue> from the body. If deserialization fails, return | ||
// the body as a string in the error. The conversion to String | ||
// is lossy and may not cover all the bytes in the body. | ||
serde_json::from_slice(&body) | ||
.map_err(|err| TransportError::deser_err(err, String::from_utf8_lossy(&body))) | ||
}) | ||
} | ||
} | ||
|
||
impl Service<RequestPacket> for HttpWithMiddleware { | ||
type Response = ResponsePacket; | ||
type Error = TransportError; | ||
type Future = TransportFut<'static>; | ||
|
||
#[inline] | ||
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { | ||
// reqwest always returns ok | ||
Poll::Ready(Ok(())) | ||
} | ||
|
||
#[inline] | ||
fn call(&mut self, req: RequestPacket) -> Self::Future { | ||
self.request_reqwest(req) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
#![allow(async_fn_in_trait)] | ||
|
||
mod ext; | ||
mod http_middleware; | ||
mod provider; | ||
mod utils; | ||
|
||
pub use ext::{ReceiptExt, ZksyncWalletProviderExt}; | ||
pub use provider::{init_testing_provider, init_testing_provider_with_http_headers, AnvilZKsyncApi, TestingProvider, DEFAULT_TX_VALUE}; | ||
pub use provider::{ | ||
init_testing_provider, init_testing_provider_with_client, AnvilZKsyncApi, TestingProvider, | ||
DEFAULT_TX_VALUE, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.