/// Sends a POST request with the provided method and the params serialized as JSON
/// over WebSockets
Sends a POST request with the provided method and the params serialized as JSON
over WebSockets
Sends a POST request with the provided method and the params serialized as JSON over WebSockets
async fn request<T: Serialize + Send + Sync, R: for<'a> Deserialize<'a>>(
        &self,
        method: &str,
        params: T,
    ) -> Result<R, ClientError> {
        // we get a lock on the websocket to avoid race conditions with multiple borrows
        let mut lock = self.ws.lock().await;
        let next_id = self.id.load(Ordering::SeqCst) + 1;
        self.id.store(next_id, Ordering::SeqCst);
        // send the message
        let payload = serde_json::to_string(&Request::new(next_id, method, params))?;
        lock.send(Message::text(payload)).await?;
        // get the response bytes
        let resp = lock.next().await.ok_or(ClientError::NoResponse)??;
        let data: ResponseData<R> = match resp {
            Message::Text(inner) => serde_json::from_str(&inner)?,
            Message::Binary(inner) => serde_json::from_slice(&inner)?,
            // TODO: Should we do something if we receive a Ping, Pong or Close?
            _ => return Err(ClientError::NoResponse),
        };
        Ok(data.into_result()?)
    }
async fn request < T : Serialize + Send + Sync , R : for < ' a > Deserialize < ' a > > ( & self , method : & str , params : T , ) -> Result < R , ClientError > { // we get a lock on the websocket to avoid race conditions with multiple borrows let mut lock = self . ws . lock ( ) . await ; let next_id = self . id . load ( Ordering :: SeqCst ) + 1 ; self . id . store ( next_id , Ordering :: SeqCst ) ; // send the message let payload = serde_json :: to_string ( & Request :: new ( next_id , method , params ) ) ? ; lock . send ( Message :: text ( payload ) ) . await ? ; // get the response bytes let resp = lock . next ( ) . await . ok_or ( ClientError :: NoResponse ) ? ? ; let data : ResponseData < R > = match resp { Message :: Text ( inner ) => serde_json :: from_str ( & inner ) ? , Message :: Binary ( inner ) => serde_json :: from_slice ( & inner ) ? , // TODO: Should we do something if we receive a Ping, Pong or Close? _ => return Err ( ClientError :: NoResponse ) , } ; Ok ( data . into_result ( ) ? ) }
Sends a POST request with the provided method and the params serialized as JSON
over WebSockets
Sends a POST request with the provided method and the params serialized as JSON over WebSockets
&self
method
&str
params
T