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

Proxy client #184

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ resolver = "2"
members = [
"libindy_vdr",
"indy-vdr-proxy",
"indy-vdr-proxy-client",
]

[profile.release]
Expand Down
12 changes: 12 additions & 0 deletions indy-vdr-proxy-client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "indy-vdr-proxy-client"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
reqwest = { version = "0.11.17", default-features = false, features = ["json"] }
indy-vdr = { path = "../libindy_vdr" }
serde_json = "1.0.96"
url = "2.3.1"
56 changes: 56 additions & 0 deletions indy-vdr-proxy-client/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use std::error::Error;
use std::fmt;

pub enum VdrProxyClientError {
HttpClientError(reqwest::Error),
ParseError(url::ParseError),
NonSuccessStatusCode(u16, String),
}

impl fmt::Display for VdrProxyClientError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
VdrProxyClientError::HttpClientError(err) => write!(f, "HTTP request failed: {}", err),
VdrProxyClientError::ParseError(err) => write!(f, "URL parsing failed: {}", err),
VdrProxyClientError::NonSuccessStatusCode(_, msg) => {
write!(f, "Non-success status code: {}", msg)
}
}
}
}

impl fmt::Debug for VdrProxyClientError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
VdrProxyClientError::HttpClientError(err) => {
write!(f, "HTTP request failed: {:?}", err)
}
VdrProxyClientError::ParseError(err) => write!(f, "URL parsing failed: {:?}", err),
VdrProxyClientError::NonSuccessStatusCode(code, body) => {
write!(f, "Non-success status code: {} {}", code, body)
}
}
}
}

impl Error for VdrProxyClientError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
VdrProxyClientError::HttpClientError(err) => Some(err),
VdrProxyClientError::ParseError(err) => Some(err),
VdrProxyClientError::NonSuccessStatusCode(_, _) => None,
}
}
}

impl From<reqwest::Error> for VdrProxyClientError {
fn from(err: reqwest::Error) -> VdrProxyClientError {
VdrProxyClientError::HttpClientError(err)
}
}

impl From<url::ParseError> for VdrProxyClientError {
fn from(err: url::ParseError) -> VdrProxyClientError {
VdrProxyClientError::ParseError(err)
}
}
136 changes: 136 additions & 0 deletions indy-vdr-proxy-client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
pub mod error;

use error::VdrProxyClientError;
use reqwest::{Client, Response, Url};

pub use indy_vdr::ledger::RequestBuilder;
pub use indy_vdr::pool::PreparedRequest;

pub struct VdrProxyClient {
client: Client,
url: Url,
}

async fn map_resp(response: Response) -> Result<String, VdrProxyClientError> {
let status = response.status();
if !status.is_success() {
let text = response.text().await.unwrap_or_default();
return Err(VdrProxyClientError::NonSuccessStatusCode(
status.as_u16(),
text,
));
}
response
.text()
.await
.map_err(VdrProxyClientError::HttpClientError)
}

impl VdrProxyClient {
pub fn new(url: &str) -> Result<VdrProxyClient, VdrProxyClientError> {
let url = Url::parse(url)?;
let client = Client::new();
Ok(VdrProxyClient { client, url })
}

async fn get_request(&self, url: Url) -> Result<String, VdrProxyClientError> {
let response = self.client.get(url).send().await?;
map_resp(response).await
}

async fn post_request(
&self,
url: Url,
request: PreparedRequest,
) -> Result<String, VdrProxyClientError> {
let response = self
.client
.post(url)
.json(&request.req_json)
.send()
.await
.map_err(VdrProxyClientError::HttpClientError)?;
map_resp(response).await
}

pub async fn post(&self, request: PreparedRequest) -> Result<String, VdrProxyClientError> {
let url = self.url.join("submit")?;
self.post_request(url, request).await
}

pub async fn get_nym(&self, did: &str) -> Result<String, VdrProxyClientError> {
let url = self.url.join(&format!("nym/{}", did))?;
self.get_request(url).await
}

pub async fn get_attrib(&self, did: &str, attrib: &str) -> Result<String, VdrProxyClientError> {
let url = self.url.join(&format!("attrib/{}/{}", did, attrib))?;
self.get_request(url).await
}

pub async fn get_schema(&self, schema_id: &str) -> Result<String, VdrProxyClientError> {
let url = self.url.join(&format!("schema/{}", schema_id))?;
self.get_request(url).await
}

pub async fn get_cred_def(&self, cred_def_id: &str) -> Result<String, VdrProxyClientError> {
let url = self.url.join(&format!("cred_def/{}", cred_def_id))?;
self.get_request(url).await
}

pub async fn get_rev_reg(&self, rev_reg_def_id: &str) -> Result<String, VdrProxyClientError> {
let url = self.url.join(&format!("rev_reg/{}", rev_reg_def_id))?;
self.get_request(url).await
}

pub async fn get_rev_reg_def(
&self,
rev_reg_def_id: &str,
) -> Result<String, VdrProxyClientError> {
let url = self.url.join(&format!("rev_reg_def/{}", rev_reg_def_id))?;
self.get_request(url).await
}

pub async fn get_rev_reg_delta(
&self,
rev_reg_def_id: &str,
) -> Result<String, VdrProxyClientError> {
let url = self
.url
.join(&format!("rev_reg_delta/{}", rev_reg_def_id))?;
self.get_request(url).await
}

pub async fn get_txn_author_agreement(&self) -> Result<String, VdrProxyClientError> {
let url = self.url.join("taa")?;
self.get_request(url).await
}

pub async fn get_genesis_txs(&self) -> Result<String, VdrProxyClientError> {
let url = self.url.join("genesis")?;
self.get_request(url).await
}

pub async fn get_acceptance_methods_list(&self) -> Result<String, VdrProxyClientError> {
let url = self.url.join("aml")?;
self.get_request(url).await
}

pub async fn get_auth_rules(&self) -> Result<String, VdrProxyClientError> {
let url = self.url.join("auth")?;
self.get_request(url).await
}

pub async fn get_proxy_status(&self) -> Result<String, VdrProxyClientError> {
self.get_request(self.url.clone()).await
}

pub async fn get_ledger_txn(
&self,
subledger: &str,
seq_no: u64,
) -> Result<String, VdrProxyClientError> {
let url = self.url.join(&format!("txn/{}/{}", subledger, seq_no))?;
self.get_request(url).await
}
}