Skip to content

Commit

Permalink
Added Post - UNTESTED
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvsangamwar committed Dec 8, 2023
1 parent 8757de0 commit 594f6b0
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Cargo.lock

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

.DS_Store

# Added by cargo

Expand Down
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ serde = { version = "1.0", features = ["derive"] }
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"
Expand All @@ -25,6 +24,5 @@ pkcs8 = "0.10.2"
openssl = "0.10"
sha3 = "0.9.0"
ed25519-dalek = "1.0.1"
rand = "0.8.5"
rand_chacha = "0.3.1"
hex = "0.4"
24 changes: 19 additions & 5 deletions src/resdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use openssl::pkey::Private;

use std::collections::HashMap;
use serde_json::Value;
use serde::Serialize;

/// A struct representing the resource database.
pub struct ResDB;
Expand Down Expand Up @@ -176,19 +177,32 @@ impl ResDB {
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)
}

/** 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)
}

/// 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
}
}
53 changes: 52 additions & 1 deletion src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -103,3 +106,51 @@ where
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)
}

0 comments on commit 594f6b0

Please sign in to comment.