Add API versioning to the JSON-RPC API

This commit is contained in:
Franz Heinzmann (Frando)
2022-07-15 14:26:55 +02:00
parent 15019ce02b
commit 361b7f5b69
7 changed files with 32 additions and 20 deletions

View File

@@ -34,20 +34,20 @@ use types::webxdc::WebxdcMessageInfo;
use self::types::message::MessageViewtype;
#[derive(Clone, Debug)]
pub struct CommandApi {
pub struct DeltaChatApiV0 {
pub(crate) accounts: Arc<RwLock<Accounts>>,
}
impl CommandApi {
impl DeltaChatApiV0 {
pub fn new(accounts: Accounts) -> Self {
CommandApi {
DeltaChatApiV0 {
accounts: Arc::new(RwLock::new(accounts)),
}
}
#[allow(dead_code)]
pub fn from_arc(accounts: Arc<RwLock<Accounts>>) -> Self {
CommandApi { accounts }
DeltaChatApiV0 { accounts }
}
async fn get_context(&self, id: u32) -> Result<deltachat::context::Context> {
@@ -63,7 +63,7 @@ impl CommandApi {
}
#[rpc(all_positional, ts_outdir = "typescript/generated")]
impl CommandApi {
impl DeltaChatApiV0 {
// ---------------------------------------------
// Misc top level functions
// ---------------------------------------------

View File

@@ -19,9 +19,7 @@ pub enum Account {
color: String,
},
#[serde(rename_all = "camelCase")]
Unconfigured {
id: u32,
},
Unconfigured { id: u32 },
}
impl Account {

View File

@@ -1,10 +1,11 @@
pub mod api;
pub use api::events;
pub use api::{Accounts, DeltaChatApiV0};
pub use yerpc;
#[cfg(test)]
mod tests {
use super::api::{Accounts, CommandApi};
use super::api::{Accounts, DeltaChatApiV0};
use async_channel::unbounded;
use futures::StreamExt;
use tempfile::TempDir;

View File

@@ -6,7 +6,7 @@ use yerpc::{RpcClient, RpcSession};
mod api;
use api::events::event_to_json_rpc_notification;
use api::{Accounts, CommandApi};
use api::{Accounts, DeltaChatApiV0};
const DEFAULT_PORT: u16 = 20808;
@@ -20,10 +20,10 @@ async fn main() -> Result<(), std::io::Error> {
.unwrap_or(DEFAULT_PORT);
log::info!("Starting with accounts directory `{path}`.");
let accounts = Accounts::new(PathBuf::from(&path)).await.unwrap();
let state = CommandApi::new(accounts);
let state = DeltaChatApiV0::new(accounts);
let app = Router::new()
.route("/ws", get(handler))
.route("/rpc/v0", get(handler))
.layer(Extension(state.clone()));
tokio::spawn(async move {