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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement PATCH operations #56

Merged
merged 7 commits into from
Feb 26, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 8 additions & 5 deletions src/kvstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,22 @@ impl KvStore

pub fn set(&self, key: String, value: Vec<u8>) -> Option<KvElement> {
// TODO: prepare iterative persistence
let mime_type = tree_magic::from_u8(value.as_ref());
match &mut self.container.get_mut(&key) {
Some(kv_element) => {
kv_element.data = value;
kv_element.mime_type = mime_type;
if !kv_element.locked {
let mime_type = tree_magic::from_u8(value.as_ref());
kv_element.data = value;
kv_element.mime_type = mime_type;
}
kv_element.updated_at = Utc::now();
kv_element.update_count = kv_element.update_count + 1;
kv_element.update_count = kv_element.update_count + 1;
Some(kv_element.to_owned())
},
None => {
let mime_type = tree_magic::from_u8(value.as_ref());
let kv_element = KvElement {
data: value,
mime_type: mime_type,
mime_type,
created_at: Utc::now(),
updated_at: Utc::now(),
expire_at: Utc::now(),
Expand Down
47 changes: 30 additions & 17 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,11 @@ impl Server {
.or(warp::patch()
.and(store.clone())
.and(api_kv_key_path)
.and(filters::body::content_length_limit(
configuration.http.request_size_limit,
))
imclint21 marked this conversation as resolved.
Show resolved Hide resolved
.and(filters::body::json())
.and(warp::body::bytes().and_then(|body: bytes::Bytes| async move {
std::str::from_utf8(&body)
.map(String::from)
.map_err(|_e| warp::reject::custom(NotUtf8)) // TODO: implement no valid string error
}))
.and_then(patch_key)),
);

Expand Down Expand Up @@ -209,25 +210,33 @@ async fn find_key(store: Arc<KvStore>, key: String) -> Result<impl Reply, Reject
}
}

#[derive(Debug, Deserialize)]
struct PatchValue {
operation: String,
}
imclint21 marked this conversation as resolved.
Show resolved Hide resolved
async fn patch_key(
store: Arc<KvStore>,
key: String,
patch_value: PatchValue,
body: String,
) -> Result<impl Reply, Rejection> {
if let Some(_) = store.get(key.clone()) {
match patch_value.operation.as_str() {
"lock" | "unlock" => {
let r = store.switch_lock(key.to_string(), true);
println!("{}", r);
Ok("")
if body.len() == 0 {
Err(reject::custom(Error::MissingBody))
}
else {
match body.to_lowercase().as_str() {
"lock" => {
store.switch_lock(key.to_string(), true);
Ok(warp::reply::json(&JsonMessage {
message: "The specified key was successfully locked.".to_string(),
}))
}
"unlock" => {
store.switch_lock(key.to_string(), false);
Ok(warp::reply::json(&JsonMessage {
message: "The specified key was successfully unlocked.".to_string(),
}))
}
_ => Err(reject::custom(Error::InvalidOperation {
operation: body,
}))
}
_ => Err(reject::custom(Error::InvalidOperation {
operation: patch_value.operation,
})),
}
} else {
Err(reject::custom(Error::KeyNotFound))
Expand Down Expand Up @@ -299,6 +308,10 @@ async fn process_error(err: Rejection) -> Result<impl Reply, Rejection> {
}
}

#[derive(Debug)]
struct NotUtf8;
impl warp::reject::Reject for NotUtf8 {}
imclint21 marked this conversation as resolved.
Show resolved Hide resolved

#[derive(Debug, Snafu)]
enum Error {
#[snafu(display("Missing request body."))]
Expand Down