Skip to content

Commit

Permalink
fix: compatibility with arcade 23.9.1
Browse files Browse the repository at this point in the history
  • Loading branch information
wolf4ood committed Oct 7, 2023
1 parent 885448c commit eebf33d
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 60 deletions.
10 changes: 7 additions & 3 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -64,13 +64,17 @@ impl Database {

pub async fn drop(&self) -> Result<GenericResponse, ArcadeDBError<ErrorResponse>> {
self.client
.request(DropDatabaseRequest::new(&self.name))
.request(ServerCommandRequest::<GenericResponse>::new(
ServerCommand::drop_db(&self.name),
))
.await
.map(|response| response.payload)
}
pub async fn create(&self) -> Result<GenericResponse, ArcadeDBError<ErrorResponse>> {
self.client
.request(CreateDatabaseRequest::new(&self.name))
.request(ServerCommandRequest::<GenericResponse>::new(
ServerCommand::create_db(&self.name),
))
.await
.map(|response| response.payload)
}
Expand Down
122 changes: 68 additions & 54 deletions src/protocol.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand All @@ -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;
Expand All @@ -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<String, String> {
HashMap::new()
Expand All @@ -31,32 +32,72 @@ pub enum Method {
Post,
}

pub struct ServerCommandRequest<'a, T> {
phantom: PhantomData<T>,
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<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
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)
}
}

Expand All @@ -82,8 +123,8 @@ impl Request for GetDatabasesRequest {
Method::Get
}

fn payload(&self) -> &Self::Payload {
&()
fn payload(&self) -> Option<&Self::Payload> {
None
}
}

Expand All @@ -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>,
Expand Down Expand Up @@ -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<String, String> {
let mut metadata = HashMap::new();
Expand All @@ -196,7 +208,7 @@ impl<'a> BeginRequest<'a> {
}

impl<'a> Request for BeginRequest<'a> {
type Payload = ();
type Payload = EmptyResponse;

type Response = ();

Expand All @@ -210,8 +222,8 @@ impl<'a> Request for BeginRequest<'a> {
Method::Post
}

fn payload(&self) -> &Self::Payload {
&()
fn payload(&self) -> Option<&Self::Payload> {
None
}
}

Expand Down Expand Up @@ -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<String, String> {
let mut metadata = HashMap::new();
metadata.insert(SESSION_HEADER.to_string(), self.session_id.to_string());
Expand Down Expand Up @@ -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<String, String> {
let mut metadata = HashMap::new();
metadata.insert(SESSION_HEADER.to_string(), self.session_id.to_string());
Expand Down
12 changes: 9 additions & 3 deletions src/transport/reqwest_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

Expand Down

0 comments on commit eebf33d

Please sign in to comment.