Skip to content

Commit

Permalink
fix: created new ApiResponse to handle all calls
Browse files Browse the repository at this point in the history
  • Loading branch information
anamansari062 committed Apr 24, 2024
1 parent b6f1638 commit a7198c6
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
27 changes: 27 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use helius_sdk::config::Config;
use helius_sdk::error::HeliusError;
use helius_sdk::rpc_client::RpcClient;
use helius_sdk::types::types::ApiResponse;
use helius_sdk::types::{AssetsByOwnerRequest, Cluster};
use std::sync::Arc;

#[tokio::main]
async fn main() -> Result<(), HeliusError> {
let api_key: &str = "0abc88ab-a583-4f9c-9265-b8f8f16c0719";
let cluster: Cluster = Cluster::MainnetBeta;

let config: Config = Config::new(api_key, cluster)?;
let client: reqwest::Client = reqwest::Client::new();
let rpc_client = RpcClient::new(Arc::new(client), Arc::new(config))?;

let request = AssetsByOwnerRequest {
owner_address: "GNPwr9fk9RJbfy9nSKbNiz5NPfc69KVwnizverx6fNze".to_string(),
page: Some(1),
..Default::default()
};

let response: ApiResponse = rpc_client.get_assets_by_owner(request).await?;
println!("Assets: {:?}", response);

Ok(())
}
5 changes: 3 additions & 2 deletions src/rpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use std::sync::Arc;
use crate::config::Config;
use crate::error::Result;
use crate::request_handler::RequestHandler;
use crate::types::{AssetsByOwnerRequest, GetAssetResponseList};
use crate::types::types::ApiResponse;
use crate::types::AssetsByOwnerRequest;

use reqwest::{Client, Method, Url};
use serde_json::{json, Value};
Expand All @@ -19,7 +20,7 @@ impl RpcClient {
Ok(RpcClient { handler, config })
}

pub async fn get_assets_by_owner(&self, request: AssetsByOwnerRequest) -> Result<GetAssetResponseList> {
pub async fn get_assets_by_owner(&self, request: AssetsByOwnerRequest) -> Result<ApiResponse> {
let url: String = format!("{}?api-key={}", self.config.endpoints.rpc, self.config.api_key);
let url: Url = Url::parse(&url).expect("Failed to parse URL");

Expand Down
22 changes: 18 additions & 4 deletions src/types/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,23 @@ pub struct AssetSortingRequest {
pub sort_direction: AssetSortDirection,
}

#[derive(Serialize, Deserialize, Debug, Default)]
pub struct ApiResponse {
pub jsonrpc: String,
pub result: ResponseType, // Serde will automatically deserialize the response into the appropriate type
pub id: u8,
}

#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(untagged)]
pub enum ResponseType {
#[default]
DefaultResponse, // This is a placeholder for the default response type. TODO: Replace this an appropriate type
GetAssetResponseList(GetAssetResponseList),
}

#[derive(Serialize, Deserialize, Debug, Default)]
pub struct GetAssetResponseList {
#[serde(rename = "grandTotal")]
pub grand_total: Option<i32>,
pub total: Option<i32>,
pub limit: Option<i32>,
pub page: Option<i32>,
Expand All @@ -107,6 +120,7 @@ pub struct GetAssetResponse {

#[derive(Serialize, Deserialize, Debug)]
pub struct Content {
#[serde(rename = "$schema")]
pub schema: String,
pub json_uri: String,
pub files: Option<Vec<File>>,
Expand All @@ -131,9 +145,9 @@ pub struct FileQuality {
#[derive(Serialize, Deserialize, Debug)]
pub struct Metadata {
pub attributes: Option<Vec<Attribute>>,
pub description: String,
pub description: Option<String>,
pub name: String,
pub symbole: String,
pub symbol: String,
}

#[derive(Serialize, Deserialize, Debug)]
Expand Down

0 comments on commit a7198c6

Please sign in to comment.