|
1 | 1 | #![deny(rustdoc::broken_intra_doc_links)]
|
2 | 2 | #![deny(rustdoc::private_intra_doc_links)]
|
| 3 | + |
| 4 | +use std::error::Error; |
| 5 | + |
| 6 | +use ::prost::Message; |
| 7 | +use reqwest; |
| 8 | +use reqwest::Client; |
| 9 | + |
| 10 | +use crate::vss::{ |
| 11 | + GetObjectRequest, GetObjectResponse, ListKeyVersionsRequest, ListKeyVersionsResponse, PutObjectRequest, |
| 12 | + PutObjectResponse, |
| 13 | +}; |
| 14 | +use crate::vss_error::VssError; |
| 15 | + |
| 16 | +mod vss_error; |
| 17 | + |
| 18 | +/// Import auto-generated code |
| 19 | +pub mod vss { |
| 20 | + include!("generated-src/org.vss.rs"); |
| 21 | +} |
| 22 | + |
| 23 | +/// Thin-client to access hosted instance of `Versioned Storage Service (VSS)`. |
| 24 | +/// Api for VssClient is minimalistic and directly mimics vss-server-side api's as it is. |
| 25 | +pub struct VssClient { |
| 26 | + base_url: String, |
| 27 | + client: Client, |
| 28 | +} |
| 29 | + |
| 30 | +impl VssClient { |
| 31 | + /// Constructs new instance of VssClient. |
| 32 | + /// `base_url` is the vss-server endpoint to be used. |
| 33 | + pub fn new(base_url: &str) -> Result<Self, Box<dyn Error>> { |
| 34 | + let client = Client::new(); |
| 35 | + Ok(Self { base_url: String::from(base_url), client }) |
| 36 | + } |
| 37 | + |
| 38 | + /// Fetches a value against a given `key` in `request`. |
| 39 | + /// Makes a service call to GetObject endpoint of vss-server. |
| 40 | + /// For api-contract/usage, refer docs for: [`GetObjectRequest`] and [`GetObjectResponse`] |
| 41 | + pub async fn get_object(&self, request: GetObjectRequest) -> Result<GetObjectResponse, VssError> { |
| 42 | + let url = format!("{}/getObject", self.base_url); |
| 43 | + |
| 44 | + let response_raw = self.client.post(url).body(request.encode_to_vec()).send().await?; |
| 45 | + let status = response_raw.status(); |
| 46 | + let payload = response_raw.bytes().await?; |
| 47 | + |
| 48 | + if status.is_success() { |
| 49 | + let response = GetObjectResponse::decode(&payload[..])?; |
| 50 | + Ok(response) |
| 51 | + } else { |
| 52 | + Err(VssError::new(status, payload)) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /// Writes multiple `transaction_items` of `request` in single transaction. |
| 57 | + /// Makes a service call to PutObject endpoint of vss-server, with multiple items. |
| 58 | + /// Items in the `request` are written in a single all-or-nothing transaction. |
| 59 | + /// For api-contract/usage, refer docs for: [`PutObjectRequest`] and [`PutObjectResponse`] |
| 60 | + pub async fn put_object(&self, request: PutObjectRequest) -> Result<PutObjectResponse, VssError> { |
| 61 | + let url = format!("{}/putObjects", self.base_url); |
| 62 | + |
| 63 | + let response_raw = self.client.post(url).body(request.encode_to_vec()).send().await?; |
| 64 | + let status = response_raw.status(); |
| 65 | + let payload = response_raw.bytes().await?; |
| 66 | + |
| 67 | + if status.is_success() { |
| 68 | + let response = PutObjectResponse::decode(&payload[..])?; |
| 69 | + Ok(response) |
| 70 | + } else { |
| 71 | + Err(VssError::new(status, payload)) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /// Lists keys and their corresponding version for a given `store_id` in `request`. |
| 76 | + /// Makes a service call to ListKeyVersions endpoint of vss-server. |
| 77 | + /// For api-contract/usage, refer docs for: [`ListKeyVersionsRequest`] and [`ListKeyVersionsResponse`] |
| 78 | + pub async fn list_key_versions( |
| 79 | + &self, request: ListKeyVersionsRequest, |
| 80 | + ) -> Result<ListKeyVersionsResponse, VssError> { |
| 81 | + let url = format!("{}/listKeyVersions", self.base_url); |
| 82 | + |
| 83 | + let response_raw = self.client.post(url).body(request.encode_to_vec()).send().await?; |
| 84 | + let status = response_raw.status(); |
| 85 | + let payload = response_raw.bytes().await?; |
| 86 | + |
| 87 | + if status.is_success() { |
| 88 | + let response = ListKeyVersionsResponse::decode(&payload[..])?; |
| 89 | + Ok(response) |
| 90 | + } else { |
| 91 | + Err(VssError::new(status, payload)) |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments