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

Implement SubscriptionClient for HttpClient #563

Merged
merged 1 commit into from
Nov 17, 2021
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
28 changes: 26 additions & 2 deletions http-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@

use crate::transport::HttpTransportClient;
use crate::types::{
traits::Client,
traits::{Client, SubscriptionClient},
v2::{Id, NotificationSer, ParamsSer, RequestSer, Response, RpcError},
CertificateStore, Error, RequestIdManager, TEN_MB_SIZE_BYTES,
CertificateStore, Error, RequestIdManager, Subscription, TEN_MB_SIZE_BYTES,
};
use async_trait::async_trait;
use fnv::FnvHashMap;
Expand Down Expand Up @@ -194,3 +194,27 @@ impl Client for HttpClient {
Ok(responses)
}
}

#[async_trait]
impl SubscriptionClient for HttpClient {
/// Send a subscription request to the server. Not implemented for HTTP; will always return [`Error::HttpNotImplemented`].
async fn subscribe<'a, N>(
&self,
_subscribe_method: &'a str,
_params: Option<ParamsSer<'a>>,
_unsubscribe_method: &'a str,
) -> Result<Subscription<N>, Error>
where
N: DeserializeOwned,
{
Err(Error::HttpNotImplemented)
}

/// Subscribe to a specific method. Not implemented for HTTP; will always return [`Error::HttpNotImplemented`].
async fn subscribe_to_method<'a, N>(&self, _method: &'a str) -> Result<Subscription<N>, Error>
where
N: DeserializeOwned,
{
Err(Error::HttpNotImplemented)
}
}
21 changes: 20 additions & 1 deletion tests/tests/proc_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@

use std::net::SocketAddr;

use jsonrpsee::{ws_client::*, ws_server::WsServerBuilder};
use jsonrpsee::{
http_client::HttpClientBuilder, http_server::HttpServerBuilder, types::Error, ws_client::*,
ws_server::WsServerBuilder,
};

use serde_json::value::RawValue;

mod rpc_impl {
Expand Down Expand Up @@ -305,3 +309,18 @@ async fn multiple_blocking_calls_overlap() {
// Each request takes 50ms, added 10ms margin for scheduling
assert!(elapsed < Duration::from_millis(60), "Expected less than 60ms, got {:?}", elapsed);
}

#[tokio::test]
async fn subscriptions_do_not_work_for_http_servers() {
let htserver = HttpServerBuilder::default().build("127.0.0.1:0".parse().unwrap()).unwrap();
let addr = htserver.local_addr().unwrap();
let htserver_url = format!("http://{}", addr);
let _handle = htserver.start(RpcServerImpl.into_rpc()).unwrap();

let htclient = HttpClientBuilder::default().build(&htserver_url).unwrap();

assert_eq!(htclient.sync_method().await.unwrap(), 10);
assert!(htclient.sub().await.is_err());
assert!(matches!(htclient.sub().await, Err(Error::HttpNotImplemented)));
assert_eq!(htclient.sync_method().await.unwrap(), 10);
}
3 changes: 3 additions & 0 deletions types/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ pub enum Error {
/// Custom error.
#[error("Custom error: {0}")]
Custom(String),
/// Not implemented for HTTP clients.
#[error("Not implemented")]
HttpNotImplemented,
}

impl Error {
Expand Down