Improve rpc api return objects encoding
Signed-off-by: Lee Smet <lee.smet@hotmail.com>
This commit is contained in:
@@ -452,7 +452,7 @@
|
|||||||
"readers",
|
"readers",
|
||||||
"executors",
|
"executors",
|
||||||
"created_at",
|
"created_at",
|
||||||
"upddated_at"
|
"updated_at"
|
||||||
],
|
],
|
||||||
"properties": {
|
"properties": {
|
||||||
"id": {
|
"id": {
|
||||||
@@ -484,10 +484,9 @@
|
|||||||
"type": "integer",
|
"type": "integer",
|
||||||
"format": "int64"
|
"format": "int64"
|
||||||
},
|
},
|
||||||
"upddated_at": {
|
"updated_at": {
|
||||||
"type": "integer",
|
"type": "integer",
|
||||||
"format": "int64",
|
"format": "int64"
|
||||||
"description": "Typo retained intentionally"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -499,7 +498,7 @@
|
|||||||
"address",
|
"address",
|
||||||
"topic",
|
"topic",
|
||||||
"local",
|
"local",
|
||||||
"crated_at",
|
"created_at",
|
||||||
"updated_at"
|
"updated_at"
|
||||||
],
|
],
|
||||||
"properties": {
|
"properties": {
|
||||||
@@ -519,10 +518,9 @@
|
|||||||
"local": {
|
"local": {
|
||||||
"type": "boolean"
|
"type": "boolean"
|
||||||
},
|
},
|
||||||
"crated_at": {
|
"created_at": {
|
||||||
"type": "integer",
|
"type": "integer",
|
||||||
"format": "int64",
|
"format": "int64"
|
||||||
"description": "Typo retained intentionally"
|
|
||||||
},
|
},
|
||||||
"updated_at": {
|
"updated_at": {
|
||||||
"type": "integer",
|
"type": "integer",
|
||||||
|
@@ -10,6 +10,6 @@ pub use actor::Actor;
|
|||||||
pub use context::Context;
|
pub use context::Context;
|
||||||
pub use flow::Flow;
|
pub use flow::Flow;
|
||||||
pub use job::Job;
|
pub use job::Job;
|
||||||
pub use message::Message;
|
pub use message::{Message, MessageFormatType, MessageStatus, MessageType};
|
||||||
pub use runner::Runner;
|
pub use runner::Runner;
|
||||||
pub use script_type::ScriptType;
|
pub use script_type::ScriptType;
|
||||||
|
@@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
|
|||||||
|
|
||||||
use crate::{models::ScriptType, time::Timestamp};
|
use crate::{models::ScriptType, time::Timestamp};
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Clone, Serialize, Deserialize)]
|
||||||
pub struct Job {
|
pub struct Job {
|
||||||
/// Job Id, this is given by the actor who created the job
|
/// Job Id, this is given by the actor who created the job
|
||||||
id: u32,
|
id: u32,
|
||||||
@@ -28,7 +28,7 @@ pub struct Job {
|
|||||||
status: JobStatus,
|
status: JobStatus,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Clone, Serialize, Deserialize)]
|
||||||
pub enum JobStatus {
|
pub enum JobStatus {
|
||||||
Dispatched,
|
Dispatched,
|
||||||
WaitingForPrerequisites,
|
WaitingForPrerequisites,
|
||||||
|
@@ -5,7 +5,7 @@ use crate::{
|
|||||||
time::Timestamp,
|
time::Timestamp,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Clone, Serialize, Deserialize)]
|
||||||
pub struct Message {
|
pub struct Message {
|
||||||
/// Unique ID for the message, set by the caller
|
/// Unique ID for the message, set by the caller
|
||||||
id: u32,
|
id: u32,
|
||||||
@@ -29,14 +29,14 @@ pub struct Message {
|
|||||||
status: MessageStatus,
|
status: MessageStatus,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub enum MessageType {
|
pub enum MessageType {
|
||||||
Job,
|
Job,
|
||||||
Chat,
|
Chat,
|
||||||
Mail,
|
Mail,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub enum MessageStatus {
|
pub enum MessageStatus {
|
||||||
Dispatched,
|
Dispatched,
|
||||||
Acknowledged,
|
Acknowledged,
|
||||||
@@ -44,7 +44,7 @@ pub enum MessageStatus {
|
|||||||
Processed,
|
Processed,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub enum MessageFormatType {
|
pub enum MessageFormatType {
|
||||||
Html,
|
Html,
|
||||||
Text,
|
Text,
|
||||||
|
@@ -19,7 +19,7 @@ pub struct Runner {
|
|||||||
updated_at: Timestamp,
|
updated_at: Timestamp,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize, Clone)]
|
||||||
pub enum RunnerType {
|
pub enum RunnerType {
|
||||||
V,
|
V,
|
||||||
Python,
|
Python,
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize)]
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||||
pub enum ScriptType {
|
pub enum ScriptType {
|
||||||
Osis,
|
Osis,
|
||||||
Sal,
|
Sal,
|
||||||
|
97
src/rpc.rs
97
src/rpc.rs
@@ -13,7 +13,7 @@ use serde::{Deserialize, Serialize};
|
|||||||
use serde_json::{Value, json};
|
use serde_json::{Value, json};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
models::{Actor, Context, Flow, Job, Message, Runner, ScriptType},
|
models::{Actor, Context, Flow, Job, Message, MessageFormatType, Runner, ScriptType},
|
||||||
storage::RedisDriver,
|
storage::RedisDriver,
|
||||||
time::current_timestamp,
|
time::current_timestamp,
|
||||||
};
|
};
|
||||||
@@ -45,18 +45,6 @@ fn storage_err(e: Box<dyn std::error::Error + Send + Sync>) -> ErrorObjectOwned
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// -----------------------------
|
|
||||||
// Local enums for DTOs (to keep quirks isolated)
|
|
||||||
// -----------------------------
|
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize, Clone, Copy)]
|
|
||||||
#[serde(rename_all = "PascalCase")]
|
|
||||||
pub enum MessageFormatTypeDto {
|
|
||||||
Html,
|
|
||||||
Text,
|
|
||||||
Md,
|
|
||||||
}
|
|
||||||
|
|
||||||
// -----------------------------
|
// -----------------------------
|
||||||
// Create DTOs and Param wrappers
|
// Create DTOs and Param wrappers
|
||||||
// -----------------------------
|
// -----------------------------
|
||||||
@@ -91,7 +79,6 @@ pub struct ContextCreate {
|
|||||||
impl ContextCreate {
|
impl ContextCreate {
|
||||||
pub fn into_domain(self) -> Result<Context, String> {
|
pub fn into_domain(self) -> Result<Context, String> {
|
||||||
let ts = current_timestamp();
|
let ts = current_timestamp();
|
||||||
// Note: keep current code quirk: "upddated_at"
|
|
||||||
let mut v = serde_json::Map::new();
|
let mut v = serde_json::Map::new();
|
||||||
v.insert("id".to_string(), Value::from(self.id));
|
v.insert("id".to_string(), Value::from(self.id));
|
||||||
v.insert(
|
v.insert(
|
||||||
@@ -107,7 +94,7 @@ impl ContextCreate {
|
|||||||
serde_json::to_value(self.executors).unwrap(),
|
serde_json::to_value(self.executors).unwrap(),
|
||||||
);
|
);
|
||||||
v.insert("created_at".to_string(), Value::from(ts));
|
v.insert("created_at".to_string(), Value::from(ts));
|
||||||
v.insert("upddated_at".to_string(), Value::from(ts));
|
v.insert("updated_at".to_string(), Value::from(ts));
|
||||||
serde_json::from_value(Value::Object(v)).map_err(|e| e.to_string())
|
serde_json::from_value(Value::Object(v)).map_err(|e| e.to_string())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -123,14 +110,13 @@ pub struct RunnerCreate {
|
|||||||
impl RunnerCreate {
|
impl RunnerCreate {
|
||||||
pub fn into_domain(self) -> Result<Runner, String> {
|
pub fn into_domain(self) -> Result<Runner, String> {
|
||||||
let ts = current_timestamp();
|
let ts = current_timestamp();
|
||||||
// Note: keep current code quirk: "crated_at"
|
|
||||||
let v = json!({
|
let v = json!({
|
||||||
"id": self.id,
|
"id": self.id,
|
||||||
"pubkey": self.pubkey,
|
"pubkey": self.pubkey,
|
||||||
"address": self.address,
|
"address": self.address,
|
||||||
"topic": self.topic,
|
"topic": self.topic,
|
||||||
"local": self.local,
|
"local": self.local,
|
||||||
"crated_at": ts,
|
"created_at": ts,
|
||||||
"updated_at": ts,
|
"updated_at": ts,
|
||||||
});
|
});
|
||||||
serde_json::from_value(v).map_err(|e| e.to_string())
|
serde_json::from_value(v).map_err(|e| e.to_string())
|
||||||
@@ -209,9 +195,8 @@ pub struct MessageCreate {
|
|||||||
pub caller_id: u32,
|
pub caller_id: u32,
|
||||||
pub context_id: u32,
|
pub context_id: u32,
|
||||||
pub message: String,
|
pub message: String,
|
||||||
// Note: model uses ScriptType for message_type (keep as-is)
|
|
||||||
pub message_type: ScriptType,
|
pub message_type: ScriptType,
|
||||||
pub message_format_type: MessageFormatTypeDto,
|
pub message_format_type: MessageFormatType,
|
||||||
pub timeout: u32,
|
pub timeout: u32,
|
||||||
pub timeout_ack: u32,
|
pub timeout_ack: u32,
|
||||||
pub timeout_result: u32,
|
pub timeout_result: u32,
|
||||||
@@ -337,11 +322,7 @@ pub fn build_module(state: Arc<AppState>) -> RpcModule<()> {
|
|||||||
.save_actor(p.context_id, &actor)
|
.save_actor(p.context_id, &actor)
|
||||||
.await
|
.await
|
||||||
.map_err(storage_err)?;
|
.map_err(storage_err)?;
|
||||||
{
|
Ok::<_, ErrorObjectOwned>(actor)
|
||||||
let out: serde_json::Value =
|
|
||||||
serde_json::to_value(actor).map_err(invalid_params_err)?;
|
|
||||||
Ok::<serde_json::Value, ErrorObjectOwned>(out)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.expect("register actor.create");
|
.expect("register actor.create");
|
||||||
@@ -358,11 +339,7 @@ pub fn build_module(state: Arc<AppState>) -> RpcModule<()> {
|
|||||||
.load_actor(p.context_id, p.id)
|
.load_actor(p.context_id, p.id)
|
||||||
.await
|
.await
|
||||||
.map_err(storage_err)?;
|
.map_err(storage_err)?;
|
||||||
{
|
Ok::<_, ErrorObjectOwned>(actor)
|
||||||
let out: serde_json::Value =
|
|
||||||
serde_json::to_value(actor).map_err(invalid_params_err)?;
|
|
||||||
Ok::<serde_json::Value, ErrorObjectOwned>(out)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.expect("register actor.load");
|
.expect("register actor.load");
|
||||||
@@ -378,11 +355,7 @@ pub fn build_module(state: Arc<AppState>) -> RpcModule<()> {
|
|||||||
let p: ContextCreateParams = params.parse().map_err(invalid_params_err)?;
|
let p: ContextCreateParams = params.parse().map_err(invalid_params_err)?;
|
||||||
let ctx = p.context.into_domain().map_err(invalid_params_err)?;
|
let ctx = p.context.into_domain().map_err(invalid_params_err)?;
|
||||||
state.redis.save_context(&ctx).await.map_err(storage_err)?;
|
state.redis.save_context(&ctx).await.map_err(storage_err)?;
|
||||||
{
|
Ok::<_, ErrorObjectOwned>(ctx)
|
||||||
let out: serde_json::Value =
|
|
||||||
serde_json::to_value(ctx).map_err(invalid_params_err)?;
|
|
||||||
Ok::<serde_json::Value, ErrorObjectOwned>(out)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.expect("register context.create");
|
.expect("register context.create");
|
||||||
@@ -395,11 +368,7 @@ pub fn build_module(state: Arc<AppState>) -> RpcModule<()> {
|
|||||||
async move {
|
async move {
|
||||||
let p: ContextLoadParams = params.parse().map_err(invalid_params_err)?;
|
let p: ContextLoadParams = params.parse().map_err(invalid_params_err)?;
|
||||||
let ctx = state.redis.load_context(p.id).await.map_err(storage_err)?;
|
let ctx = state.redis.load_context(p.id).await.map_err(storage_err)?;
|
||||||
{
|
Ok::<_, ErrorObjectOwned>(ctx)
|
||||||
let out: serde_json::Value =
|
|
||||||
serde_json::to_value(ctx).map_err(invalid_params_err)?;
|
|
||||||
Ok::<serde_json::Value, ErrorObjectOwned>(out)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.expect("register context.load");
|
.expect("register context.load");
|
||||||
@@ -419,11 +388,7 @@ pub fn build_module(state: Arc<AppState>) -> RpcModule<()> {
|
|||||||
.save_runner(p.context_id, &runner)
|
.save_runner(p.context_id, &runner)
|
||||||
.await
|
.await
|
||||||
.map_err(storage_err)?;
|
.map_err(storage_err)?;
|
||||||
{
|
Ok::<_, ErrorObjectOwned>(runner)
|
||||||
let out: serde_json::Value =
|
|
||||||
serde_json::to_value(runner).map_err(invalid_params_err)?;
|
|
||||||
Ok::<serde_json::Value, ErrorObjectOwned>(out)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.expect("register runner.create");
|
.expect("register runner.create");
|
||||||
@@ -440,11 +405,7 @@ pub fn build_module(state: Arc<AppState>) -> RpcModule<()> {
|
|||||||
.load_runner(p.context_id, p.id)
|
.load_runner(p.context_id, p.id)
|
||||||
.await
|
.await
|
||||||
.map_err(storage_err)?;
|
.map_err(storage_err)?;
|
||||||
{
|
Ok::<_, ErrorObjectOwned>(runner)
|
||||||
let out: serde_json::Value =
|
|
||||||
serde_json::to_value(runner).map_err(invalid_params_err)?;
|
|
||||||
Ok::<serde_json::Value, ErrorObjectOwned>(out)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.expect("register runner.load");
|
.expect("register runner.load");
|
||||||
@@ -464,11 +425,7 @@ pub fn build_module(state: Arc<AppState>) -> RpcModule<()> {
|
|||||||
.save_flow(p.context_id, &flow)
|
.save_flow(p.context_id, &flow)
|
||||||
.await
|
.await
|
||||||
.map_err(storage_err)?;
|
.map_err(storage_err)?;
|
||||||
{
|
Ok::<_, ErrorObjectOwned>(flow)
|
||||||
let out: serde_json::Value =
|
|
||||||
serde_json::to_value(flow).map_err(invalid_params_err)?;
|
|
||||||
Ok::<serde_json::Value, ErrorObjectOwned>(out)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.expect("register flow.create");
|
.expect("register flow.create");
|
||||||
@@ -485,11 +442,7 @@ pub fn build_module(state: Arc<AppState>) -> RpcModule<()> {
|
|||||||
.load_flow(p.context_id, p.id)
|
.load_flow(p.context_id, p.id)
|
||||||
.await
|
.await
|
||||||
.map_err(storage_err)?;
|
.map_err(storage_err)?;
|
||||||
{
|
Ok::<_, ErrorObjectOwned>(flow)
|
||||||
let out: serde_json::Value =
|
|
||||||
serde_json::to_value(flow).map_err(invalid_params_err)?;
|
|
||||||
Ok::<serde_json::Value, ErrorObjectOwned>(out)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.expect("register flow.load");
|
.expect("register flow.load");
|
||||||
@@ -509,11 +462,8 @@ pub fn build_module(state: Arc<AppState>) -> RpcModule<()> {
|
|||||||
.save_job(p.context_id, &job)
|
.save_job(p.context_id, &job)
|
||||||
.await
|
.await
|
||||||
.map_err(storage_err)?;
|
.map_err(storage_err)?;
|
||||||
{
|
|
||||||
let out: serde_json::Value =
|
Ok::<_, ErrorObjectOwned>(job)
|
||||||
serde_json::to_value(job).map_err(invalid_params_err)?;
|
|
||||||
Ok::<serde_json::Value, ErrorObjectOwned>(out)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.expect("register job.create");
|
.expect("register job.create");
|
||||||
@@ -530,11 +480,7 @@ pub fn build_module(state: Arc<AppState>) -> RpcModule<()> {
|
|||||||
.load_job(p.context_id, p.caller_id, p.id)
|
.load_job(p.context_id, p.caller_id, p.id)
|
||||||
.await
|
.await
|
||||||
.map_err(storage_err)?;
|
.map_err(storage_err)?;
|
||||||
{
|
Ok::<_, ErrorObjectOwned>(job)
|
||||||
let out: serde_json::Value =
|
|
||||||
serde_json::to_value(job).map_err(invalid_params_err)?;
|
|
||||||
Ok::<serde_json::Value, ErrorObjectOwned>(out)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.expect("register job.load");
|
.expect("register job.load");
|
||||||
@@ -554,11 +500,7 @@ pub fn build_module(state: Arc<AppState>) -> RpcModule<()> {
|
|||||||
.save_message(p.context_id, &message)
|
.save_message(p.context_id, &message)
|
||||||
.await
|
.await
|
||||||
.map_err(storage_err)?;
|
.map_err(storage_err)?;
|
||||||
{
|
Ok::<_, ErrorObjectOwned>(message)
|
||||||
let out: serde_json::Value =
|
|
||||||
serde_json::to_value(message).map_err(invalid_params_err)?;
|
|
||||||
Ok::<serde_json::Value, ErrorObjectOwned>(out)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.expect("register message.create");
|
.expect("register message.create");
|
||||||
@@ -575,11 +517,7 @@ pub fn build_module(state: Arc<AppState>) -> RpcModule<()> {
|
|||||||
.load_message(p.context_id, p.caller_id, p.id)
|
.load_message(p.context_id, p.caller_id, p.id)
|
||||||
.await
|
.await
|
||||||
.map_err(storage_err)?;
|
.map_err(storage_err)?;
|
||||||
{
|
Ok::<_, ErrorObjectOwned>(msg)
|
||||||
let out: serde_json::Value =
|
|
||||||
serde_json::to_value(msg).map_err(invalid_params_err)?;
|
|
||||||
Ok::<serde_json::Value, ErrorObjectOwned>(out)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.expect("register message.load");
|
.expect("register message.load");
|
||||||
@@ -610,4 +548,3 @@ pub async fn start_ws<C>(
|
|||||||
let handle = server.start(module);
|
let handle = server.start(module);
|
||||||
Ok(handle)
|
Ok(handle)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user