Skip to content

Commit

Permalink
Support _updatedOn field
Browse files Browse the repository at this point in the history
  • Loading branch information
kuy committed Sep 26, 2019
1 parent 4819fe2 commit 8d6242d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ fn main() -> Result<(), Error> {
// Create client with <BOX_ID>
let client = Client::new("enjoy_your_first_jsonbox_rs");

// Put data
// Insert data
let data = Data {
name: "kuy".into(),
message: "Hello, Jsonbox!".into(),
};
let (record, meta) = client.create(&data)?;
println!("CREATE: data={:?}, meta={:?}", record, meta);
println!("CREATE: data={:?}, id={} @{}", record, meta.id, meta.created_on);

Ok(())
}
Expand Down
45 changes: 37 additions & 8 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
use reqwest;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use snafu::ResultExt;
use std::convert::From;

use crate::error::{self, Error, Result};
use crate::query_builder::QueryBuilder;
use crate::url;

#[derive(Deserialize, Debug)]
pub struct Meta {
struct MetaRaw {
#[serde(rename = "_id")]
pub id: String,
id: String,
#[serde(rename = "_createdOn")]
created_on: String,
#[serde(rename = "_updatedOn", default)]
updated_on: Option<String>,
}

#[derive(Debug)]
pub struct Meta {
pub id: String,
pub created_on: String,
pub updated_on: String,
}

impl From<MetaRaw> for Meta {
fn from(meta: MetaRaw) -> Self {
let updated_on = match meta.updated_on {
Some(date) => date,
None => meta.created_on.clone(),
};
Meta {
id: meta.id.clone(),
created_on: meta.created_on.clone(),
updated_on,
}
}
}

#[derive(Deserialize, Debug)]
Expand Down Expand Up @@ -52,8 +76,9 @@ impl Client {
if res.status().is_success() {
let raw = res.text().context(error::Network {})?;
let data: T = serde_json::from_str(&raw).context(error::Json { reason: "data" })?;
let meta: Meta = serde_json::from_str(&raw).context(error::Json { reason: "meta" })?;
Ok((data, meta))
let meta: MetaRaw =
serde_json::from_str(&raw).context(error::Json { reason: "meta" })?;
Ok((data, Meta::from(meta)))
} else {
let err: ErrorMessage = res.json().context(error::Network {})?;
Err(Error::General {
Expand All @@ -76,8 +101,9 @@ impl Client {
if res.status().is_success() {
let raw = res.text().context(error::Network {})?;
let data: T = serde_json::from_str(&raw).context(error::Json { reason: "data" })?;
let meta: Meta = serde_json::from_str(&raw).context(error::Json { reason: "meta" })?;
Ok((data, meta))
let meta: MetaRaw =
serde_json::from_str(&raw).context(error::Json { reason: "meta" })?;
Ok((data, Meta::from(meta)))
} else {
let err: ErrorMessage = res.json().context(error::Network {})?;
Err(Error::General {
Expand All @@ -97,9 +123,12 @@ impl Client {
let raw = res.text().context(error::Network {})?;
let data: Vec<T> =
serde_json::from_str(&raw).context(error::Json { reason: "data" })?;
let meta: Vec<Meta> =
let meta: Vec<MetaRaw> =
serde_json::from_str(&raw).context(error::Json { reason: "meta" })?;
Ok(data.into_iter().zip(meta.into_iter()).collect())
Ok(data
.into_iter()
.zip(meta.into_iter().map(|meta| Meta::from(meta)))
.collect())
} else {
let err: ErrorMessage = res.json().context(error::Network {})?;
Err(Error::General {
Expand Down
19 changes: 19 additions & 0 deletions tests/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ fn test_create() {
assert_eq!(data.count, 42);
assert_eq!(meta.id, "11111111111111111111");
assert_eq!(meta.created_on, "2019-09-22T12:24:37.513Z");
assert_eq!(meta.updated_on, "2019-09-22T12:24:37.513Z");
}

#[test]
Expand Down Expand Up @@ -148,6 +149,7 @@ fn test_read() {
assert_eq!(data.count, 42);
assert_eq!(meta.id, "11111111111111111111");
assert_eq!(meta.created_on, "2019-09-22T12:24:37.513Z");
assert_eq!(meta.updated_on, "2019-09-22T12:24:37.513Z");
}

#[test]
Expand All @@ -172,13 +174,30 @@ fn test_update() {
.with_header("content-type", "application/json; charset=utf-8")
.with_body(r#"{"message":"Record updated."}"#)
.create();

let client = Client::with_base_url("00000000000000000000", &mockito::server_url());
let data = Data {
name: "cargo".into(),
count: 42,
};
let res = client.update("33333333333333333333", &data);
assert!(res.is_ok());

let _m = mock("GET", "/00000000000000000000/33333333333333333333")
.with_status(200)
.with_header("content-type", "application/json; charset=utf-8")
.with_body(r#"{"_id":"33333333333333333333","name":"cargo","count":42,"_createdOn":"2019-09-22T12:24:37.513Z","_updatedOn":"2019-09-22T12:25:52.114Z"}"#)
.create();

let res = client.read().id::<Data>("33333333333333333333");
assert!(res.is_ok());

let (data, meta) = res.unwrap();
assert_eq!(data.name, "cargo");
assert_eq!(data.count, 42);
assert_eq!(meta.id, "33333333333333333333");
assert_eq!(meta.created_on, "2019-09-22T12:24:37.513Z");
assert_eq!(meta.updated_on, "2019-09-22T12:25:52.114Z");
}

#[test]
Expand Down

0 comments on commit 8d6242d

Please sign in to comment.