wip
This commit is contained in:
parent
94c7851080
commit
a029097028
|
@ -17,9 +17,8 @@ async-std = { version = "1.1.0", features=["unstable","attributes"] }
|
||||||
crossbeam = "0.7.3"
|
crossbeam = "0.7.3"
|
||||||
log = "0.4.8"
|
log = "0.4.8"
|
||||||
env_logger = "0.7.1"
|
env_logger = "0.7.1"
|
||||||
serde = "1.0.104"
|
serde = { version = "1.0.104", features = ["derive"] }
|
||||||
serde_json = { version = "1.0.41", features=["preserve_order","arbitrary_precision"] }
|
serde_json = { version = "1.0.41", features=["preserve_order","arbitrary_precision"] }
|
||||||
serde_derive = "1.0.104"
|
|
||||||
dirs = "2.0"
|
dirs = "2.0"
|
||||||
futures = "0.3.1"
|
futures = "0.3.1"
|
||||||
snap = "0.2.5"
|
snap = "0.2.5"
|
||||||
|
|
|
@ -69,7 +69,7 @@ async fn main() -> AnyResult<()> {
|
||||||
println!("💃 handshake complete");
|
println!("💃 handshake complete");
|
||||||
|
|
||||||
let (box_stream_read, box_stream_write) =
|
let (box_stream_read, box_stream_write) =
|
||||||
BoxStream::from_handhake(&mut socket, handshake, 0x8000)
|
BoxStream::from_handshake(&socket,&socket,handshake, 0x8000)
|
||||||
.split_read_write();
|
.split_read_write();
|
||||||
|
|
||||||
let mut client = ApiClient::new(RpcClient::new(box_stream_read, box_stream_write));
|
let mut client = ApiClient::new(RpcClient::new(box_stream_read, box_stream_write));
|
||||||
|
|
|
@ -2,7 +2,7 @@ use async_std::io::{Read, Write};
|
||||||
use serde_json;
|
use serde_json;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
use crate::rpc::{RpcClient, Header, RequestNo, RpcType};
|
use crate::rpc::{RpcClient, Header, RequestNo, RpcType, Body};
|
||||||
use crate::feed::Message;
|
use crate::feed::Message;
|
||||||
use crate::feed::Feed;
|
use crate::feed::Feed;
|
||||||
|
|
||||||
|
@ -273,15 +273,24 @@ impl<R: Read + Unpin, W: Write + Unpin> ApiClient<R, W> {
|
||||||
// whoami: sync
|
// whoami: sync
|
||||||
// Get information about the current ssb-server user.
|
// Get information about the current ssb-server user.
|
||||||
pub async fn send_whoami(&mut self) -> Result<RequestNo> {
|
pub async fn send_whoami(&mut self) -> Result<RequestNo> {
|
||||||
let args: [&str; 0] = [];
|
let body = Body {
|
||||||
let req_no = self.rpc.send(&["whoami"], RpcType::Async, &args).await?;
|
name : vec!["whoami".to_string()],
|
||||||
|
rpc_type : RpcType::Async,
|
||||||
|
args : Vec::<String>::new()
|
||||||
|
};
|
||||||
|
let req_no = self.rpc.send(&body).await?;
|
||||||
Ok(req_no)
|
Ok(req_no)
|
||||||
}
|
}
|
||||||
|
|
||||||
// get: async
|
// get: async
|
||||||
// Get a message by its hash-id. (sould start with %)
|
// Get a message by its hash-id. (sould start with %)
|
||||||
pub async fn send_get(&mut self, msg_id: &str) -> Result<RequestNo> {
|
pub async fn send_get(&mut self, msg_id: &str) -> Result<RequestNo> {
|
||||||
let req_no = self.rpc.send(&["get"], RpcType::Async, &msg_id).await?;
|
let body = Body {
|
||||||
|
name : vec!["get".to_string()],
|
||||||
|
rpc_type : RpcType::Async,
|
||||||
|
args : vec![msg_id.to_string()]
|
||||||
|
};
|
||||||
|
let req_no = self.rpc.send(&body).await?;
|
||||||
Ok(req_no)
|
Ok(req_no)
|
||||||
}
|
}
|
||||||
// createHistoryStream: source
|
// createHistoryStream: source
|
||||||
|
@ -290,10 +299,12 @@ impl<R: Read + Unpin, W: Write + Unpin> ApiClient<R, W> {
|
||||||
&mut self,
|
&mut self,
|
||||||
args: &'a CreateHistoryStreamArgs<'a>,
|
args: &'a CreateHistoryStreamArgs<'a>,
|
||||||
) -> Result<RequestNo> {
|
) -> Result<RequestNo> {
|
||||||
let req_no = self
|
let body = Body {
|
||||||
.rpc
|
name : vec!["createHistoryStream".to_string()],
|
||||||
.send(&["createHistoryStream"], RpcType::Source, &args)
|
rpc_type : RpcType::Source,
|
||||||
.await?;
|
args : args
|
||||||
|
};
|
||||||
|
let req_no = self.rpc.send(&body).await?;
|
||||||
Ok(req_no)
|
Ok(req_no)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -303,18 +314,24 @@ impl<R: Read + Unpin, W: Write + Unpin> ApiClient<R, W> {
|
||||||
&mut self,
|
&mut self,
|
||||||
args: &'a CreateStreamArgs<u64>,
|
args: &'a CreateStreamArgs<u64>,
|
||||||
) -> Result<RequestNo> {
|
) -> Result<RequestNo> {
|
||||||
let req_no = self
|
let body = Body {
|
||||||
.rpc
|
name : vec!["createFeedStream".to_string()],
|
||||||
.send(&["createFeedStream"], RpcType::Source, &args)
|
rpc_type : RpcType::Source,
|
||||||
.await?;
|
args
|
||||||
|
};
|
||||||
|
let req_no = self.rpc.send(&body).await?;
|
||||||
Ok(req_no)
|
Ok(req_no)
|
||||||
}
|
}
|
||||||
|
|
||||||
// latest: source
|
// latest: source
|
||||||
// Get the seq numbers of the latest messages of all users in the database.
|
// Get the seq numbers of the latest messages of all users in the database.
|
||||||
pub async fn send_latest(&mut self) -> Result<RequestNo> {
|
pub async fn send_latest(&mut self) -> Result<RequestNo> {
|
||||||
let args: [&str; 0] = [];
|
let body = Body {
|
||||||
let req_no = self.rpc.send(&["latest"], RpcType::Source, &args).await?;
|
name : vec!["latest".to_string()],
|
||||||
|
rpc_type : RpcType::Source,
|
||||||
|
args : Vec::<String>::new(),
|
||||||
|
};
|
||||||
|
let req_no = self.rpc.send(&body).await?;
|
||||||
Ok(req_no)
|
Ok(req_no)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,9 +24,35 @@ pub enum BodyType {
|
||||||
JSON,
|
JSON,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug,PartialEq)]
|
/*
|
||||||
|
let mut body = String::from("{\"name\":");
|
||||||
|
body.push_str(&serde_json::to_string(&name)?);
|
||||||
|
body.push_str(",\"type\":\"");
|
||||||
|
body.push_str(rpc_type.rpc_id());
|
||||||
|
body.push_str("\",\"args\":[");
|
||||||
|
body.push_str(&serde_json::to_string(&args)?);
|
||||||
|
body.push_str("]}");
|
||||||
|
*/
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
pub struct Body<T:serde::Serialize> {
|
||||||
|
pub name : Vec<String>,
|
||||||
|
#[serde(rename="type")]
|
||||||
|
pub rpc_type : RpcType,
|
||||||
|
pub args : T,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T:serde::Serialize> Body<T> {
|
||||||
|
pub fn new(name: Vec<String>, rpc_type : RpcType, args:T) -> Self {
|
||||||
|
Body { name, rpc_type, args }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize,Debug,PartialEq)]
|
||||||
pub enum RpcType {
|
pub enum RpcType {
|
||||||
|
#[serde(rename="async")]
|
||||||
Async,
|
Async,
|
||||||
|
#[serde(rename="source")]
|
||||||
Source,
|
Source,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,7 +64,6 @@ impl RpcType {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug,PartialEq)]
|
#[derive(Debug,PartialEq)]
|
||||||
pub struct Header {
|
pub struct Header {
|
||||||
pub req_no : RequestNo,
|
pub req_no : RequestNo,
|
||||||
|
@ -124,28 +149,24 @@ impl<R:io::Read+Unpin , W:io::Write+Unpin> RpcClient<R,W> {
|
||||||
Ok((rpc_header,rpc_body))
|
Ok((rpc_header,rpc_body))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn send<T:serde::Serialize>(&mut self, name : &[&str], rpc_type: RpcType, args :&T) -> Result<RequestNo>{
|
pub async fn send<T:serde::Serialize>(&mut self, body : &Body<T>) -> Result<RequestNo>{
|
||||||
|
|
||||||
self.req_no+=1;
|
self.req_no+=1;
|
||||||
|
|
||||||
let mut body = String::from("{\"name\":");
|
let body_str = serde_json::to_string(body)?;
|
||||||
body.push_str(&serde_json::to_string(&name)?);
|
|
||||||
body.push_str(",\"type\":\"");
|
|
||||||
body.push_str(rpc_type.rpc_id());
|
|
||||||
body.push_str("\",\"args\":[");
|
|
||||||
body.push_str(&serde_json::to_string(&args)?);
|
|
||||||
body.push_str("]}");
|
|
||||||
|
|
||||||
let rpc_header = Header {
|
let rpc_header = Header {
|
||||||
req_no : self.req_no,
|
req_no : self.req_no,
|
||||||
is_stream : rpc_type == RpcType::Source,
|
is_stream : body.rpc_type == RpcType::Source,
|
||||||
is_end_or_error : false,
|
is_end_or_error : false,
|
||||||
body_type : BodyType::JSON,
|
body_type : BodyType::JSON,
|
||||||
body_len : body.len() as u32,
|
body_len : body_str.as_bytes().len() as u32,
|
||||||
}.to_array();
|
}.to_array();
|
||||||
|
|
||||||
|
println!("\n{}\n",body_str);
|
||||||
|
|
||||||
self.box_writer.write_all(&rpc_header[..]).await?;
|
self.box_writer.write_all(&rpc_header[..]).await?;
|
||||||
self.box_writer.write_all(body.as_bytes()).await?;
|
self.box_writer.write_all(body_str.as_bytes()).await?;
|
||||||
self.box_writer.flush().await?;
|
self.box_writer.flush().await?;
|
||||||
|
|
||||||
Ok(self.req_no)
|
Ok(self.req_no)
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
mod client;
|
mod client;
|
||||||
mod error;
|
mod error;
|
||||||
|
|
||||||
pub use client::{RpcClient,Header, RequestNo, RpcType};
|
pub use client::{RpcClient,Header, RequestNo, RpcType, Body};
|
||||||
pub use error::{Error,Result};
|
pub use error::{Error,Result};
|
||||||
|
|
Loading…
Reference in New Issue