From d409d7af798d192c23f1eaf32aec1019efdcf0d4 Mon Sep 17 00:00:00 2001 From: Evan <0xIchigo@protonmail.com> Date: Sat, 27 Apr 2024 04:22:52 -0400 Subject: [PATCH] Trying to Streamline POST Reqs --- src/request_handler.rs | 7 +++++++ src/rpc_client.rs | 35 ++++++++++++++++++++++--------- src/types/types.rs | 41 +++++++++++++++++++++++++++++-------- tests/rpc/test_get_asset.rs | 4 ++++ 4 files changed, 69 insertions(+), 18 deletions(-) create mode 100644 tests/rpc/test_get_asset.rs diff --git a/src/request_handler.rs b/src/request_handler.rs index bb3f7c3..78b3a17 100644 --- a/src/request_handler.rs +++ b/src/request_handler.rs @@ -33,6 +33,9 @@ impl RequestHandler { } let response: Response = self.send_request(request_builder).await?; + + print!("{:?}", response); + self.handle_response(response).await } @@ -42,6 +45,10 @@ impl RequestHandler { let path: String = response.url().path().to_string(); let body_text: String = response.text().await.unwrap_or_default(); + print!("{}", status); + print!("{}", path); + print!("{}", body_text); + if status.is_success() { match serde_json::from_str::(&body_text) { Ok(data) => Ok(data), diff --git a/src/rpc_client.rs b/src/rpc_client.rs index c6dec41..af16225 100644 --- a/src/rpc_client.rs +++ b/src/rpc_client.rs @@ -1,12 +1,15 @@ +use std::fmt::Debug; use std::sync::Arc; use crate::config::Config; use crate::error::Result; use crate::request_handler::RequestHandler; -use crate::types::types::ApiResponse; +use crate::types::types::{ApiResponse, RpcRequest, RpcResponse}; use crate::types::{AssetsByAuthorityRequest, AssetsByOwnerRequest, GetAssetRequest}; use reqwest::{Client, Method, Url}; +use serde::de::DeserializeOwned; +use serde::Serialize; use serde_json::{json, Value}; pub struct RpcClient { @@ -21,19 +24,31 @@ impl RpcClient { Ok(RpcClient { handler, config }) } - /// Gets an asset by its ID - pub async fn get_asset(&self, request: GetAssetRequest) -> Result { + /// Streamlines an RPC POST request + pub async fn post_rpc_request(&self, method: String, request: R) -> Result + where + R: Debug + Serialize + Debug + Send + Sync, + T: Debug +DeserializeOwned + Default, + { let base_url: String = format!("{}?api-key={}", self.config.endpoints.rpc, self.config.api_key); let url: Url = Url::parse(&base_url).expect("Failed to parse URL"); - let request_body: Value = json!({ - "jsonrpc": "2.0", - "id": 1, - "method": "getAsset", - "params": request - }); + print!("{}", base_url); + print!("{}", url); - self.handler.send(Method::POST, url, Some(&request_body)).await + let rpc_request: RpcRequest = RpcRequest::new(method, request); + + + print!("{:?}", rpc_request); + + let rpc_response: RpcResponse = self.handler.send(Method::POST, url, Some(&rpc_request)).await?; + print!("{:?}", rpc_response); + Ok(rpc_response.result) + } + + /// Gets an asset by its ID + pub async fn get_asset(&self, request: GetAssetRequest) -> Result { + self.post_rpc_request("getAsset".to_string(), request).await } /// Gets a list of assets owned by a given address diff --git a/src/types/types.rs b/src/types/types.rs index f06f004..1b20e27 100644 --- a/src/types/types.rs +++ b/src/types/types.rs @@ -33,6 +33,33 @@ impl HeliusEndpoints { } } +#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)] +pub struct RpcRequest { + pub jsonrpc: String, + pub id: String, + pub method: String, + pub parameters: T, +} + +impl RpcRequest { + pub fn new(method: String, parameters: T) -> Self { + Self { + jsonrpc: "2.0".to_string(), + id: "1".to_string(), + method, + parameters, + } + } +} + +#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)] +pub struct RpcResponse { + pub jsonrpc: String, + pub id: String, + pub result: T, + +} + #[derive(Serialize, Deserialize, Default)] pub struct AssetsByOwnerRequest { #[serde(rename = "ownerAddress")] @@ -68,22 +95,20 @@ pub struct AssetsByAuthorityRequest { #[derive(Serialize, Deserialize, Debug)] pub struct GetAssetRequest { pub id: String, - #[serde(rename = "displayOptions")] + #[serde(flatten)] pub display_options: Option, } #[derive(Serialize, Deserialize, Debug)] pub struct DisplayOptions { #[serde(rename = "showUnverifiedCollections")] - pub show_unverified_collections: Option, + pub show_unverified_collections: bool, #[serde(rename = "showCollectionMetadata")] - pub show_collection_metadata: Option, - #[serde(rename = "showGrandTotal")] - pub show_grand_total: Option, + pub show_collection_metadata: bool, #[serde(rename = "showFungible")] - pub show_fungible: Option, + pub show_fungible: bool, #[serde(rename = "showInscription")] - pub show_inscription: Option, + pub show_inscription: bool, } #[derive(Serialize, Deserialize, Debug)] @@ -107,7 +132,7 @@ pub enum ResponseType { #[default] DefaultResponse, // This is a placeholder for the default response type. TODO: Replace this an appropriate type GetAssetResponseList(GetAssetResponseList), - GetAssetResponse(GetAssetResponse), + GetAssetResponseForAsset(GetAssetResponseForAsset), Other(Value), } diff --git a/tests/rpc/test_get_asset.rs b/tests/rpc/test_get_asset.rs new file mode 100644 index 0000000..690a94e --- /dev/null +++ b/tests/rpc/test_get_asset.rs @@ -0,0 +1,4 @@ +use helius_sdk::config::Config; +use helius_sdk::error::HeliusError; +use helius_sdk::rpc_client::RpcClient; +use helius_sdk::types::{ApiResponse, GetAssetRequest, GetAssetResponseForAsset}; \ No newline at end of file