From eebf33d6863026baec675f808ae862157630014a Mon Sep 17 00:00:00 2001 From: Enrico Risa Date: Sat, 7 Oct 2023 23:48:56 +0200 Subject: [PATCH] fix: compatibility with arcade 23.9.1 --- src/db.rs | 10 ++- src/protocol.rs | 122 +++++++++++++++++++--------------- src/transport/reqwest_impl.rs | 12 +++- 3 files changed, 84 insertions(+), 60 deletions(-) diff --git a/src/db.rs b/src/db.rs index 6f510c5..ba5fa05 100644 --- a/src/db.rs +++ b/src/db.rs @@ -5,7 +5,7 @@ use serde::de::DeserializeOwned; use crate::{ command::{Statement, StatementKind}, error::{ArcadeDBError, ErrorResponse}, - protocol::{CreateDatabaseRequest, DropDatabaseRequest, GenericResponse, QueryCommand}, + protocol::{GenericResponse, QueryCommand, ServerCommand, ServerCommandRequest}, transaction::Transaction, ArcadeDB, }; @@ -64,13 +64,17 @@ impl Database { pub async fn drop(&self) -> Result> { self.client - .request(DropDatabaseRequest::new(&self.name)) + .request(ServerCommandRequest::::new( + ServerCommand::drop_db(&self.name), + )) .await .map(|response| response.payload) } pub async fn create(&self) -> Result> { self.client - .request(CreateDatabaseRequest::new(&self.name)) + .request(ServerCommandRequest::::new( + ServerCommand::create_db(&self.name), + )) .await .map(|response| response.payload) } diff --git a/src/protocol.rs b/src/protocol.rs index fd8a0f6..674f73a 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -1,6 +1,5 @@ -use std::{collections::HashMap, fmt::Display, marker::PhantomData}; - use serde::{de::DeserializeOwned, Deserialize, Serialize}; +use std::{collections::HashMap, fmt::Display, marker::PhantomData}; use crate::{ command::{Statement, StatementKind}, @@ -10,6 +9,8 @@ use crate::{ const SESSION_HEADER: &str = "arcadedb-session-id"; +type EmptyResponse = serde_json::Value; + pub trait Request { type Payload: Serialize; type Response: DeserializeOwned; @@ -19,7 +20,7 @@ pub trait Request { fn method(&self) -> Method; - fn payload(&self) -> &Self::Payload; + fn payload(&self) -> Option<&Self::Payload>; fn metadata(&self) -> HashMap { HashMap::new() @@ -31,32 +32,72 @@ pub enum Method { Post, } +pub struct ServerCommandRequest<'a, T> { + phantom: PhantomData, + command: Command<'a>, +} + #[derive(Serialize)] -pub struct CreateDatabaseRequest<'a> { - name: &'a str, +pub struct Command<'a> { + command: ServerCommand<'a>, } -impl<'a> CreateDatabaseRequest<'a> { - pub fn new(name: &'a str) -> Self { - Self { name } +impl<'a, T> ServerCommandRequest<'a, T> { + pub fn new(command: ServerCommand<'a>) -> ServerCommandRequest<'a, GenericResponse> { + ServerCommandRequest { + phantom: PhantomData, + command: Command { command }, + } } } -impl<'a> Request for CreateDatabaseRequest<'a> { - type Payload = (); - type Response = GenericResponse; +pub enum ServerCommand<'a> { + CreateDatabase(&'a str), + DropDatabase(&'a str), +} + +impl<'a> ServerCommand<'a> { + pub fn create_db(db: &'a str) -> Self { + Self::CreateDatabase(db) + } + pub fn drop_db(db: &'a str) -> Self { + Self::DropDatabase(db) + } +} + +impl<'a> Serialize for ServerCommand<'a> { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + match self { + ServerCommand::CreateDatabase(db) => { + serializer.serialize_str(&format!("CREATE DATABASE {}", db)) + } + ServerCommand::DropDatabase(db) => { + serializer.serialize_str(&format!("DROP DATABASE {}", db)) + } + } + } +} + +impl<'a, T: DeserializeOwned> Request for ServerCommandRequest<'a, T> { + type Payload = Command<'a>; + + type Response = T; type ResponseError = ErrorResponse; fn path(&self) -> String { - format!("/api/v1/create/{}", self.name) + "/api/v1/server".to_owned() } + fn method(&self) -> Method { Method::Post } - fn payload(&self) -> &Self::Payload { - &() + fn payload(&self) -> Option<&Self::Payload> { + Some(&self.command) } } @@ -82,8 +123,8 @@ impl Request for GetDatabasesRequest { Method::Get } - fn payload(&self) -> &Self::Payload { - &() + fn payload(&self) -> Option<&Self::Payload> { + None } } @@ -94,35 +135,6 @@ pub struct DatabasesResponse { pub version: String, } -#[derive(Serialize)] -pub struct DropDatabaseRequest<'a> { - name: &'a str, -} - -impl<'a> DropDatabaseRequest<'a> { - pub fn new(name: &'a str) -> Self { - Self { name } - } -} - -impl<'a> Request for DropDatabaseRequest<'a> { - type Payload = (); - type Response = GenericResponse; - - type ResponseError = ErrorResponse; - - fn path(&self) -> String { - format!("/api/v1/drop/{}", self.name) - } - fn method(&self) -> Method { - Method::Post - } - - fn payload(&self) -> &Self::Payload { - &() - } -} - pub struct QueryCommand<'a, 'b, T: DeserializeOwned, Q: Queryable> { payload: Statement<'a, 'b, Q>, session_id: Option<&'a str>, @@ -173,8 +185,8 @@ impl<'a, 'b, T: DeserializeOwned, Q: Queryable> Request for QueryCommand<'a, 'b, Method::Post } - fn payload(&self) -> &Self::Payload { - &self.payload + fn payload(&self) -> Option<&Self::Payload> { + Some(&self.payload) } fn metadata(&self) -> HashMap { let mut metadata = HashMap::new(); @@ -196,7 +208,7 @@ impl<'a> BeginRequest<'a> { } impl<'a> Request for BeginRequest<'a> { - type Payload = (); + type Payload = EmptyResponse; type Response = (); @@ -210,8 +222,8 @@ impl<'a> Request for BeginRequest<'a> { Method::Post } - fn payload(&self) -> &Self::Payload { - &() + fn payload(&self) -> Option<&Self::Payload> { + None } } @@ -241,9 +253,10 @@ impl<'a> Request for CommitRequest<'a> { Method::Post } - fn payload(&self) -> &Self::Payload { - &() + fn payload(&self) -> Option<&Self::Payload> { + None } + fn metadata(&self) -> HashMap { let mut metadata = HashMap::new(); metadata.insert(SESSION_HEADER.to_string(), self.session_id.to_string()); @@ -277,9 +290,10 @@ impl<'a> Request for RollbackRequest<'a> { Method::Post } - fn payload(&self) -> &Self::Payload { - &() + fn payload(&self) -> Option<&Self::Payload> { + None } + fn metadata(&self) -> HashMap { let mut metadata = HashMap::new(); metadata.insert(SESSION_HEADER.to_string(), self.session_id.to_string()); diff --git a/src/transport/reqwest_impl.rs b/src/transport/reqwest_impl.rs index b0c4a93..b35e5d1 100644 --- a/src/transport/reqwest_impl.rs +++ b/src/transport/reqwest_impl.rs @@ -68,10 +68,16 @@ impl ReqwestTransport { Method::Get => self.client.get(url), Method::Post => self.client.post(url), }; - builder + + let builder = builder .authenticated(&self.opts.auth) - .with_custom_headers(request.metadata()) - .json(request.payload()) + .with_custom_headers(request.metadata()); + + if let Some(json) = request.payload() { + builder.json(json) + } else { + builder + } } }