-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from sreedevk/v003
Version 0.0.3
- Loading branch information
Showing
19 changed files
with
510 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
pub mod operations; | ||
|
||
pub use operations::create; | ||
pub use operations::list; | ||
pub use operations::view; | ||
pub use operations::delete; | ||
pub use operations::update; | ||
|
||
pub trait Recordable {} | ||
pub trait Changeset {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use crate::client::Client; | ||
use serde::{Serialize, Deserialize}; | ||
use std::error::Error; | ||
use serde::de::DeserializeOwned; | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct FailureResponse { | ||
code: String, | ||
message: String | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
#[serde(rename_all = "camelCase")] | ||
pub enum CreateResponse<T> { | ||
SuccessResponse(T), | ||
FailureResponse(FailureResponse) | ||
} | ||
|
||
pub async fn record<T: Serialize + DeserializeOwned>(collection: &str, changeset: &T, client: &Client) -> Result<CreateResponse<T>, Box<dyn Error>> { | ||
let url = format!("collections/{}/records", collection); | ||
match client.post::<T>(url, &changeset).await { | ||
Ok(response) => { | ||
match response.json::<T>().await { | ||
Ok(parsed) => Ok(CreateResponse::SuccessResponse(parsed)), | ||
Err(e) => Err(Box::new(e) as Box<dyn Error>) | ||
} | ||
}, | ||
Err(e) => Err(e) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use std::collections::HashMap; | ||
use std::error::Error; | ||
|
||
use serde::{Serialize, Deserialize}; | ||
|
||
use crate::client::Client; | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
struct SuccessResponse {} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
struct FailureResponse { | ||
code: String, | ||
message: String, | ||
data: HashMap<String, String> | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
#[serde(rename_all = "camelCase", untagged)] | ||
enum DeleteResponse { | ||
SuccessResponse(SuccessResponse), | ||
FailureResponse(FailureResponse) | ||
} | ||
|
||
pub async fn record(collection: &str, id: &str, client: &Client) -> Result<(), Box<dyn Error>> { | ||
let url = format!("/api/collections/{}/records/{}", collection, id); | ||
match client.delete(url, None).await { | ||
Ok(resp) => { | ||
match resp.json::<DeleteResponse>().await { | ||
Ok(_) => Ok(()), | ||
Err(e) => Err(Box::new(e) as Box<dyn Error>) | ||
} | ||
}, | ||
Err(err) => Err(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
pub mod list; | ||
pub mod create; | ||
pub mod view; | ||
pub mod delete; | ||
pub mod update; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use crate::client::Client; | ||
use serde::{Serialize, Deserialize}; | ||
use std::error::Error; | ||
use serde::de::DeserializeOwned; | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct FailureResponse { | ||
code: String, | ||
message: String | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
#[serde(rename_all = "camelCase")] | ||
pub enum CreateResponse<T> { | ||
SuccessResponse(T), | ||
FailureResponse(FailureResponse) | ||
} | ||
|
||
pub async fn record<T: Serialize + DeserializeOwned>(collection: &str, id: &str, changeset: &T, client: &Client) -> Result<CreateResponse<T>, Box<dyn Error>> { | ||
let url = format!("collections/{}/records/{}", collection, id); | ||
match client.patch::<T>(url, &changeset).await { | ||
Ok(response) => { | ||
match response.json::<T>().await { | ||
Ok(parsed) => Ok(CreateResponse::SuccessResponse(parsed)), | ||
Err(e) => Err(Box::new(e) as Box<dyn Error>) | ||
} | ||
}, | ||
Err(e) => Err(e) | ||
} | ||
} |
Oops, something went wrong.