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

prepare for client release #215

Merged
merged 8 commits into from Mar 4, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions examples/Cargo.toml
Expand Up @@ -12,9 +12,9 @@ async-std = "1.9"
env_logger = "0.8"
futures = "0.3"
log = "0.4"
jsonrpsee-types = { path = "../types", version = "0.1" }
jsonrpsee-http-client = { path = "../http-client", version = "0.1" }
jsonrpsee-ws-client = { path = "../ws-client", version = "0.1" }
jsonrpsee-ws-server = { path = "../ws-server", version = "0.1" }
jsonrpsee-http-server = { path = "../http-server", version = "0.1" }
jsonrpsee-types = { path = "../types" }
jsonrpsee-http-client = { path = "../http-client" }
jsonrpsee-ws-client = { path = "../ws-client" }
jsonrpsee-ws-server = { path = "../ws-server" }
jsonrpsee-http-server = { path = "../http-server" }
tokio = { version = "1", features = ["full"] }
7 changes: 7 additions & 0 deletions http-client/CHANGELOG.md
@@ -0,0 +1,7 @@
# Changelog

The format is based on [Keep a Changelog].

[Keep a Changelog]: http://keepachangelog.com/en/1.0.0/

## [Unreleased]
6 changes: 3 additions & 3 deletions http-client/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "jsonrpsee-http-client"
version = "0.1.0"
version = "0.1.0-alpha"
authors = ["Parity Technologies <admin@parity.io>", "Pierre Krieger <pierre.krieger1708@gmail.com>"]
description = "HTTP client for JSON-RPC"
edition = "2018"
Expand All @@ -10,8 +10,8 @@ license = "MIT"
futures = "0.3"
hyper14 = { package = "hyper", version = "0.14", features = ["client", "http1", "http2", "tcp"], optional = true }
hyper13 = { package = "hyper", version = "0.13", optional = true }
jsonrpsee-types = { path = "../types", version = "0.1" }
jsonrpsee-utils = { path = "../utils", version = "0.1", default-features = false, optional = true }
jsonrpsee-types = { path = "../types", version = "0.1.0-alpha" }
jsonrpsee-utils = { path = "../utils", version = "0.1.0-alpha", default-features = false, optional = true }
log = "0.4"
serde = { version = "1.0", default-features = false, features = ["derive"] }
serde_json = "1.0"
Expand Down
7 changes: 1 addition & 6 deletions http-client/src/client.rs
Expand Up @@ -10,8 +10,7 @@ use std::convert::TryInto;
use std::sync::atomic::{AtomicU64, Ordering};

