Skip to content

Operations #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
fn main() {
}
fn main() {}
18 changes: 18 additions & 0 deletions src/claimable_balances/single_claimable_balance_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,21 @@ impl Request for SingleClaimableBalanceRequest<ClaimableBalanceId> {
)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_single_claimable_balance_request() {
let request =
SingleClaimableBalanceRequest::new().set_claimable_balance_id("00000000".to_string());

assert_eq!(request.get_query_parameters(), "00000000".to_string());

assert_eq!(
request.build_url("https://horizon-testnet.stellar.org"),
"https://horizon-testnet.stellar.org/claimable_balances/00000000".to_string()
);
}
}
9 changes: 2 additions & 7 deletions src/effects/all_effects_request.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{models::*, BuildQueryParametersExt};


/// Represents a request to fetch effect data from the Stellar Horizon API.
///
/// `AllEffectsRequest` is a struct used to construct queries for retrieving information about effects
Expand Down Expand Up @@ -91,7 +90,6 @@ impl AllEffectsRequest {
}
}


impl Request for AllEffectsRequest {
fn get_query_parameters(&self) -> String {
vec![
Expand All @@ -112,7 +110,6 @@ impl Request for AllEffectsRequest {
}
}


#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -121,8 +118,7 @@ mod tests {
fn test_all_effects_request_set_limit() {
let invalid_limit: u8 = 255;

let request = AllEffectsRequest::new()
.set_limit(invalid_limit);
let request = AllEffectsRequest::new().set_limit(invalid_limit);

assert!(request.is_err());
}
Expand All @@ -131,8 +127,7 @@ mod tests {
fn test_all_effects_request_set_cursor() {
let invalid_cursor = 0;

let request = AllEffectsRequest::new()
.set_cursor(invalid_cursor);
let request = AllEffectsRequest::new().set_cursor(invalid_cursor);

assert!(request.is_err());
}
Expand Down
19 changes: 10 additions & 9 deletions src/effects/effects_for_account_request.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{models::{Order, Request}, BuildQueryParametersExt};
use crate::{
models::{Order, Request},
BuildQueryParametersExt,
};

/// Represents the request to fetch effects for a specific account from the Horizon API.
///
Expand Down Expand Up @@ -56,10 +59,10 @@ impl EffectsForAccountRequest {
}

/// Sets the account id for the request.
///
///
/// # Arguments
/// * `account_id` - A `String` value representing the account id.
///
///
pub fn set_account_id(self, account_id: String) -> EffectsForAccountRequest {
EffectsForAccountRequest {
account_id: Some(account_id),
Expand Down Expand Up @@ -160,13 +163,12 @@ mod tests {
"https://horizon-testnet.stellar.org/effects?account=GBL3QJ2MB3KJ7YV7YVXJ5ZL5V6Z5ZL5V6Z5ZL5V6Z5ZL5V6Z5ZL5V6Z&cursor=1&limit=10&order=desc"
);
}

#[test]
fn test_effects_for_account_request_set_limit() {
let invalid_limit: u8 = 255;

let request = EffectsForAccountRequest::new()
.set_limit(invalid_limit);
let request = EffectsForAccountRequest::new().set_limit(invalid_limit);

assert!(request.is_err());
}
Expand All @@ -175,9 +177,8 @@ mod tests {
fn test_effects_for_account_request_set_cursor() {
let invalid_cursor = 0;

let request = EffectsForAccountRequest::new()
.set_cursor(invalid_cursor);
let request = EffectsForAccountRequest::new().set_cursor(invalid_cursor);

assert!(request.is_err());
}
}
}
30 changes: 18 additions & 12 deletions src/effects/effects_for_ledger_request.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{models::{Order, Request}, BuildQueryParametersExt};
use crate::{
models::{Order, Request},
BuildQueryParametersExt,
};

/// Represents a request to fetch effects associated with a specific ledger from the Stellar Horizon API.
///
Expand Down Expand Up @@ -43,10 +46,10 @@ impl EffectsForLedgerRequest {
}

/// Sets the ledger sequence for the request.
///
///
/// # Arguments
/// * `sequence` - A `String` value representing the ledger sequence.
///
///
pub fn set_sequence(self, sequence: u32) -> EffectsForLedgerRequest {
EffectsForLedgerRequest {
sequence: Some(sequence),
Expand Down Expand Up @@ -111,7 +114,10 @@ impl Request for EffectsForLedgerRequest {

fn build_url(&self, base_url: &str) -> String {
// Extract the sequence as a string if set
let seq = self.sequence.as_ref().map_or(String::new(), |s| s.to_string());
let seq = self
.sequence
.as_ref()
.map_or(String::new(), |s| s.to_string());

format!(
"{}/ledgers/{}/{}{}",
Expand All @@ -123,7 +129,6 @@ impl Request for EffectsForLedgerRequest {
}
}


#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -132,23 +137,25 @@ mod tests {
fn test_effects_for_ledger_request_build_url() {
let sequence: u32 = 125;

let request = EffectsForLedgerRequest::new()
.set_sequence(sequence);
let request = EffectsForLedgerRequest::new().set_sequence(sequence);

let url = request.build_url("https://horizon-testnet.stellar.org");

assert_eq!(
url,
format!("https://horizon-testnet.stellar.org/ledgers/{}/{}", sequence, crate::effects::EFFECTS_PATH)
format!(
"https://horizon-testnet.stellar.org/ledgers/{}/{}",
sequence,
crate::effects::EFFECTS_PATH
)
);
}

#[test]
fn test_effects_for_ledger_request_set_limit() {
let invalid_limit: u8 = 255;

let request = EffectsForLedgerRequest::new()
.set_limit(invalid_limit);
let request = EffectsForLedgerRequest::new().set_limit(invalid_limit);

assert!(request.is_err());
}
Expand All @@ -157,8 +164,7 @@ mod tests {
fn test_effects_for_ledger_request_set_cursor() {
let invalid_cursor = 0;

let request = EffectsForLedgerRequest::new()
.set_cursor(invalid_cursor);
let request = EffectsForLedgerRequest::new().set_cursor(invalid_cursor);

assert!(request.is_err());
}
Expand Down
2 changes: 1 addition & 1 deletion src/effects/effects_for_liquidity_pools_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::BuildQueryParametersExt;
/// Instances of `EffectsForLiquidityPoolRequest` are created and optionally configured using the builder pattern.
/// Once the desired parameters are set, the request can be passed to the Horizon client to fetch
/// effect data.
///
///
/// # Fields
/// * `liquidity_pool_id` - The liquidity pool id.
/// * `cursor` - A pointer to a specific location in a collection of responses, derived from the
Expand Down
159 changes: 159 additions & 0 deletions src/horizon_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ use crate::{
SingleLiquidityPoolRequest,
}},
models::{Request, Response},
operations::{
operations_for_account_request::OperationsForAccountRequest,
prelude::{AllOperationsRequest, OperationResponse, OperationsForLedgerRequest},
response::Operation,
single_operation_request::{OperationId, SingleOperationRequest},
},
};
use reqwest;
use url::Url;
Expand Down Expand Up @@ -778,6 +784,159 @@ impl HorizonClient {
self.get::<FeeStatsResponse>(request).await
}

/// Retrieves a list of all operations from the Horizon server.
///
/// This asynchronous method fetches a list of all operations from the Horizon server.
/// It requires an [`AllOperationsRequest`] to specify the optional query parameters.
///
/// # Arguments
/// * `request` - A reference to an [`AllOperationsRequest`] instance, containing the
/// parameters for the operations request.
///
/// # Returns
///
/// On successful execution, returns a `Result` containing an [`OperationResponse`], which includes
/// the list of all operations obtained from the Horizon server. If the request fails, it returns an error within `Result`.
///
/// # Usage
/// To use this method, create an instance of [`AllOperationsRequest`] and set any desired
/// filters or parameters.
///
/// ```
/// # use stellar_rs::operations::prelude::*;
/// # use stellar_rs::models::Request;
/// # use stellar_rs::horizon_client::HorizonClient;
/// #
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// # let base_url = "https://horizon-testnet.stellar.org".to_string();
/// # let horizon_client = HorizonClient::new(base_url)
/// # .expect("Failed to create Horizon Client");
/// let request = AllOperationsRequest::new()
/// .set_limit(2).unwrap();
///
/// let response = horizon_client.get_all_operations(&request).await;
///
/// // Access the operations
/// if let Ok(operations_response) = response {
/// for operation in operations_response.embedded().records() {
/// println!("Operation ID: {}", operation.id());
/// // Further processing...
/// }
/// }
/// # Ok({})
/// # }
/// ```
///
pub async fn get_all_operations(
&self,
request: &AllOperationsRequest,
) -> Result<OperationResponse, String> {
self.get::<OperationResponse>(request).await
}

/// Retrieves detailed information for a specific operation from the Horizon server.
///
/// This asynchronous method fetches details of a single operation from the Horizon server.
/// It requires a [`SingleOperationRequest`] parameterized with `OperationId`, which includes the unique identifier
/// of the operation to be retrieved.
///
/// # Arguments
/// * `request` - A reference to a [`SingleOperationRequest`] instance containing the unique ID of the operation to be fetched.
///
/// # Returns
///
/// Returns a `Result` containing an [`Operation`], which includes detailed information about the requested operation.
/// If the request fails, it returns an error encapsulated within `Result`.
///
/// # Usage
/// To use this method, create an instance of [`SingleOperationRequest`] and set the unique ID of the operation to be queried.
///
/// ```
/// # use stellar_rs::operations::prelude::*;
/// # use stellar_rs::models::Request;
/// # use stellar_rs::horizon_client::HorizonClient;
/// #
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// # let base_url = "https://horizon-testnet.stellar.org".to_string();
/// # let horizon_client = HorizonClient::new(base_url)
/// # .expect("Failed to create Horizon Client");
/// let request = SingleOperationRequest::new()
/// .set_operation_id("459561504769".to_string());
///
/// let response = horizon_client.get_single_operation(&request).await;
///
/// if let Ok(operation) = response {
/// println!("Operation ID: {}", operation.id());
/// // Additional processing...
/// }
/// # Ok({})
/// # }
/// ```
///
pub async fn get_single_operation(
&self,
request: &SingleOperationRequest<OperationId>,
) -> Result<Operation, String> {
self.get::<Operation>(request).await
}

/// Retrieves a list of all operations for an account from the Horizon server.
///
/// This asynchronous method fetches a list of all operations for an account from the Horizon server.
/// It requires an [`OperationsForAccountRequest`] to specify the optional query parameters.
///
/// # Arguments
/// * `request` - A reference to an [`OperationsForAccountRequest`] instance, containing the
/// parameters for the operations for account request.
///
/// # Returns
///
/// On successful execution, returns a `Result` containing a [`OperationsForAccountRequest`], which includes
/// the list of all operations obtained from the Horizon server. If the request fails, it returns an error within `Result`.
///
/// # Usage
/// To use this method, create an instance of [`OperationsForAccountRequest`] and set any desired
/// filters or parameters.
///
/// ```
/// # use stellar_rs::operations::prelude::*;
/// # use stellar_rs::models::Request;
/// # use stellar_rs::horizon_client::HorizonClient;
/// #
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// # let base_url = "https://horizon-testnet.stellar.org".to_string();
/// # let horizon_client = HorizonClient::new(base_url)
/// # .expect("Failed to create Horizon Client");
/// let request = OperationsForAccountRequest::new()
/// .set_limit(2).unwrap();
///
/// let response = horizon_client.get_operation_for_account(&request).await;
///
/// // Access the payments
/// if let Ok(operations_for_account_response) = response {
/// for operation in operations_for_account_response.embedded().records() {
/// println!("operation ID: {}", operation.id());
/// // Further processing...
/// }
/// }
/// # Ok({})
/// # }
/// ```
///
pub async fn get_operation_for_account(
&self,
request: &OperationsForAccountRequest,
) -> Result<OperationResponse, String> {
self.get::<OperationResponse>(request).await
}

pub async fn get_operations_for_ledger(
&self,
request: &OperationsForLedgerRequest,
) -> Result<OperationResponse, String> {
self.get::<OperationResponse>(request).await
}

/// Sends a GET request to the Horizon server and retrieves a specified response type.
///
/// This internal asynchronous method is designed to handle various GET requests to the
Expand Down
1 change: 0 additions & 1 deletion src/ledgers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,3 @@ pub mod tests {
}
}
}

1 change: 0 additions & 1 deletion src/ledgers/single_ledger_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,3 @@ impl Request for SingleLedgerRequest<Sequence> {
)
}
}

Loading