Skip to content

Commit

Permalink
Add VSS Http thin client implementation for get/put/listKeyVersions a…
Browse files Browse the repository at this point in the history
…pi's
  • Loading branch information
G8XSU committed Apr 26, 2023
1 parent 7b9e187 commit e909549
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 230 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
/vss-accessor/src/proto/
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[workspace]
members = [
"vss-accessor",
]
]
5 changes: 3 additions & 2 deletions vss-accessor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ build = "build.rs"

[dependencies]
prost = "0.11.3"
reqwest = "0.11.13"
reqwest = { version = "0.11.13", features = ["rustls-tls"] }

[dev-dependencies]
mockito = "0.31.1"
tokio = { version = "1.22.0"}

[build-dependencies]
prost-build = { version = "0.11.3" }
prost-build = { version = "0.11.3" }
reqwest = { version = "0.11.13", features = ["blocking"] }
17 changes: 16 additions & 1 deletion vss-accessor/build.rs
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(())
}
95 changes: 95 additions & 0 deletions vss-accessor/src/lib.rs
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))
}
}
}
226 changes: 0 additions & 226 deletions vss-accessor/src/proto/vss.proto

This file was deleted.

Loading

0 comments on commit e909549

Please sign in to comment.