Skip to content
This repository has been archived by the owner on Oct 26, 2022. It is now read-only.

Commit

Permalink
Develop 0.2 (#18)
Browse files Browse the repository at this point in the history
Signed-off-by: koushiro <koushiro.cqx@gmail.com>
  • Loading branch information
koushiro committed Sep 22, 2021
1 parent 74ff011 commit a17c419
Show file tree
Hide file tree
Showing 27 changed files with 1,522 additions and 951 deletions.
1 change: 1 addition & 0 deletions .github/codecov.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
coverage:
status:
patch: off
project: off
21 changes: 17 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,26 @@ jobs:
command: fmt
args: --all -- --check

- name: Code Lint
- name: Code Lint With `tokio` Runtime
uses: actions-rs/cargo@v1
with:
command: clippy
args: --all-features --all-targets -- -D warnings

- name: Test
args: --no-default-features --features 'http-tokio,ws-tokio' --all-targets -- -D warnings

# - name: Code Lint With `async-std` Runtime
# uses: actions-rs/cargo@v1
# with:
# command: clippy
# args: --no-default-features --features 'http-async-std,ws-async-std' --all-targets -- -D warnings

- name: Test With `tokio` Runtime
uses: actions-rs/cargo@v1
with:
command: test
args: --no-default-features --features 'http-tokio,ws-tokio'

# - name: Test With `async-std` Runtime
# uses: actions-rs/cargo@v1
# with:
# command: test
# args: --no-default-features --features 'http-async-std,ws-async-std'
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/target
target
Cargo.lock
/.idea
.idea
71 changes: 45 additions & 26 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,55 +1,74 @@
[package]
name = "async-jsonrpc-client"
version = "0.1.3"
version = "0.2.0-dev"
authors = ["koushiro <koushiro.cqx@gmail.com>"]
edition = "2018"
description = "An asynchronous JSON-RPC client library, which supports HTTP and WebSocket"
readme = "README.md"
license = "MIT OR Apache-2.0"
license = "MIT/Apache-2.0"
documentation = "https://docs.rs/async-jsonrpc-client"
repository = "https://github.com/koushiro/async-jsonrpc-client-rs"
description = "An asynchronous JSON-RPC client library, which supports HTTP and WebSocket"
keywords = ["async", "jsonrpc", "client"]

[features]
default = ["http-reqwest", "ws"]
http-reqwest = ["reqwest"]
http-surf = ["surf", "anyhow"]
ws = ["async-tungstenite", "parking_lot", "tokio"]
default = ["http-tokio", "ws-tokio"]
# HTTP
http-tokio = ["tokio", "reqwest"]
http-async-std = ["async-std", "anyhow", "surf"]
# WebSocket
ws-tokio = ["tokio", "async-tungstenite/tokio-runtime", "async-tungstenite/tokio-native-tls"]
ws-async-std = ["async-std", "async-tungstenite/async-std-runtime", "async-tungstenite/async-native-tls"]

[dependencies]
async-trait = "0.1"
base64 = "0.13"
futures = "0.3"
log = "0.4"
jsonrpc-types = "0.3"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
thiserror = "1.0"

# HTTP (reqwest)
reqwest = { version = "0.10", features = ["json"], optional = true }

# HTTP (async-std)
surf = { version = "2.1", optional = true, default-features = false, features = ["h1-client"] }
# HTTP
reqwest = { version = "0.11", features = ["json"], optional = true }
anyhow = { version = "1.0", optional = true }
surf = { version = "2.1", default-features = false, features = ["h1-client"], optional = true }

# WebSocket
async-tungstenite = { version = "0.8", features = ["tokio-runtime"], optional = true }
parking_lot = { version = "0.11.1", optional = true }
tokio = { version = "0.2.0", features = ["macros"], optional = true }
async-tungstenite = { version = "0.12", optional = true }
parking_lot = { version = "0.11", optional = true }

# Runtime
async-std = { version = "1.9", optional = true }
tokio = { version = "1.1", optional = true }

[dev-dependencies]
env_logger = "0.8.2"
async-std = { version = "1.8.0", features = ["attributes"] }
env_logger = "0.8"
async-std = { version = "1.9", features = ["attributes"] }
hyper = { version = "0.14", features = ["server"] }
tokio = { version = "1.0", features = ["macros"] }

[[example]]
name = "http-tokio"
path = "examples/http_tokio.rs"
required-features = ["http-tokio"]

[[example]]
name = "http-tokio-v1"
path = "examples/http_tokio_v1.rs"
required-features = ["http-tokio"]

[[example]]
name = "http_reqwest"
path = "examples/http_reqwest.rs"
required-features = ["http-reqwest"]
name = "http-async-std"
path = "examples/http_async_std.rs"
required-features = ["http-async-std"]

[[example]]
name = "http_surf"
path = "examples/http_surf.rs"
required-features = ["http-surf"]
name = "ws-tokio"
path = "examples/ws_tokio.rs"
required-features = ["ws-tokio"]

[[example]]
name = "ws"
path = "examples/ws.rs"
required-features = ["ws"]
name = "ws-async-std"
path = "examples/ws_async_std.rs"
required-features = ["ws-async-std"]
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
[![docs-svg]][docs-url]
[![deps-svg]][deps-url]

[ga-svg]: https://github.com/koushiro/async-jsonrpc-client-rs/workflows/build/badge.svg
[ga-url]: https://github.com/koushiro/async-jsonrpc-client-rs/actions
[ga-svg]: https://github.com/koushiro/async-jsonrpc-client/workflows/build/badge.svg
[ga-url]: https://github.com/koushiro/async-jsonrpc-client/actions
[crates-svg]: https://img.shields.io/crates/v/async-jsonrpc-client
[crates-url]: https://crates.io/crates/async-jsonrpc-client
[docs-svg]: https://docs.rs/async-jsonrpc-client/badge.svg
[docs-url]: https://docs.rs/async-jsonrpc-client
[deps-svg]: https://deps.rs/repo/github/koushiro/async-jsonrpc-client-rs/status.svg
[deps-url]: https://deps.rs/repo/github/koushiro/async-jsonrpc-client-rs
[deps-svg]: https://deps.rs/repo/github/koushiro/async-jsonrpc-client/status.svg
[deps-url]: https://deps.rs/repo/github/koushiro/async-jsonrpc-client

An asynchronous JSON-RPC client library, written in Rust, which supports HTTP and WebSocket.

Expand All @@ -28,3 +28,9 @@ Licensed under either of
- [MIT License](LICENSE-MIT)

at your option.

## Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
dual licensed as above, without any additional terms or conditions.
4 changes: 4 additions & 0 deletions examples/http_surf.rs → examples/http_async_std.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/*
use async_jsonrpc_client::{HttpSurfTransport, Params, Transport, Value};
#[async_std::main]
Expand All @@ -22,3 +23,6 @@ async fn main() {
.unwrap();
println!("LogList: {:?}", log_list);
}
*/

fn main() {}
4 changes: 4 additions & 0 deletions examples/http_reqwest.rs → examples/http_tokio.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/*
use async_jsonrpc_client::{HttpReqwestTransport, Params, Transport, Value};
#[tokio::main]
Expand All @@ -22,3 +23,6 @@ async fn main() {
.unwrap();
println!("LogList: {:?}", log_list);
}
*/

fn main() {}
28 changes: 28 additions & 0 deletions examples/http_tokio_v1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
use async_jsonrpc_client::{HttpReqwestTransport, Params, Transport, Value};
#[tokio::main]
async fn main() {
env_logger::init();
let http = HttpReqwestTransport::new("http://127.0.0.1:1234/rpc/v0");
// Filecoin.Version need read permission
let version: Value = http
.send("Filecoin.Version", Params::Array(vec![]))
.await
.unwrap();
println!("Version: {:?}", version);
// lotus auth create-token --perm admin
let token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJBbGxvdyI6WyJyZWFkIiwid3JpdGUiLCJzaWduIiwiYWRtaW4iXX0.V82x4rrMmyzgLhW0jeBCL6FVN8I6iSnB0Dc05xeZjVE";
let http = HttpReqwestTransport::new_with_bearer_auth("http://127.0.0.1:1234/rpc/v0", token);
// Filecoin.LogList need write permission
let log_list: Value = http
.send("Filecoin.LogList", Params::Array(vec![]))
.await
.unwrap();
println!("LogList: {:?}", log_list);
}
*/

fn main() {}
4 changes: 4 additions & 0 deletions examples/ws.rs → examples/ws_async_std.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/*
use async_jsonrpc_client::{
Params, PubsubTransport, SubscriptionId, Transport, Value, WebSocketTransport,
};
Expand Down Expand Up @@ -36,3 +37,6 @@ async fn main() {
println!("Block: {:?}", value);
}
}
*/

fn main() {}
42 changes: 42 additions & 0 deletions examples/ws_tokio.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
use async_jsonrpc_client::{
Params, PubsubTransport, SubscriptionId, Transport, Value, WebSocketTransport,
};
use futures::stream::StreamExt;
#[tokio::main]
async fn main() {
env_logger::init();
let ws = WebSocketTransport::new("ws://127.0.0.1:1234/rpc/v0");
// Filecoin.Version need read permission
let version: Value = ws
.send("Filecoin.Version", Params::Array(vec![]))
.await
.unwrap();
println!("Version: {:?}", version);
// lotus auth create-token --perm admin
let token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJBbGxvdyI6WyJyZWFkIiwid3JpdGUiLCJzaWduIiwiYWRtaW4iXX0.zKxsT3HxoYdjy6A8cF1Q3qvJEbCxJb3PcAZ_CM-sf9I";
let ws = WebSocketTransport::new_with_bearer_auth("ws://127.0.0.1:1234/rpc/v0", token);
// Filecoin.LogList need write permission
let log_list: Value = ws
.send("Filecoin.LogList", Params::Array(vec![]))
.await
.unwrap();
println!("LogList: {:?}", log_list);
let ws = WebSocketTransport::new("ws://127.0.0.1:1234/rpc/v0");
let id: SubscriptionId = ws
.send("Filecoin.SyncIncomingBlocks", Params::Array(vec![]))
.await
.unwrap();
println!("Subscription Id: {}", id);
let mut stream = ws.subscribe::<Value>(id);
while let Some(value) = stream.next().await {
println!("Block: {:?}", value);
}
}
*/

fn main() {}
35 changes: 35 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use thiserror::Error;

pub(crate) type Result<T, E = RpcClientError> = std::result::Result<T, E>;

/// The error type for rpc client.
#[derive(Debug, Error)]
pub enum RpcClientError {
/// Json serialization/deserialization error.
#[error(transparent)]
Json(#[from] serde_json::Error),
#[cfg(feature = "http-tokio")]
/// HTTP request error.
#[error(transparent)]
HttpRequest(#[from] reqwest::Error),
#[cfg(feature = "http-async-std")]
/// HTTP request error.
#[error(transparent)]
HttpRequest(anyhow::Error),
/// HTTP connection error.
#[cfg(any(feature = "ws-tokio", feature = "ws-async-std"))]
#[error(transparent)]
HttpConnection(#[from] async_tungstenite::tungstenite::http::Error),
/// WebSocket protocol error.
#[cfg(any(feature = "ws-tokio", feature = "ws-async-std"))]
#[error(transparent)]
WebSocket(#[from] async_tungstenite::tungstenite::Error),
/// Internal task finished
#[cfg(any(feature = "ws-tokio", feature = "ws-async-std"))]
#[error("Cannot send request, internal task finished")]
InternalTaskFinish,

/// Rpc request error, return failure response.
#[error(transparent)]
RpcResponse(#[from] jsonrpc_types::Error),
}
25 changes: 0 additions & 25 deletions src/errors.rs

This file was deleted.

26 changes: 12 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@

#![deny(missing_docs)]

#[macro_use]
extern crate log;

mod errors;
mod error;
mod transports;
mod types;

pub use self::errors::{Result, RpcError};
#[cfg(feature = "http-reqwest")]
pub use self::transports::HttpReqwestTransport;
#[cfg(feature = "http-surf")]
pub use self::transports::HttpSurfTransport;
pub use self::transports::{BatchTransport, PubsubTransport, Transport};
#[cfg(feature = "ws")]
pub use self::transports::{NotificationStream, WebSocketTransport};
pub use self::types::*;
pub use self::error::RpcClientError;
pub use self::transports::{BatchTransport, NotificationStream, PubsubTransport, Transport};

//#[cfg(any(feature = "http-async-std", feature = "http-tokio"))]
#[cfg(feature = "http-tokio")]
pub use self::transports::{HttpTransport, HttpTransportBuilder};
// #[cfg(any(feature = "ws-async-std", feature = "ws-tokio"))]
#[cfg(feature = "ws-tokio")]
pub use self::transports::{WsTransport, WsTransportBuilder};

pub use jsonrpc_types::*;
Loading

0 comments on commit a17c419

Please sign in to comment.