Skip to content

Commit

Permalink
enchancement: update the core to reqwest (#68)
Browse files Browse the repository at this point in the history
* enchancement: update the core to reqwest

* chore: uncomment pallet communities
  • Loading branch information
S0c5 authored Jul 26, 2024
1 parent 523248b commit f4d963a
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 33 deletions.
17 changes: 7 additions & 10 deletions sube/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ twox-hash = { version = "1.6.2", default-features = false }
url = "2.5.0"

# http backend
surf = { version = "2.3.2", default-features = false, optional = true }
reqwest = { version = "0.12.5", optional = true, features = ["json"]}

# ws backend
futures-channel = { version = "0.3.21", default-features = false, features = ["alloc"], optional = true }
Expand All @@ -33,8 +33,8 @@ async-mutex = { version = "1.4.0", optional = true }
async-tls = { version = "0.11.0", default-features = false, optional = true }

# bin target
async-std = { version = "1.11.0", default-features = false, optional = true }
paste = { version = "1.0", optional = true }
async-std = { version = "1.11.0", optional = true }
paste = { version = "1.0" }
wasm-bindgen = { version = "0.2.91", optional = true }
once_cell = { version = "1.17.1", optional = true }
heapless = { version = "0.7.16", optional = true }
Expand All @@ -44,16 +44,16 @@ ewebsock = { git = "https://github.com/S0c5/ewebsock.git", optional = true, bran
env_logger = "0.11.3"

[dev-dependencies]
async-std = { version = "1.11.0", features = ["attributes"] }
async-std = { version = "1.11.0", features = ["attributes", "tokio1"] }
hex-literal = "0.3.4"
libwallet = { path = "../libwallet", default-features=false, features=["substrate", "mnemonic", "sr25519", "util_pin", "rand", "std" ] }
rand_core = "0.6.3"

[features]
default = ["v14"]
test = ["std", "wss", "http", "json", "v14", "dep:async-std", "dep:rand_core"]
http = ["dep:jsonrpc", "surf/h1-client-rustls"]
http-web = ["dep:jsonrpc", "dep:wasm-bindgen", "surf/wasm-client"]
http = ["dep:jsonrpc", "dep:reqwest"]
http-web = ["dep:jsonrpc", "dep:wasm-bindgen", "dep:reqwest"]
json = ["scales/json"]
std = []
no_std = []
Expand All @@ -71,7 +71,4 @@ features = ["http"]
members = [
"sube-js",
"cli"
]

[patch.crates-io]
cookie = { git = "https://github.com/S0c5/cookie-rs.git" }
]
6 changes: 2 additions & 4 deletions sube/examples/pallet_communities.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use async_trait::async_trait;

use env_logger;
use serde_json;
use sube::{sube, ExtrinsicBody, Response, Result, SubeBuilder};
Expand All @@ -10,12 +8,12 @@ async fn main() -> Result<()> {

let response = sube!("ws://127.0.0.1:12281/communityMemberships/collection").await?;

if let Response::ValueSet(value) = result {
if let Response::ValueSet(value) = response {
let data = serde_json::to_value(&value).expect("to be serializable");
println!("Collection {}", serde_json::to_string_pretty(&data).expect("it must return an str"));
}

let result = sube!("ws://127.0.0.1:12281/system/account/0x12840f0626ac847d41089c4e05cf0719c5698af1e3bb87b66542de70b2de4b2b").await?;
let result = sube!("https://kreivo.io/system/account/0x12840f0626ac847d41089c4e05cf0719c5698af1e3bb87b66542de70b2de4b2b").await?;

if let Response::Value(value) = result {
let data = serde_json::to_value(&value).expect("to be serializable");
Expand Down
1 change: 0 additions & 1 deletion sube/examples/query_balance.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use async_trait::async_trait;
use core::future::{Future, IntoFuture};
use sube::{sube, Response, Result};

Expand Down
2 changes: 0 additions & 2 deletions sube/examples/query_balance_builder.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use async_trait::async_trait;

use env_logger;
use sube::{sube, ExtrinsicBody, Response, Result, SubeBuilder};

Expand Down
29 changes: 14 additions & 15 deletions sube/src/http.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::prelude::*;
use crate::rpc::{self, Rpc, RpcResult};
use core::{convert::TryInto, fmt};
use jsonrpc::{
error::{standard_error, StandardError},
serde_json::{to_string, value::to_raw_value},
serde_json::value::to_raw_value,
};
use reqwest::Client;
use serde::Deserialize;
pub use surf::Url;

use crate::rpc::{self, Rpc, RpcResult};
use url::Url;

#[derive(Debug)]
pub struct Backend(Url);
Expand All @@ -29,24 +29,22 @@ impl Rpc for Backend {
T: for<'de> Deserialize<'de>,
{
log::info!("RPC `{}` to {}", method, &self.0);
let req = surf::post(&self.0).content_type("application/json").body(
to_string(&rpc::Request {

let res = Client::new()
.post(self.0.to_string())
.json(&rpc::Request {
id: 1.into(),
jsonrpc: Some("2.0"),
method,
params: &Self::convert_params(params),
})
.unwrap(),
);
let client = surf::client().with(surf::middleware::Redirect::new(2));
let mut res = client
.send(req)
.send()
.await
.map_err(|err| rpc::Error::Transport(err.into_inner().into()))?;
.map_err(|err| rpc::Error::Transport(Box::new(err)))?;

let status = res.status();
let res = if status.is_success() {
res.body_json::<rpc::Response>()
res.json::<rpc::Response>()
.await
.map_err(|err| {
standard_error(
Expand All @@ -58,9 +56,10 @@ impl Rpc for Backend {
} else {
log::debug!("RPC HTTP status: {}", res.status());
let err = res
.body_string()
.text()
.await
.unwrap_or_else(|_| status.canonical_reason().into());
.unwrap_or_else(|_| status.canonical_reason().expect("to have a message").into());

let err = to_raw_value(&err).expect("error string");

return Err(if status.is_client_error() {
Expand Down
2 changes: 1 addition & 1 deletion sube/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl<R: Rpc> Backend for RpcClient<R> {
log::debug!("Extrinsic: {}", extrinsic);

self.0
.rpc("author_submitExtrinsic", &[&format!("\"{}\"", &extrinsic)])
.rpc::<serde_json::Value>("author_submitExtrinsic", &[&format!("\"{}\"", &extrinsic)])
.await
.map_err(|e| crate::Error::Node(e.to_string()))?;
Ok(())
Expand Down

0 comments on commit f4d963a

Please sign in to comment.