Skip to content

Commit

Permalink
Trying to Streamline POST Reqs
Browse files Browse the repository at this point in the history
  • Loading branch information
0xIchigo committed Apr 27, 2024
1 parent 390dcd7 commit d409d7a
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 18 deletions.
7 changes: 7 additions & 0 deletions src/request_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ impl RequestHandler {
}

let response: Response = self.send_request(request_builder).await?;

print!("{:?}", response);

self.handle_response(response).await
}

Expand All @@ -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::<T>(&body_text) {
Ok(data) => Ok(data),
Expand Down
35 changes: 25 additions & 10 deletions src/rpc_client.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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<ApiResponse> {
/// Streamlines an RPC POST request
pub async fn post_rpc_request<R, T>(&self, method: String, request: R) -> Result<T>
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<R> = RpcRequest::new(method, request);


print!("{:?}", rpc_request);

let rpc_response: RpcResponse<T> = 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<ApiResponse> {
self.post_rpc_request("getAsset".to_string(), request).await
}

/// Gets a list of assets owned by a given address
Expand Down
41 changes: 33 additions & 8 deletions src/types/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,33 @@ impl HeliusEndpoints {
}
}

#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
pub struct RpcRequest<T> {
pub jsonrpc: String,
pub id: String,
pub method: String,
pub parameters: T,
}

impl<T> RpcRequest<T> {
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<T> {
pub jsonrpc: String,
pub id: String,
pub result: T,

}

#[derive(Serialize, Deserialize, Default)]
pub struct AssetsByOwnerRequest {
#[serde(rename = "ownerAddress")]
Expand Down Expand Up @@ -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<DisplayOptions>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct DisplayOptions {
#[serde(rename = "showUnverifiedCollections")]
pub show_unverified_collections: Option<bool>,
pub show_unverified_collections: bool,
#[serde(rename = "showCollectionMetadata")]
pub show_collection_metadata: Option<bool>,
#[serde(rename = "showGrandTotal")]
pub show_grand_total: Option<bool>,
pub show_collection_metadata: bool,
#[serde(rename = "showFungible")]
pub show_fungible: Option<bool>,
pub show_fungible: bool,
#[serde(rename = "showInscription")]
pub show_inscription: Option<bool>,
pub show_inscription: bool,
}

#[derive(Serialize, Deserialize, Debug)]
Expand All @@ -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),
}

Expand Down
4 changes: 4 additions & 0 deletions tests/rpc/test_get_asset.rs
Original file line number Diff line number Diff line change
@@ -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};

0 comments on commit d409d7a

Please sign in to comment.