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: HttpConnect #786

Merged
merged 2 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions crates/transport-http/src/hyper_transport.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::Http;
use crate::{Http, HttpConnect};
use alloy_json_rpc::{RequestPacket, ResponsePacket};
use alloy_transport::{TransportError, TransportErrorKind, TransportFut};
use alloy_transport::{
utils::guess_local_url, TransportConnect, TransportError, TransportErrorKind, TransportFut,
};
use http_body_util::{BodyExt, Full};
use hyper::{
body::{Buf, Bytes},
Expand All @@ -20,6 +22,27 @@ pub type HyperClient = hyper_util::client::legacy::Client<
/// An [`Http`] transport using [`hyper`].
pub type HyperTransport = Http<HyperClient>;

/// Connection details for a [`HyperTransport`].
pub type HyperConnect = HttpConnect<HyperTransport>;

impl TransportConnect for HyperConnect {
type Transport = HyperTransport;

fn is_local(&self) -> bool {
guess_local_url(self.url.as_str())
}

fn get_transport<'a: 'b, 'b>(
&'a self,
) -> alloy_transport::Pbf<'b, Self::Transport, TransportError> {
let executor = hyper_util::rt::TokioExecutor::new();

let client = hyper_util::client::legacy::Client::builder(executor).build_http();

Box::pin(async move { Ok(Http::with_client(client, self.url.clone())) })
}
}

impl<C, B> Http<Client<C, Full<B>>>
where
C: Connect + Clone + Send + Sync + 'static,
Expand Down
31 changes: 31 additions & 0 deletions crates/transport-http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#[cfg(feature = "reqwest")]
mod reqwest_transport;

#[cfg(feature = "reqwest")]
#[doc(inline)]
pub use reqwest_transport::*;
Expand All @@ -36,8 +37,38 @@ pub use hyper;
pub use hyper_util;

use alloy_transport::utils::guess_local_url;
use core::{marker::PhantomData, str::FromStr};
use url::Url;

/// Connection details for an HTTP transport.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[doc(hidden)]
pub struct HttpConnect<T> {
/// The URL to connect to.
url: Url,
_pd: PhantomData<T>,
}

impl<T> HttpConnect<T> {
/// Create a new [`HttpConnect`] with the given URL.
pub const fn new(url: Url) -> Self {
Self { url, _pd: PhantomData }
}

/// Get a reference to the URL.
pub const fn url(&self) -> &Url {
&self.url
}
}

impl<T> FromStr for HttpConnect<T> {
type Err = url::ParseError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self::new(s.parse()?))
}
}

/// An Http transport.
///
/// The user must provide an internal http client and a URL to which to
Expand Down
23 changes: 21 additions & 2 deletions crates/transport-http/src/reqwest_transport.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::Http;
use crate::{Http, HttpConnect};
use alloy_json_rpc::{RequestPacket, ResponsePacket};
use alloy_transport::{TransportError, TransportErrorKind, TransportFut};
use alloy_transport::{
utils::guess_local_url, TransportConnect, TransportError, TransportErrorKind, TransportFut,
};
use std::task;
use tower::Service;
use tracing::{debug, debug_span, trace, Instrument};
Expand All @@ -12,6 +14,23 @@ pub use reqwest::Client;
/// An [`Http`] transport using [`reqwest`].
pub type ReqwestTransport = Http<Client>;

/// Connection details for a [`ReqwestTransport`].
pub type ReqwestConnect = HttpConnect<ReqwestTransport>;

impl TransportConnect for ReqwestConnect {
type Transport = ReqwestTransport;

fn is_local(&self) -> bool {
guess_local_url(self.url.as_str())
}

fn get_transport<'a: 'b, 'b>(
&'a self,
) -> alloy_transport::Pbf<'b, Self::Transport, TransportError> {
Box::pin(async move { Ok(Http::with_client(Client::new(), self.url.clone())) })
}
}

impl Http<Client> {
/// Create a new [`Http`] transport.
pub fn new(url: Url) -> Self {
Expand Down
Loading