From ccfd73f32a3ae2d11e3fba0e8d702af237e91a7b Mon Sep 17 00:00:00 2001 From: mycognosist Date: Fri, 17 Dec 2021 16:20:47 +0200 Subject: [PATCH] add publish api helper functions --- src/api/helper.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/api/helper.rs b/src/api/helper.rs index 35896fd..74b3d21 100644 --- a/src/api/helper.rs +++ b/src/api/helper.rs @@ -1,4 +1,5 @@ use crate::{ + api::dto::content::TypedMessage, feed::Message, rpc::{Body, BodyType, RequestNo, RpcType, RpcWriter}, }; @@ -10,6 +11,7 @@ const MAX_RPC_BODY_LEN: usize = 65536; #[derive(Debug)] pub enum ApiMethod { + Publish, WhoAmI, Get, CreateHistoryStream, @@ -23,6 +25,7 @@ impl ApiMethod { pub fn selector(&self) -> &'static [&'static str] { use ApiMethod::*; match self { + Publish => &["publish"], WhoAmI => &["whoami"], Get => &["get"], CreateHistoryStream => &["createHistoryStream"], @@ -35,6 +38,7 @@ impl ApiMethod { pub fn from_selector(s: &[&str]) -> Option { use ApiMethod::*; match s { + ["publish"] => Some(Publish), ["whoami"] => Some(WhoAmI), ["get"] => Some(Get), ["createHistoryStream"] => Some(CreateHistoryStream), @@ -64,6 +68,23 @@ impl ApiCaller { &mut self.rpc } + /// Send ["publish"] request. + pub async fn publish_req_send(&mut self, msg: TypedMessage) -> Result { + let req_no = self + .rpc + .send_request(ApiMethod::Publish.selector(), RpcType::Async, &msg) + .await?; + Ok(req_no) + } + + /// Send ["publish"] response. + pub async fn publish_res_send(&mut self, req_no: RequestNo, msg_ref: String) -> Result<()> { + Ok(self + .rpc + .send_response(req_no, RpcType::Async, BodyType::JSON, msg_ref.as_bytes()) + .await?) + } + /// Send ["whoami"] request. pub async fn whoami_req_send(&mut self) -> Result { let args: [&str; 0] = [];