Skip to content

Commit

Permalink
Add delete_object fn to client
Browse files Browse the repository at this point in the history
  • Loading branch information
G8XSU committed Aug 17, 2023
1 parent 75fa21e commit 928f91e
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 8 deletions.
22 changes: 20 additions & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use reqwest::Client;

use crate::error::VssError;
use crate::types::{
GetObjectRequest, GetObjectResponse, ListKeyVersionsRequest, ListKeyVersionsResponse, PutObjectRequest,
PutObjectResponse,
DeleteObjectRequest, DeleteObjectResponse, GetObjectRequest, GetObjectResponse, ListKeyVersionsRequest,
ListKeyVersionsResponse, PutObjectRequest, PutObjectResponse,
};

/// Thin-client to access a hosted instance of Versioned Storage Service (VSS).
Expand Down Expand Up @@ -60,6 +60,24 @@ impl VssClient {
}
}

/// Deletes the given `key` and `value` in `request`.
/// Makes a service call to the `DeleteObject` endpoint of the VSS server.
/// For API contract/usage, refer to docs for [`DeleteObjectRequest`] and [`DeleteObjectResponse`].
pub async fn delete_object(&self, request: &DeleteObjectRequest) -> Result<DeleteObjectResponse, VssError> {
let url = format!("{}/deleteObject", self.base_url);

let response_raw = self.client.post(url).body(request.encode_to_vec()).send().await?;
let status = response_raw.status();
let payload = response_raw.bytes().await?;

if status.is_success() {
let response = DeleteObjectResponse::decode(&payload[..])?;
Ok(response)
} else {
Err(VssError::new(status, payload))
}
}

/// Lists keys and their corresponding version for a given [`ListKeyVersionsRequest::store_id`].
/// Makes a service call to the `ListKeyVersions` endpoint of the VSS server.
/// For API contract/usage, refer to docs for [`ListKeyVersionsRequest`] and [`ListKeyVersionsResponse`].
Expand Down
64 changes: 58 additions & 6 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ mod tests {
use vss_client::error::VssError;

use vss_client::types::{
ErrorCode, ErrorResponse, GetObjectRequest, GetObjectResponse, KeyValue, ListKeyVersionsRequest,
ListKeyVersionsResponse, PutObjectRequest, PutObjectResponse,
DeleteObjectRequest, DeleteObjectResponse, ErrorCode, ErrorResponse, GetObjectRequest, GetObjectResponse,
KeyValue, ListKeyVersionsRequest, ListKeyVersionsResponse, PutObjectRequest, PutObjectResponse,
};

const GET_OBJECT_ENDPOINT: &'static str = "/getObject";
const PUT_OBJECT_ENDPOINT: &'static str = "/putObjects";
const DELETE_OBJECT_ENDPOINT: &'static str = "/deleteObject";
const LIST_KEY_VERSIONS_ENDPOINT: &'static str = "/listKeyVersions";

#[tokio::test]
Expand Down Expand Up @@ -55,6 +56,7 @@ mod tests {
store_id: "store".to_string(),
global_version: Some(4),
transaction_items: vec![KeyValue { key: "k1".to_string(), version: 2, value: b"k1v3".to_vec() }],
delete_items: vec![],
};
let mock_response = PutObjectResponse::default();

Expand All @@ -76,6 +78,36 @@ mod tests {
mock_server.expect(1).assert();
}

#[tokio::test]
async fn test_delete() {
// Spin-up mock server with mock response for given request.
let base_url = mockito::server_url().to_string();

// Set up the mock request/response.
let request = DeleteObjectRequest {
store_id: "store".to_string(),
key_value: Some(KeyValue { key: "k1".to_string(), version: 2, value: b"k1v3".to_vec() }),
};
let mock_response = DeleteObjectResponse::default();

// Register the mock endpoint with the mockito server.
let mock_server = mockito::mock("POST", DELETE_OBJECT_ENDPOINT)
.match_body(request.encode_to_vec())
.with_status(200)
.with_body(mock_response.encode_to_vec())
.create();

// Create a new VssClient with the mock server URL.
let vss_client = VssClient::new(&base_url);
let actual_result = vss_client.delete_object(&request).await.unwrap();

let expected_result = &mock_response;
assert_eq!(actual_result, *expected_result);

// Verify server endpoint was called exactly once.
mock_server.expect(1).assert();
}

#[tokio::test]
async fn test_list_key_versions() {
// Spin-up mock server with mock response for given request.
Expand Down Expand Up @@ -142,10 +174,19 @@ mod tests {
store_id: "store".to_string(),
global_version: Some(4),
transaction_items: vec![KeyValue { key: "k1".to_string(), version: 2, value: b"k1v3".to_vec() }],
delete_items: vec![],
})
.await;
assert!(matches!(put_result.unwrap_err(), VssError::InvalidRequestError { .. }));

let delete_result = vss_client
.delete_object(&DeleteObjectRequest {
store_id: "store".to_string(),
key_value: Some(KeyValue { key: "k1".to_string(), version: 2, value: b"k1v3".to_vec() }),
})
.await;
assert!(matches!(delete_result.unwrap_err(), VssError::InvalidRequestError { .. }));

let list_result = vss_client
.list_key_versions(&ListKeyVersionsRequest {
store_id: "store".to_string(),
Expand All @@ -156,8 +197,8 @@ mod tests {
.await;
assert!(matches!(list_result.unwrap_err(), VssError::InvalidRequestError { .. }));

// Verify 3 requests hit the server
mock_server.expect(3).assert();
// Verify 4 requests hit the server
mock_server.expect(4).assert();
}

#[tokio::test]
Expand All @@ -178,6 +219,7 @@ mod tests {
store_id: "store".to_string(),
global_version: Some(4),
transaction_items: vec![KeyValue { key: "k1".to_string(), version: 2, value: b"k1v3".to_vec() }],
delete_items: vec![],
})
.await;
assert!(matches!(put_result.unwrap_err(), VssError::ConflictError { .. }));
Expand Down Expand Up @@ -211,10 +253,19 @@ mod tests {
store_id: "store".to_string(),
global_version: Some(4),
transaction_items: vec![KeyValue { key: "k1".to_string(), version: 2, value: b"k1v3".to_vec() }],
delete_items: vec![],
})
.await;
assert!(matches!(put_result.unwrap_err(), VssError::InternalServerError { .. }));

let delete_result = vss_client
.delete_object(&DeleteObjectRequest {
store_id: "store".to_string(),
key_value: Some(KeyValue { key: "k1".to_string(), version: 2, value: b"k1v3".to_vec() }),
})
.await;
assert!(matches!(delete_result.unwrap_err(), VssError::InternalServerError { .. }));

let list_result = vss_client
.list_key_versions(&ListKeyVersionsRequest {
store_id: "store".to_string(),
Expand All @@ -225,8 +276,8 @@ mod tests {
.await;
assert!(matches!(list_result.unwrap_err(), VssError::InternalServerError { .. }));

// Verify 3 requests hit the server
mock_server.expect(3).assert();
// Verify 4 requests hit the server
mock_server.expect(4).assert();
}

#[tokio::test]
Expand All @@ -248,6 +299,7 @@ mod tests {
store_id: "store".to_string(),
global_version: Some(4),
transaction_items: vec![KeyValue { key: "k1".to_string(), version: 2, value: b"k1v3".to_vec() }],
delete_items: vec![],
};
let put_result = vss_client.put_object(&put_request).await;
assert!(matches!(put_result.unwrap_err(), VssError::InternalError { .. }));
Expand Down

0 comments on commit 928f91e

Please sign in to comment.