/// JSON-RPC HTTP Client that provides functionality to perform method calls and notifications.
///
/// WARNING: The async methods must be executed on [Tokio 1.0](https://docs.rs/tokio/1.0.1/tokio).
#[derive(Debug)]
pub struct HttpClient {
/// HTTP transport client.
transport: HttpTransportClient,
Expand All @@ -29,8 +28,6 @@ impl HttpClient {
}

/// Send a notification to the server.
///
/// WARNING: This method must be executed on [Tokio 1.0](https://docs.rs/tokio/1.0.1/tokio).
pub async fn notification(
&self,
method: impl Into<String>,
Expand All @@ -46,8 +43,6 @@ impl HttpClient {
}

/// Perform a request towards the server.
///
/// WARNING: This method must be executed on [Tokio 1.0](https://docs.rs/tokio/1.0.1/tokio).
pub async fn request<Ret>(
&self,
method: impl Into<String>,
Expand Down
22 changes: 22 additions & 0 deletions http-client/src/lib.rs
@@ -1,3 +1,25 @@
#![warn(missing_debug_implementations, missing_docs, rust_2018_idioms, unreachable_pub)]

//! # jsonrpsee-http-client
//!
//! `jsonrpsee-http-client` is [JSON RPC](https://www.jsonrpc.org/specification) HTTP client library that's is built for `async/await`.
//!
//! It is tightly-coupled to [`tokio`](https://docs.rs/tokio) because [`hyper`](https://docs.rs/hyper) is used as transport client,
//! which is not compatible with other async runtimes such as
//! [`async-std`](https://docs.rs/async-std/), [`smol`](https://docs.rs/smol) and similar.
//!
//! It supports both [`tokio 1.0`](https://docs.rs/tokio/1.2.0/tokio/) and [`tokio 0.2`](https://docs.rs/tokio/0.2.25/tokio/index.html)
//! via [Optional features](#optional-features).
//!
//! # Optional Features
//!
//! `jsonrpsee-http-client` uses the following [feature flags]:
//!
//! - `tokio1`: Enable to work with [`tokio 1.0`](https://docs.rs/tokio/1.2.0/tokio/) (mutual exclusive to `tokio02`)
//! - `tokio0.2`: Enable to work with [`tokio 0.2`](https://docs.rs/tokio/0.2.25/tokio/index.html) (mutual exclusive to `tokio1`)
niklasad1 marked this conversation as resolved.
Show resolved Hide resolved
//!
//! [feature flags]: https://doc.rust-lang.org/cargo/reference/manifest.html#the-features-section

#[cfg(all(feature = "tokio1", feature = "tokio02"))]
compile_error!("feature `tokio1` and `tokio02` are mutably exclusive");

Expand Down
7 changes: 7 additions & 0 deletions http-server/CHANGELOG.md
@@ -0,0 +1,7 @@
# Changelog

The format is based on [Keep a Changelog].

[Keep a Changelog]: http://keepachangelog.com/en/1.0.0/

## [Unreleased]
6 changes: 3 additions & 3 deletions http-server/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "jsonrpsee-http-server"
version = "0.1.0"
version = "0.1.0-alpha"
authors = ["Parity Technologies <admin@parity.io>", "Pierre Krieger <pierre.krieger1708@gmail.com>"]
description = "HTTP server for JSON-RPC"
edition = "2018"
Expand All @@ -11,8 +11,8 @@ async-std = "1.8"
futures = "0.3"
fnv = "1"
hyper = { version = "0.14", features = ["stream", "client", "server", "http1", "http2", "tcp"] }
jsonrpsee-types = { path = "../types", version = "0.1" }
jsonrpsee-utils = { path = "../utils", version = "0.1" }
jsonrpsee-types = { path = "../types", version = "0.1.0-alpha" }
jsonrpsee-utils = { path = "../utils", version = "0.1.0-alpha" }
log = "0.4"
serde = { version = "1", default-features = false, features = ["derive"] }
serde_json = "1"
Expand Down
2 changes: 1 addition & 1 deletion test-utils/src/types.rs
Expand Up @@ -50,7 +50,7 @@ impl WebSocketTestClient {
let (tx, rx) = client.into_builder().finish();
Ok(Self { tx, rx })
}
r @ _ => Err(format!("WebSocketHandshake failed: {:?}", r).into()),
r => Err(format!("WebSocketHandshake failed: {:?}", r).into()),
}
}

Expand Down
7 changes: 7 additions & 0 deletions types/CHANGELOG.md
@@ -0,0 +1,7 @@
# Changelog
niklasad1 marked this conversation as resolved.
Show resolved Hide resolved

The format is based on [Keep a Changelog].

[Keep a Changelog]: http://keepachangelog.com/en/1.0.0/

## [Unreleased]
2 changes: 1 addition & 1 deletion types/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "jsonrpsee-types"
version = "0.1.0"
version = "0.1.0-alpha"
authors = ["Parity Technologies <admin@parity.io>"]
description = "Shared types for jsonrpsee"
edition = "2018"
Expand Down
4 changes: 2 additions & 2 deletions utils/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "jsonrpsee-utils"
version = "0.1.0"
version = "0.1.0-alpha"
authors = ["Parity Technologies <admin@parity.io>"]
description = "Utilities for jsonrpsee"
edition = "2018"
Expand All @@ -11,7 +11,7 @@ futures = "0.3"
globset = "0.4"
hyper13 = { package = "hyper", version = "0.13", default-features = false, features = ["stream"], optional = true }
hyper14 = { package = "hyper", version = "0.14", default-features = false, features = ["stream"], optional = true }
jsonrpsee-types = { path = "../types", version = "0.1" }
jsonrpsee-types = { path = "../types", version = "0.1.0-alpha" }
lazy_static = "1.4"
log = "0.4"
unicase = "2.6"
Expand Down
7 changes: 7 additions & 0 deletions ws-client/CHANGELOG.md
@@ -0,0 +1,7 @@
# Changelog

The format is based on [Keep a Changelog].

[Keep a Changelog]: http://keepachangelog.com/en/1.0.0/

## [Unreleased]
4 changes: 2 additions & 2 deletions ws-client/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "jsonrpsee-ws-client"
version = "0.1.0"
version = "0.1.0-alpha"
authors = ["Parity Technologies <admin@parity.io>", "Pierre Krieger <pierre.krieger1708@gmail.com>"]
description = "WebSocket client for JSON-RPC"
edition = "2018"
Expand All @@ -11,7 +11,7 @@ async-std = { version = "1.8", features = ["attributes"] }
async-tls = "0.11"
fnv = "1"
futures = "0.3"
jsonrpsee-types = { path = "../types", version = "0.1" }
jsonrpsee-types = { path = "../types", version = "0.1.0-alpha" }
log = "0.4"
serde = { version = "1", default-features = false, features = ["derive"] }
serde_json = "1"
Expand Down
7 changes: 4 additions & 3 deletions ws-client/src/client.rs
Expand Up @@ -42,7 +42,7 @@ use std::{io, marker::PhantomData};
/// Client that can be cloned.
///
/// > **Note**: This struct is designed to be easy to use, but it works by maintaining a background task running in parallel.
#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct WsClient {
/// Channel to send requests to the background task.
to_back: mpsc::Sender<FrontToBack>,
Expand Down Expand Up @@ -98,7 +98,7 @@ impl<'a> WsConfig<'a> {
}
}
}

