-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
107 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,34 @@ | ||
#![allow(dead_code)] | ||
use std::sync::Arc; | ||
|
||
use crate::config::Config; | ||
use crate::error::Result; | ||
use crate::rpc_client::RpcClient; | ||
use crate::types::Cluster; | ||
|
||
use reqwest::Client; | ||
|
||
pub struct Helius { | ||
pub config: Config, | ||
pub client: Client, | ||
pub rpc_client: Arc<RpcClient>, | ||
} | ||
|
||
impl Helius { | ||
pub fn new(api_key: &str, cluster: Cluster) -> Result<Self> { | ||
let config: Config = Config::new(api_key, cluster)?; | ||
let client: Client = Client::new(); | ||
let rpc_client: RpcClient = Arc::new(RpcClient::new(Arc::new(client.clone()), config.clone())); | ||
|
||
Ok(Helius { config, client }) | ||
Ok(Helius { | ||
config, | ||
client, | ||
rpc_client, | ||
}) | ||
} | ||
|
||
/// Provides a thread-safe way to access RPC functionalities | ||
pub fn rpc(&self) -> Arc<RpcClient> { | ||
self.rpc_client.clone() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use crate::error::{HeliusError, Result}; | ||
use reqwest::{Client, Method, RequestBuilder, Response, StatusCode, Url}; | ||
use serde::{Deserialize, Serialize}; | ||
use std::fmt::Debug; | ||
use std::sync::Arc; | ||
|
||
#[derive(Clone)] | ||
pub struct RequestHandler { | ||
pub http_client: Arc<Client>, | ||
} | ||
|
||
impl RequestHandler { | ||
pub fn new(client: Arc<Client>) -> Result<Self> { | ||
Ok(Self { http_client: client }) | ||
} | ||
|
||
async fn send_request(&self, request_builder: RequestBuilder) -> Result<Response> { | ||
let response: Response = request_builder.send().await.map_err(|e| HeliusError::Network(e))?; | ||
Ok(response) | ||
} | ||
|
||
pub async fn send<R, T>(&self, method: Method, url: Url, body: Option<&R>) -> Result<T> | ||
where | ||
R: Serialize + ?Sized + Send + Sync + Debug, | ||
T: for<'de> Deserialize<'de> + Default, | ||
{ | ||
let mut request_builder: RequestBuilder = self.http_client.request(method, url.clone()); | ||
|
||
if let Some(body) = body { | ||
request_builder = request_builder.json(body); | ||
} | ||
|
||
let response: Response = self.send_request(request_builder).await?; | ||
let path: String = url.path().to_string(); | ||
self.handle_response(path, response).await | ||
} | ||
|
||
async fn handle_response<T: for<'de> Deserialize<'de>>(&self, path: String, response: Response) -> Result<T> { | ||
let status: StatusCode = response.status(); | ||
|
||
if status == StatusCode::OK || status == StatusCode::CREATED { | ||
response.json::<T>().await.map_err(HeliusError::SerdeJson) | ||
} else { | ||
let error_text: String = response.text().await.unwrap_or_default(); | ||
Err(HeliusError::from_response_status(status, path, error_text)) | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use std::sync::Arc; | ||
|
||
use crate::config::Config; | ||
use crate::error::Result; | ||
use crate::request_handler::RequestHandler; | ||
|
||
use reqwest::Client; | ||
|
||
pub struct RpcClient { | ||
pub handler: RequestHandler, | ||
pub config: Arc<Config>, | ||
} | ||
|
||
impl RpcClient { | ||
pub fn new(client: Arc<Client>, config: Arc<Config>) -> Result<Self> { | ||
let handler: RequestHandler = RequestHandler::new(client)?; | ||
Ok(RpcClient { handler, config }) | ||
} | ||
} |