|
@@ -3,9 +3,13 @@ use std::str;
|
|
|
use rand::Rng;
|
|
use rand::Rng;
|
|
|
use serde::{Deserialize, Serialize};
|
|
use serde::{Deserialize, Serialize};
|
|
|
use serde_json::{json, Value};
|
|
use serde_json::{json, Value};
|
|
|
|
|
+use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
|
|
|
|
+use tokio::net::TcpStream;
|
|
|
|
|
+
|
|
|
|
|
+use crate::Error;
|
|
|
|
|
|
|
|
-#[derive(Serialize, Deserialize, Debug)]
|
|
|
|
|
#[serde(untagged)]
|
|
#[serde(untagged)]
|
|
|
|
|
+#[derive(Serialize, Deserialize, Debug)]
|
|
|
pub enum JsonResult {
|
|
pub enum JsonResult {
|
|
|
Resp(JsonResponse),
|
|
Resp(JsonResponse),
|
|
|
Err(JsonError),
|
|
Err(JsonError),
|
|
@@ -86,3 +90,16 @@ pub fn notification(m: Value, p: Value) -> JsonNotification {
|
|
|
params: p,
|
|
params: p,
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+pub async fn send_request(url: String, data: Value) -> Result<JsonResult, Error> {
|
|
|
|
|
+ // TODO: TLS
|
|
|
|
|
+ let mut buf = [0; 2048];
|
|
|
|
|
+ let mut stream = TcpStream::connect(url).await?;
|
|
|
|
|
+ let data_str = serde_json::to_string(&data)?;
|
|
|
|
|
+
|
|
|
|
|
+ stream.write_all(&data_str.as_bytes()).await?;
|
|
|
|
|
+ let bytes_read = stream.read(&mut buf[..]).await?;
|
|
|
|
|
+
|
|
|
|
|
+ let reply: JsonResult = serde_json::from_slice(&buf[0..bytes_read])?;
|
|
|
|
|
+ Ok(reply)
|
|
|
|
|
+}
|