#[derive(Debug)]
/// Active subscription on a [`WsClient`].
pub struct WsSubscription<Notif> {
/// Channel to send requests to the background task.
Expand All @@ -111,7 +111,8 @@ pub struct WsSubscription<Notif> {
marker: PhantomData<Notif>,
}

/// Message that the [`Client`] can send to the background task.
#[derive(Debug)]
/// Message that the [`WsClient`] can send to the background task.
enum FrontToBack {
/// Send a one-shot notification to the server. The server doesn't give back any feedback.
Notification {
Expand Down
2 changes: 2 additions & 0 deletions ws-client/src/jsonrpc_transport.rs
Expand Up @@ -14,6 +14,7 @@ pub async fn websocket_connection(config: WsConfig<'_>) -> Result<(Sender, Recei

/// JSONRPC WebSocket sender.
/// It's a wrapper over `WebSocket sender` with additional `JSONRPC request_id`.
#[derive(Debug)]
pub struct Sender {
request_id: u64,
transport: transport::Sender,
Expand Down Expand Up @@ -91,6 +92,7 @@ impl Sender {
}

/// JSONRPC WebSocket receiver.
#[derive(Debug)]
pub struct Receiver {
transport: transport::Receiver,
}
Expand Down
6 changes: 6 additions & 0 deletions ws-client/src/lib.rs
@@ -1,3 +1,9 @@
#![warn(missing_debug_implementations, missing_docs, rust_2018_idioms, unreachable_pub)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should agree on a standard here with @maciejhirsz too. I suggest we make missing_docs and unreachable_pub errors.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy with missing_docs and unreachable_pub, rust_2018_idioms seems overly pendantic with elided lifetimes (I'd rather have Cow<str> than Cow<'_, str>, etc.).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should keep it as warn because deny/forbid would force it stop compile on new toolchains as rustc gets smarter :)

forbid(warnings) in substrate is an example of this :)


//! # jsonrpsee-ws-client
//!
//! `jsonrpsee-ws-client` is a [JSON RPC](https://www.jsonrpc.org/specification) WebSocket client library that's is built for `async/await`.

/// WebSocket Client.
pub mod client;
/// JSONRPC WebSocket transport.
Expand Down
6 changes: 5 additions & 1 deletion ws-client/src/manager.rs
Expand Up @@ -14,12 +14,14 @@ use jsonrpsee_types::{
};
use std::collections::hash_map::{Entry, HashMap};

#[derive(Debug)]
enum Kind {
PendingMethodCall(PendingCallOneshot),
PendingSubscription((PendingSubscriptionOneshot, UnsubscribeMethod)),
Subscription((SubscriptionSink, UnsubscribeMethod)),
}

#[derive(Debug)]
/// Indicates the status of a given request/response.
pub enum RequestStatus {
/// The method call is waiting for a response,
Expand All @@ -38,6 +40,7 @@ type SubscriptionSink = mpsc::Sender<JsonValue>;
type UnsubscribeMethod = String;
type RequestId = u64;

#[derive(Debug, Default)]
/// Manages and monitors JSONRPC v2 method calls and subscriptions.
pub struct RequestManager {
/// List of requests that are waiting for a response from the server.
Expand All @@ -48,8 +51,9 @@ pub struct RequestManager {
}

impl RequestManager {
/// Create an empty `RequestManager`.
pub fn new() -> Self {
Self { requests: FnvHashMap::default(), subscriptions: HashMap::new() }
Self::default()
}

/// Tries to insert a new pending call.
Expand Down
19 changes: 12 additions & 7 deletions ws-client/src/stream.rs
Expand Up @@ -33,6 +33,7 @@ use futures::{
use pin_project::pin_project;
use std::{io::Error as IoError, pin::Pin, task::Context, task::Poll};

/// Stream to represent either a unencrypted or encrypted socket stream.
#[pin_project(project = EitherStreamProj)]
#[derive(Debug, Copy, Clone)]
pub enum EitherStream<S, T> {
Expand All @@ -47,7 +48,7 @@ where
S: AsyncRead,
T: AsyncRead,
{
fn poll_read(self: Pin<&mut Self>, cx: &mut Context, buf: &mut [u8]) -> Poll<Result<usize, IoError>> {
fn poll_read(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8]) -> Poll<Result<usize, IoError>> {
match self.project() {
EitherStreamProj::Plain(s) => AsyncRead::poll_read(s, cx, buf),
EitherStreamProj::Tls(t) => AsyncRead::poll_read(t, cx, buf),
Expand All @@ -56,8 +57,8 @@ where

fn poll_read_vectored(
self: Pin<&mut Self>,
cx: &mut Context,
bufs: &mut [IoSliceMut],
cx: &mut Context<'_>,
bufs: &mut [IoSliceMut<'_>],
) -> Poll<Result<usize, IoError>> {
match self.project() {
EitherStreamProj::Plain(s) => AsyncRead::poll_read_vectored(s, cx, bufs),
Expand All @@ -71,28 +72,32 @@ where
S: AsyncWrite,
T: AsyncWrite,
{
fn poll_write(self: Pin<&mut Self>, cx: &mut Context, buf: &[u8]) -> Poll<Result<usize, IoError>> {
fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize, IoError>> {
match self.project() {
EitherStreamProj::Plain(s) => AsyncWrite::poll_write(s, cx, buf),
EitherStreamProj::Tls(t) => AsyncWrite::poll_write(t, cx, buf),
}
}

fn poll_write_vectored(self: Pin<&mut Self>, cx: &mut Context, bufs: &[IoSlice]) -> Poll<Result<usize, IoError>> {
fn poll_write_vectored(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &[IoSlice<'_>],
) -> Poll<Result<usize, IoError>> {
match self.project() {
EitherStreamProj::Plain(s) => AsyncWrite::poll_write_vectored(s, cx, bufs),
EitherStreamProj::Tls(t) => AsyncWrite::poll_write_vectored(t, cx, bufs),
}
}

fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), IoError>> {
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), IoError>> {
match self.project() {
EitherStreamProj::Plain(s) => AsyncWrite::poll_flush(s, cx),
EitherStreamProj::Tls(t) => AsyncWrite::poll_flush(t, cx),
}
}

fn poll_close(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), IoError>> {
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), IoError>> {
match self.project() {
EitherStreamProj::Plain(s) => AsyncWrite::poll_close(s, cx),
EitherStreamProj::Tls(t) => AsyncWrite::poll_close(t, cx),
Expand Down
3 changes: 3 additions & 0 deletions ws-client/src/transport.rs
Expand Up @@ -38,16 +38,19 @@ use thiserror::Error;
type TlsOrPlain = crate::stream::EitherStream<TcpStream, TlsStream<TcpStream>>;

/// Sending end of WebSocket transport.
#[derive(Debug)]
pub struct Sender {
inner: connection::Sender<BufReader<BufWriter<TlsOrPlain>>>,
}

/// Receiving end of WebSocket transport.
#[derive(Debug)]
pub struct Receiver {
inner: connection::Receiver<BufReader<BufWriter<TlsOrPlain>>>,
}

/// Builder for a WebSocket transport [`Sender`] and ['Receiver`] pair.
#[derive(Debug)]
pub struct WsTransportClientBuilder<'a> {
/// IP address to try to connect to.
target: SocketAddr,
Expand Down
7 changes: 7 additions & 0 deletions ws-server/CHANGELOG.md
@@ -0,0 +1,7 @@
# Changelog

The format is based on [Keep a Changelog].

[Keep a Changelog]: http://keepachangelog.com/en/1.0.0/

## [Unreleased]
4 changes: 2 additions & 2 deletions ws-server/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "jsonrpsee-ws-server"
version = "0.1.0"
version = "0.1.0-alpha"
authors = ["Parity Technologies <admin@parity.io>", "Pierre Krieger <pierre.krieger1708@gmail.com>"]
description = "WebSocket server for JSON-RPC"
edition = "2018"
Expand All @@ -11,7 +11,7 @@ async-std = { version = "1.8.0", features = ["attributes"] }
bs58 = "0.4"
fnv = "1"
futures = "0.3"
jsonrpsee-types = { path = "../types", version = "0.1" }
jsonrpsee-types = { path = "../types", version = "0.1.0-alpha" }
log = "0.4"
parking_lot = "0.11"
rand = "0.8"
Expand Down