-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add VSS Http thin client implementation for get/put/listKeyVersions a…
…pi's
- Loading branch information
Showing
7 changed files
with
191 additions
and
230 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
/target | ||
/vss-accessor/src/proto/ |
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,4 +1,4 @@ | ||
[workspace] | ||
members = [ | ||
"vss-accessor", | ||
] | ||
] |
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,6 +1,21 @@ | ||
extern crate prost_build; | ||
|
||
use std::fs; | ||
use std::fs::File; | ||
use std::path::Path; | ||
|
||
fn main() { | ||
download_file("https://raw.githubusercontent.com/lightningdevkit/vss-server/main/app/src/main/proto/vss.proto", | ||
"src/proto/vss.proto").unwrap(); | ||
|
||
prost_build::compile_protos(&["src/proto/vss.proto"], | ||
&["src/"]).unwrap(); | ||
} | ||
} | ||
|
||
fn download_file(url: &str, save_to: &str) -> Result<(), Box<dyn std::error::Error>> { | ||
let mut response = reqwest::blocking::get(url)?; | ||
fs::create_dir_all(Path::new(save_to).parent().unwrap())?; | ||
let mut out_file = File::create(save_to)?; | ||
response.copy_to(&mut out_file)?; | ||
Ok(()) | ||
} |
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,95 @@ | ||
use ::prost::Message; | ||
use reqwest; | ||
use reqwest::Client; | ||
use std::error::Error; | ||
|
||
use crate::vss::{GetObjectRequest, GetObjectResponse, KeyValue, ListKeyVersionsRequest, ListKeyVersionsResponse, PutObjectRequest, PutObjectResponse}; | ||
use crate::vss_error::VssError; | ||
|
||
mod vss_error; | ||
|
||
pub mod vss { | ||
include!(concat!(env!("OUT_DIR"), "/org.vss.rs")); | ||
} | ||
|
||
pub struct VssAccessor { | ||
base_url: String, | ||
client: Client, | ||
} | ||
|
||
impl VssAccessor { | ||
pub fn new(base_url: &str) -> Result<Self, Box<dyn Error>> { | ||
let client = Client::new(); | ||
Ok(Self { base_url: String::from(base_url), client }) | ||
} | ||
|
||
pub async fn get(&self, store: &str, key: &str) -> Result<GetObjectResponse, VssError> { | ||
let url = format!("{}/getObject", self.base_url); | ||
|
||
let request = GetObjectRequest { store_id: store.into(), key: key.to_string() }; | ||
|
||
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 = GetObjectResponse::decode(&payload[..])?; | ||
Ok(response) | ||
} else { | ||
Err(VssError::new(status, &payload)) | ||
} | ||
} | ||
|
||
pub async fn put(&self, store: &str, global_version: Option<i64>, key: &str, version: i64, value: &[u8]) | ||
-> Result<PutObjectResponse, VssError> { | ||
let kv = KeyValue { key: String::from(key), version, value: value.to_vec() }; | ||
return self.put_tx(store, global_version, vec![kv]).await; | ||
} | ||
|
||
pub async fn put_tx(&self, store: &str, global_version: Option<i64>, transaction_items: Vec<KeyValue>) | ||
-> Result<PutObjectResponse, VssError> { | ||
let url = format!("{}/putObjects", self.base_url); | ||
|
||
let request = PutObjectRequest { store_id: store.into(), global_version, transaction_items }; | ||
|
||
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 = PutObjectResponse::decode(&payload[..])?; | ||
Ok(response) | ||
} else { | ||
Err(VssError::new(status, &payload)) | ||
} | ||
} | ||
|
||
pub async fn list_key_versions(&self, store: &str, key_prefix: &str, page_size: Option<i32>, page_token: Option<String>) | ||
-> Result<ListKeyVersionsResponse, VssError> { | ||
let url = format!("{}/listKeyVersions", self.base_url); | ||
|
||
let request = ListKeyVersionsRequest { | ||
store_id: store.to_string(), | ||
key_prefix: Some(key_prefix.to_string()), | ||
page_size, | ||
page_token, | ||
}; | ||
|
||
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 = ListKeyVersionsResponse::decode(&payload[..])?; | ||
Ok(response) | ||
} else { | ||
Err(VssError::new(status, &payload)) | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.