Added Post - UNTESTED
diff --git a/.gitignore b/.gitignore
index 7a51535..71d807e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -12,7 +12,7 @@
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
-
+.DS_Store
# Added by cargo
diff --git a/Cargo.toml b/Cargo.toml
index 80edfec..8181975 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -16,7 +16,6 @@
serde_json = "1.0"
anyhow = "1.0.75"
file = "1.1.2"
-read = "0.1.1"
futures = "0.3"
futures-util = "0.3"
futures-macro = "0.3"
@@ -26,6 +25,5 @@
openssl = "0.10"
sha3 = "0.9.0"
ed25519-dalek = "1.0.1"
-rand = "0.8.5"
rand_chacha = "0.3.1"
hex = "0.4"
diff --git a/src/resdb.rs b/src/resdb.rs
index 90ba663..d03ea21 100644
--- a/src/resdb.rs
+++ b/src/resdb.rs
@@ -15,6 +15,7 @@
use std::collections::HashMap;
use serde_json::Value;
+use serde::Serialize;
/// A struct representing the resource database.
pub struct ResDB;
@@ -176,19 +177,32 @@
blocks::get_blocks_by_range_map(api_url, range_begin, range_end, map).await
}
- /** APIs provided to create public/private key pairs and Hashing **/
- pub fn generate_keypair(key_size: usize) -> (Vec<u8>, PKey<Private>)
- //pub fn generate_keypair() -> (Vec<u8>, PKey<Private>)
+ /// Provided function to create public/private key pairs
+ pub fn generate_keypair(&self, key_size: usize) -> (Vec<u8>, PKey<Private>)
where
{
- crypto::generate_keypair(key_size: usize)
+ crypto::generate_keypair(key_size)
}
- /** APIs provided to create hashed data using SHA3-256 **/
- pub fn hash_data(data: &str) -> String
+ /// Provided function to create hashed data using SHA3-256
+ pub fn hash_data(&self, data: &str) -> String
where
{
- crypto::hash_data(data: &str)
+ crypto::hash_data(data)
}
+ /// Post a transaction by passing in data (Struct) and endpoint
+ pub async fn post_transaction<T>(&self, data: T, endpoint: &str) -> Result<String, reqwest::Error>
+ where
+ T: Serialize,
+ {
+ transaction::post_transaction(data, endpoint).await
+ }
+
+ /// Post a transaction by passing in data (Hash Map) and endpoint
+ pub async fn post_transaction_map(&self, data: HashMap<&str, Value>, endpoint: &str) -> Result<String, reqwest::Error>
+ where
+ {
+ transaction::post_transaction_map(data, endpoint).await
+ }
}
diff --git a/src/transaction.rs b/src/transaction.rs
index a0c176b..d93c514 100644
--- a/src/transaction.rs
+++ b/src/transaction.rs
@@ -6,11 +6,14 @@
use std::fs::File;
use std::io::Read;
+use serde::Serialize;
+use serde::Serializer;
use serde::Deserialize;
use anyhow::Error;
use serde_json::Value;
+use serde_json::json;
+use reqwest::{header, StatusCode};
use reqwest::blocking::Client;
-use reqwest::StatusCode;
use std::collections::HashMap;
/// A testing function that reads JSON data from a file.
@@ -103,3 +106,51 @@
let transactions: Vec<HashMap<String, Value>> = response.json().await?;
Ok(transactions)
}
+
+/// Post transaction to an endpoint using a struct
+pub async fn post_transaction<T>(
+ _data: T,
+ _endpoint: &str,
+) -> Result<String, reqwest::Error>
+where
+ T: Serialize,
+{
+ let client = reqwest::Client::builder().build()?;
+ let mut headers = reqwest::header::HeaderMap::new();
+ headers.insert(
+ header::CONTENT_TYPE,
+ header::HeaderValue::from_static("application/json"),
+ );
+ let json = json!(_data);
+ let response = client
+ .post(_endpoint)
+ .headers(headers)
+ .json(&json)
+ .send()
+ .await?;
+
+ let body = response.text().await?;
+ Ok(body)
+}
+
+/// Post transaction to an endpoint using a map
+pub async fn post_transaction_map(
+ _data: HashMap<&str, Value>,
+ _endpoint: &str,
+) -> Result<String, reqwest::Error>
+{
+ let client = reqwest::Client::builder().build()?;
+ let mut headers = reqwest::header::HeaderMap::new();
+ headers.insert(
+ header::CONTENT_TYPE,
+ header::HeaderValue::from_static("application/json"),
+ );
+ let json = json!(_data);
+ let request = client.request(reqwest::Method::POST, _endpoint)
+ .headers(headers)
+ .json(&json);
+
+ let response = request.send().await?;
+ let body = response.text().await?;
+ Ok(body)
+}
\ No newline at end of file