Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Manage data transactions #3

Merged
merged 1 commit into from
Jul 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 112 additions & 36 deletions src/api.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
use crate::Result;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use serde_json::Value;
use serde_json::{json, Value};

#[derive(Deserialize, Debug, PartialEq, Default)]
pub trait ApiTrait {
fn url(&self) -> String;
fn token(&self) -> String;
}

#[derive(Deserialize, Debug, PartialEq, Default, Clone)]
pub struct ApiParam {
pub url: String,
pub token: String,
Expand All @@ -21,96 +26,167 @@ impl Api {
}
}

#[derive(Debug, PartialEq)]
pub struct DataApi {
api: ApiParam,
impl ApiTrait for Api {
fn url(&self) -> String {
self.data.api.url.clone()
}

fn token(&self) -> String {
self.data.api.token.clone()
}
}

impl DataApi {
pub fn get_doc<T: Doc>(&self, coll: &str, id: &str) -> Result<T> {
trait DataApiTrait: ApiTrait {
fn get_doc<T: Doc>(&self, coll: &str, id: &str) -> Result<T> {
log::debug!("get_doc {}[{}]", coll, id);
let request_url = format!(
"{url}/app/colls/{coll}/docs/{id}",
url = self.api.url,
url = self.url(),
id = id
);

ureq::get(request_url.as_str())
.set(
"Authorization",
format!("Bearer {}", self.api.token).as_str(),
)
.set("Authorization", format!("Bearer {}", self.token()).as_str())
.call()?
.into_json()
.map_err(|e| e.into())
}

pub fn create_doc<T: Doc>(&self, coll: &str, doc: T) -> Result<T> {
fn create_doc<T: Doc>(&self, coll: &str, doc: T) -> Result<T> {
log::debug!("create_doc {}", serde_json::to_string(&doc).unwrap());

let request_url = format!("{url}/app/colls/{coll}/docs", url = self.api.url);
let request_url = format!("{url}/app/colls/{coll}/docs", url = self.url());

ureq::post(request_url.as_str())
.set(
"Authorization",
format!("Bearer {}", self.api.token).as_str(),
)
.set("Authorization", format!("Bearer {}", self.token()).as_str())
.send_json(doc)?
.into_json()
.map_err(|e| e.into())
}

pub fn update_doc<T: Doc>(&self, coll: &str, doc: T) -> Result<T> {
fn update_doc<T: Doc>(&self, coll: &str, doc: T) -> Result<T> {
log::debug!("update_doc {}", serde_json::to_string(&doc).unwrap());

let request_url = format!(
"{url}/app/colls/{coll}/docs/{id}",
url = self.api.url,
url = self.url(),
id = doc.id().unwrap()
);

ureq::put(request_url.as_str())
.set(
"Authorization",
format!("Bearer {}", self.api.token).as_str(),
)
.set("Authorization", format!("Bearer {}", self.token()).as_str())
.send_json(doc)?
.into_json()
.map_err(|e| e.into())
}

pub fn delete_doc<T: Doc>(&self, coll: &str, doc: T) -> Result<()> {
fn delete_doc<T: Doc>(&self, coll: &str, doc: T) -> Result<()> {
let request_url = format!(
"{url}/app/colls/{coll}/docs/{id}",
url = self.api.url,
url = self.url(),
id = doc.id().unwrap()
);

ureq::delete(request_url.as_str())
.set(
"Authorization",
format!("Bearer {}", self.api.token).as_str(),
)
.set("Authorization", format!("Bearer {}", self.token()).as_str())
.call()?;

Ok(())
}

pub fn find<T: Doc, Q: Serialize>(&self, coll: &str, query: Q) -> Result<Vec<T>> {
fn find<T: Doc, Q: Serialize>(&self, coll: &str, query: Q) -> Result<Vec<T>> {
log::debug!("find {}", serde_json::to_string(&query).unwrap());
let request_url = format!("{url}/app/colls/${coll}/docs/find", url = self.api.url);
let request_url = format!("{url}/app/colls/${coll}/docs/find", url = self.url());

ureq::post(request_url.as_str())
.set(
"Authorization",
format!("Bearer {}", self.api.token).as_str(),
)
.set("Authorization", format!("Bearer {}", self.token()).as_str())
.send_json(query)?
.into_json()
.map_err(|e| e.into())
}
}

#[derive(Debug, PartialEq)]
pub struct DataApi {
api: ApiParam,
}

impl DataApi {
pub fn start_transaction(&self) -> Result<Transaction> {
log::debug!("start_transaction");

let request_url = format!("{url}/app/transaction", url = self.url());

ureq::post(request_url.as_str())
.set("Authorization", format!("Bearer {}", self.token()).as_str())
.send_json(json!({}))?
.into_string()
.map(|token| Transaction {
api: self.api.clone(),
token,
})
.map_err(|e| e.into())
}
}

impl ApiTrait for DataApi {
fn url(&self) -> String {
self.api.url.clone()
}

fn token(&self) -> String {
self.api.token.clone()
}
}

impl DataApiTrait for DataApi {}

#[derive(Debug, PartialEq)]
pub struct Transaction {
api: ApiParam,
token: String,
}

impl ApiTrait for Transaction {
fn url(&self) -> String {
self.api.url.clone()
}

fn token(&self) -> String {
self.token.clone()
}
}

impl DataApiTrait for Transaction {}

impl Transaction {
pub fn commit(&self) -> Result<()> {
log::debug!("transaction commit");

let request_url = format!("{url}/app/transaction/commit", url = self.url());

ureq::post(request_url.as_str())
.set("Authorization", format!("Bearer {}", self.token()).as_str())
.send_json(json!({}))?
.into_string()
.map(|_| ())
.map_err(|e| e.into())
}

pub fn abort(&self) -> Result<()> {
log::debug!("transaction commit");

let request_url = format!("{url}/app/transaction/abort", url = self.url());

ureq::post(request_url.as_str())
.set("Authorization", format!("Bearer {}", self.token()).as_str())
.send_json(json!({}))?
.into_string()
.map(|_| ())
.map_err(|e| e.into())
}
}

pub trait Doc: Sized + DeserializeOwned + Serialize + 'static + Clone {
fn id(&self) -> Option<String>;
}
Expand Down
Loading