Skip to content

Commit

Permalink
Add documentation and clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-pease committed Jul 19, 2024
1 parent c78fd9d commit 7342ead
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 25 deletions.
141 changes: 138 additions & 3 deletions stellar_rust_sdk/src/horizon_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1646,23 +1646,158 @@ impl HorizonClient {
self.get::<AllTransactionsResponse>(request).await
}

// TODO: Documentation
/// Retrieves a list of all transactions for a given account from the Horizon server.
///
/// This asynchronous method fetches a list of all transactions for a given account from
/// the Horizon server. It requires an [`TransactionsForAccountRequest`] to specify the optional query parameters.
///
/// # Arguments
/// * `request` - A reference to an [`TransactionsForAccountRequest`] instance, containing the
/// parameters for the transactions request.
///
/// # Returns
///
/// On successful execution, returns a `Result` containing an [`AllTransactionsResponse`], which includes
/// the list of all transactions obtained from the Horizon server. If the request fails, it returns an error within `Result`.
///
/// # Usage
/// To use this method, create an instance of [`TransactionsForAccountRequest`] and set any desired
/// filters or parameters.
///
/// ```
/// # use stellar_rs::transactions::prelude::*;
/// # use stellar_rs::models::Request;
/// # use stellar_rs::horizon_client::HorizonClient;
/// # use stellar_rust_sdk_derive::Pagination;
/// # use stellar_rs::Paginatable;
/// #
/// # 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 = TransactionsForAccountRequest::new()
/// .set_account_id("GBRPYHIL2CI3FNQ4BXLFMNDLFJUNPU2HY3ZMFSHONUCEOASW7QC7OX2H".to_string()).unwrap()
/// .set_include_failed(true).unwrap();
///
/// let response = horizon_client.get_transactions_for_account(&request).await;
///
/// // Access the transactions
/// if let Ok(transactions_response) = response {
/// for transaction in transactions_response.embedded().records() {
/// println!("Transaction ID: {}", transaction.id());
/// // Further processing...
/// }
/// }
/// # Ok({})
/// # }
/// ```
///
pub async fn get_transactions_for_account(
&self,
request: &TransactionsForAccountRequest<TransactionsAccountId>,
) -> Result<AllTransactionsResponse, String> {
self.get::<AllTransactionsResponse>(request).await
}

// TODO: Documentation
/// Retrieves a list of all transactions in a given ledger from the Horizon server.
///
/// This asynchronous method fetches a list of all transactions in a given ledger from
/// the Horizon server. It requires an [`TransactionsForLedgerRequest`] to specify the optional query parameters.
///
/// # Arguments
/// * `request` - A reference to an [`TransactionsForLedgerRequest`] instance, containing the
/// parameters for the transactions request.
///
/// # Returns
///
/// On successful execution, returns a `Result` containing an [`AllTransactionsResponse`], which includes
/// the list of all transactions obtained from the Horizon server. If the request fails, it returns an error within `Result`.
///
/// # Usage
/// To use this method, create an instance of [`TransactionsForLedgerRequest`] and set any desired
/// filters or parameters.
///
/// ```
/// # use stellar_rs::transactions::prelude::*;
/// # use stellar_rs::models::Request;
/// # use stellar_rs::horizon_client::HorizonClient;
/// # use stellar_rust_sdk_derive::Pagination;
/// # use stellar_rs::Paginatable;
/// #
/// # 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 = TransactionsForLedgerRequest::new()
/// .set_ledger_sequence("539".to_string()).unwrap()
/// .set_include_failed(true).unwrap();
///
/// let response = horizon_client.get_transactions_for_ledger(&request).await;
///
/// // Access the transactions
/// if let Ok(transactions_response) = response {
/// for transaction in transactions_response.embedded().records() {
/// println!("Transaction ID: {}", transaction.id());
/// // Further processing...
/// }
/// }
/// # Ok({})
/// # }
/// ```
///
pub async fn get_transactions_for_ledger(
&self,
request: &TransactionsForLedgerRequest<TransactionsLedgerId>,
) -> Result<AllTransactionsResponse, String> {
self.get::<AllTransactionsResponse>(request).await
}

