Skip to content

Commit

Permalink
remove code from ws-client and use helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
haerdib committed Nov 8, 2023
1 parent 98df866 commit 4243aee
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub use jsonrpsee_client::JsonrpseeClient;
pub mod jsonrpsee_client;

pub mod error;
#[cfg(any(feature = "ws-client", feature = "tungstenite-client"))]
mod helpers;

pub use error::{Error, Result};
Expand Down
22 changes: 6 additions & 16 deletions src/rpc/ws_client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

use crate::rpc::Error as RpcClientError;
use crate::rpc::{helpers, Error as RpcClientError};
use log::*;
use std::{fmt::Debug, sync::mpsc::Sender as ThreadOut};
use ws::{CloseCode, Handler, Handshake, Message, Result as WsResult, Sender};
Expand Down Expand Up @@ -120,7 +120,7 @@ impl HandleMessage for SubscriptionHandler {
let send_result = match self.subscription_id.as_ref() {
Some(id) => handle_subscription_message(result, &value, id),
None => {
self.subscription_id = get_subscription_id(&value);
self.subscription_id = helpers::read_subscription_id(&value);
if self.subscription_id.is_none() {
send_error_response(result, &value, msg)
} else {
Expand All @@ -143,27 +143,17 @@ fn handle_subscription_message(
value: &serde_json::Value,
subscription_id: &str,
) -> Result<(), RpcClientError> {
if let Some(msg_subscription_id) = value["params"]["subscription"].as_str() {
if subscription_id == msg_subscription_id {
result.send(serde_json::to_string(&value["params"]["result"])?)?;
}
if helpers::subscription_id_matches(value, subscription_id) {
result.send(serde_json::to_string(&value["params"]["result"])?)?;
}
Ok(())
}

fn get_subscription_id(value: &serde_json::Value) -> Option<String> {
value["result"].as_str().map(|id| id.to_string())
}

fn send_error_response(
result: &ThreadOut<String>,
value: &serde_json::Value,
original_message: &str,
msg: &str,
) -> Result<(), RpcClientError> {
let message = match value["error"]["message"].is_string() {
true => serde_json::to_string(&value["error"])?,
false => format!("Received unexpected response: {}", original_message),
};
result.send(message)?;
result.send(helpers::read_error_message(value, msg))?;
Ok(())
}

0 comments on commit 4243aee

Please sign in to comment.