// TODO: Documentation
/// Retrieves a list of all transactions referencing a given liquidity pool from the Horizon server.
///
/// This asynchronous method fetches a list of all transactions referencing a given liquidity pool from
/// the Horizon server. It requires an [`TransactionsForLiquidityPoolRequest`] to specify the optional query parameters.
///
/// # Arguments
/// * `request` - A reference to an [`TransactionsForLiquidityPoolRequest`] instance, containing the
/// parameters for the transactions request.
///
/// # Returns
///
/// On successful execution, returns a `Result` containing an [`AllTransactionsResponse`], which includes
/// the list of all transactions obtained from the Horizon server. If the request fails, it returns an error within `Result`.
///
/// # Usage
/// To use this method, create an instance of [`TransactionsForLiquidityPoolRequest`] and set any desired
/// filters or parameters.
///
/// ```
/// # use stellar_rs::transactions::prelude::*;
/// # use stellar_rs::models::Request;
/// # use stellar_rs::horizon_client::HorizonClient;
/// # use stellar_rust_sdk_derive::Pagination;
/// # use stellar_rs::Paginatable;
/// #
/// # 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 = TransactionsForLiquidityPoolRequest::new()
/// .set_liquidity_pool_id("0066b15f5d0dc0be771209c33f3e4126383e58183a598eae8b3813024c6a6d10".to_string()).unwrap()
/// .set_include_failed(true).unwrap();
///
/// let response = horizon_client.get_transactions_for_liquidity_pool(&request).await;
///
/// // Access the transactions
/// if let Ok(transactions_response) = response {
/// for transaction in transactions_response.embedded().records() {
/// println!("Transaction ID: {}", transaction.id());
/// // Further processing...
/// }
/// }
/// # Ok({})
/// # }
/// ```
///
pub async fn get_transactions_for_liquidity_pool(
&self,
request: &TransactionsForLiquidityPoolRequest<TransactionsLiquidityPoolId>,
Expand Down
40 changes: 31 additions & 9 deletions stellar_rust_sdk/src/transactions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,35 @@ pub mod single_transaction_request;
///
pub mod all_transactions_request;

// TODO: Documentation
/// Provides the `TransactionsForAccountRequest`.
///
/// # Usage
/// This module provides the `TransactionsForAccountRequest` struct, specifically designed for
/// constructing requests to query information about all successful transactions for a given account from the Horizon
/// server. It is tailored for use with the [`HorizonClient::get_transactions_for_account`](crate::horizon_client::HorizonClient::get_transactions_for_account)
/// method.
///
pub mod transactions_for_account_request;

// TODO: Documentation
/// Provides the `TransactionsForLedgersRequest`.
///
/// # Usage
/// This module provides the `TransactionsForLedgersRequest` struct, specifically designed for
/// constructing requests to query information about all successful transactions in a given ledger from the Horizon
/// server. It is tailored for use with the [`HorizonClient::get_transactions_for_ledger`](crate::horizon_client::HorizonClient::get_transactions_for_ledger)
/// method.
///
pub mod transactions_for_ledger_request;

// TODO: Documentation
/// Provides the `TransactionsForLiquidityPoolRequest`.
///
/// # Usage
/// This module provides the `TransactionsForLiquidityPoolRequest` struct, specifically designed for
/// constructing requests to query information about all successful transactions referencing
/// a given liquidity pool from the Horizon server.
/// It is tailored for use with the [`HorizonClient::get_transactions_for_liquidity_pool`](crate::horizon_client::HorizonClient::get_transactions_for_liquidity_pool)
/// method.
///
pub mod transactions_for_liquidity_pool_request;


Expand Down Expand Up @@ -121,7 +143,6 @@ pub mod test {

#[tokio::test]
async fn test_get_single_transaction() {

let horizon_client =
HorizonClient::new("https://horizon-testnet.stellar.org"
.to_string())
Expand Down Expand Up @@ -172,7 +193,8 @@ pub mod test {
.unwrap();

let all_transactions_request = AllTransactionsRequest::new()
.set_include_failed(true).unwrap();
.set_include_failed(true)
.unwrap();

let all_transactions_response = horizon_client
.get_all_transactions(&all_transactions_request)
Expand Down Expand Up @@ -209,14 +231,15 @@ pub mod test {

#[tokio::test]
async fn test_get_transactions_for_account() {

let horizon_client =
HorizonClient::new("https://horizon-testnet.stellar.org"
.to_string())
.unwrap();

let transactions_for_account_request = TransactionsForAccountRequest::new()
.set_account_id("GBRPYHIL2CI3FNQ4BXLFMNDLFJUNPU2HY3ZMFSHONUCEOASW7QC7OX2H".to_string())
.unwrap()
.set_include_failed(true)
.unwrap();

let transactions_for_account_response = horizon_client
Expand Down Expand Up @@ -254,8 +277,6 @@ pub mod test {

#[tokio::test]
async fn test_get_transactions_for_ledger() {
use crate::{models::*, BuildQueryParametersExt, Paginatable};

const LEDGER_SEQUENCE: &str = "539";

let horizon_client =
Expand All @@ -266,7 +287,8 @@ pub mod test {
let transactions_for_ledger_request = TransactionsForLedgerRequest::new()
.set_ledger_sequence(LEDGER_SEQUENCE.to_string())
.unwrap()
.set_include_failed(true).unwrap();
.set_include_failed(true)
.unwrap();

let transactions_for_ledger_response = horizon_client
.get_transactions_for_ledger(&transactions_for_ledger_request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use crate::{models::*, BuildQueryParametersExt};
use stellar_rust_sdk_derive::Pagination;
use crate::Paginatable;

// TODO: Documentation
/// Represents the ID of an account for which the transactions are to be retrieved.
#[derive(Default, Clone)]
pub struct TransactionsAccountId(String);

// TODO: Documentation
/// Represents the absence of an ID of an account for which the transactions are to be retrieved.
#[derive(Default, Clone)]
pub struct NoTransactionsAccountId;

Expand Down Expand Up @@ -56,6 +56,15 @@ impl TransactionsForAccountRequest<NoTransactionsAccountId> {
}

impl TransactionsForAccountRequest<TransactionsAccountId> {
/// Sets the `include_failed` field for the request. Can only be set on a request that
/// has a set account id.
///
/// # Arguments
/// * `include_failed` - A `bool` to indicate whether or not to include failed operations.
///
/// # Returns
/// A `TransactionsForAccountRequest` with the updated `include_failed` field.
///
pub fn set_include_failed(
self,
include_failed: bool,
Expand Down Expand Up @@ -84,7 +93,8 @@ impl Request for TransactionsForAccountRequest<TransactionsAccountId> {
}

fn build_url(&self, base_url: &str) -> String {
// TODO: Documentation
// This URL comprises paths and query parameters.
// Additionally, this request uses the API endpoint for `accounts`.
let account_id = &self.account_id.0;
use crate::accounts::ACCOUNTS_PATH;
format!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ use crate::{models::*, BuildQueryParametersExt};
use stellar_rust_sdk_derive::Pagination;
use crate::Paginatable;

// TODO: Documentation
/// Represents the ID of a ledger for which the transactions are to be retrieved.
#[derive(Default, Clone)]
pub struct TransactionsLedgerId(String);

// TODO: Documentation
/// Represents the absence of an ID of a ledger for which the transactions are to be retrieved.
#[derive(Default, Clone)]
pub struct NoTransactionsLedgerId;

#[derive(Default, Pagination)]
pub struct TransactionsForLedgerRequest<S> {
/// The ID of the account for which the transactions are to be retrieved.
/// The ID of the ledger for which the transactions are to be retrieved.
ledger_sequence: S,
// Indicates whether or not to include failed operations in the response.
include_failed: Option<bool>,
Expand All @@ -33,13 +33,13 @@ impl TransactionsForLedgerRequest<NoTransactionsLedgerId> {
TransactionsForLedgerRequest::default()
}

/// Sets the account ID for the request.
/// Sets the ledger ID for the request.
///
/// # Arguments
/// * `account_id` - The account ID for which the transactions are to be retrieved.
/// * `ledger_id` - The ledger ID for which the transactions are to be retrieved.
///
/// # Returns
/// A `TransactionsForAccountRequest` with the specified account ID, or an error if the account ID is invalid.
/// A `TransactionsForLedgerRequest` with the specified ledger ID.
///
pub fn set_ledger_sequence(
self,
Expand All @@ -56,6 +56,15 @@ impl TransactionsForLedgerRequest<NoTransactionsLedgerId> {
}

impl TransactionsForLedgerRequest<TransactionsLedgerId> {
/// Sets the `include_failed` field for the request. Can only be set on a request that
/// has a set ledger id.
///
/// # Arguments
/// * `include_failed` - A `bool` to indicate whether or not to include failed operations.
///
/// # Returns
/// A `TransactionsForLedgerRequest` with the updated `include_failed` field.
///
pub fn set_include_failed(
self,
include_failed: bool,
Expand Down Expand Up @@ -84,7 +93,8 @@ impl Request for TransactionsForLedgerRequest<TransactionsLedgerId> {
}

fn build_url(&self, base_url: &str) -> String {
// TODO: Documentation
// This URL comprises paths and query parameters.
// Additionally, this request uses the API endpoint for `ledgers`.
let ledger_sequence = &self.ledger_sequence.0;
use crate::ledgers::LEDGERS_PATH;
format!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use crate::{models::*, BuildQueryParametersExt};
use stellar_rust_sdk_derive::Pagination;
use crate::Paginatable;

// TODO: Documentation
/// Represents the ID of a liquidity pool for which the transactions are to be retrieved.
#[derive(Default, Clone)]
pub struct TransactionsLiquidityPoolId(String);

// TODO: Documentation
/// Represents the absence of an ID of a liquidity pool for which the transactions are to be retrieved.
#[derive(Default, Clone)]
pub struct NoTransactionsLiquidityPoolId;

Expand Down Expand Up @@ -56,6 +56,15 @@ impl TransactionsForLiquidityPoolRequest<NoTransactionsLiquidityPoolId> {
}

impl TransactionsForLiquidityPoolRequest<TransactionsLiquidityPoolId> {
/// Sets the `include_failed` field for the request. Can only be set on a request that
/// has a set liquidity pool id.
///
/// # Arguments
/// * `include_failed` - A `bool` to indicate whether or not to include failed operations.
///
/// # Returns
/// A `TransactionsForLiquidityPoolRequest` with the updated `include_failed` field.
///
pub fn set_include_failed(
self,
include_failed: bool,
Expand Down Expand Up @@ -84,7 +93,8 @@ impl Request for TransactionsForLiquidityPoolRequest<TransactionsLiquidityPoolId
}

fn build_url(&self, base_url: &str) -> String {
// TODO: Documentation
// This URL comprises paths and query parameters.
// Additionally, this request uses the API endpoint for `liquidity_pools`.
let liquidity_pool_id = &self.liquidity_pool_id.0;
use crate::liquidity_pools::LIQUIDITY_POOLS_PATH;
format!(
Expand Down

0 comments on commit 7342ead

Please sign in to comment.