Skip to content
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

54 implement payments endpoint #97

Merged
merged 14 commits into from
Jul 26, 2024
260 changes: 235 additions & 25 deletions stellar_rust_sdk/src/horizon_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use crate::{
trade_aggregations::prelude::*,
transactions::prelude::*,
trades::prelude::*,
payments::prelude::*
};
use reqwest;
use url::Url;
Expand Down Expand Up @@ -1093,26 +1094,26 @@ impl HorizonClient {
}

/// Retrieves a list of order book details from the Horizon server.
///
///
/// This asynchronous method fetches a list of order book details from the Horizon server.
/// It requires a [`DetailsRequest`] to specify the parameters for the order book details request.
///
///
/// # Arguments
/// * `request` - A reference to a [`DetailsRequest`] instance, containing the parameters for the order book details request.
///
///
/// # Returns
///
///
/// On successful execution, returns a `Result` containing a [`DetailsResponse`], which includes the list of order book details obtained from the Horizon server.
/// If the request fails, it returns an error within `Result`.
///
///
/// # Usage
/// To use this method, create an instance of [`DetailsRequest`] and set any desired filters or parameters.
///
///
/// ```
/// # use stellar_rs::order_book::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)
Expand All @@ -1126,14 +1127,14 @@ impl HorizonClient {
/// # .to_string(),
/// # }))
/// # .unwrap();
///
///
/// let response = horizon_client.get_order_book_details(&details_request).await;
///
///
/// assert!(response.is_ok());
/// # Ok({})
/// # }
/// ```
///
///
pub async fn get_order_book_details(
&self,
request: &DetailsRequest<SellingAsset, BuyingAsset>,
Expand All @@ -1142,29 +1143,29 @@ impl HorizonClient {
}

/// Retrieves a list of trade aggregations from the Horizon server.
///
///
/// This asynchronous method fetches a list of trade aggregations from the Horizon server.
/// It requires a [`TradeAggregationsRequest`] to specify the parameters for the trade aggregations request.
///
///
/// # Arguments
/// * `request` - A reference to a [`TradeAggregationsRequest`] instance, containing the parameters for the order book details request.
///
///
/// # Returns
///
///
/// On successful execution, returns a `Result` containing a [`AllTradeAggregationsResponse`], which includes the list of order book details obtained from the Horizon server.
/// If the request fails, it returns an error within `Result`.
///
///
/// # Usage
/// To use this method, create an instance of [`TradeAggregationsRequest`] and set any desired filters or parameters.
///
///
/// ```rust
/// use stellar_rs::horizon_client::HorizonClient;
/// use stellar_rs::trade_aggregations::prelude::*;
/// use stellar_rs::models::Request;
///
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let horizon_client = HorizonClient::new("https://horizon-testnet.stellar.org".to_string())?;
///
///
/// // Example: Fetching trade aggregations
/// let request = TradeAggregationsRequest::new()
/// .set_base_asset(AssetType::Native).unwrap()
Expand All @@ -1174,12 +1175,12 @@ impl HorizonClient {
/// })).unwrap()
/// .set_resolution(Resolution(ResolutionData::Duration604800000)).unwrap();
/// let response = horizon_client.get_trade_aggregations(&request).await?;
///
///
/// // Process the response...
/// # Ok(())
/// # }
/// ```
///
///
pub async fn get_trade_aggregations(
&self,
request: &TradeAggregationsRequest<BaseAsset, CounterAsset, Resolution>,
Expand Down Expand Up @@ -1388,7 +1389,7 @@ impl HorizonClient {
) -> Result<AllTradesResponse, String> {
self.get::<AllTradesResponse>(request).await
}

/// Retrieves a list of all operations for a specific liquidity pool from the Horizon server.
///
/// This asynchronous method fetches a list of all operations for a specific liquidity pool from the Horizon server.
Expand Down Expand Up @@ -1622,7 +1623,7 @@ impl HorizonClient {
/// .unwrap();
///
/// let response = horizon_client.get_single_transaction(&request).await;
///
///
/// // Access the details of the claimable balance
/// if let Ok(transaction_response) = response {
/// println!("Created at: {}", transaction_response.created_at());
Expand All @@ -1640,7 +1641,6 @@ impl HorizonClient {
self.get::<TransactionResponse>(request).await
}


/// Retrieves a list of all transactions from the Horizon server.
///
/// This asynchronous method fetches a list of all transactions from the Horizon server.
Expand Down Expand Up @@ -1692,7 +1692,7 @@ impl HorizonClient {
) -> Result<AllTransactionsResponse, String> {
self.get::<AllTransactionsResponse>(request).await
}

/// 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
Expand Down Expand Up @@ -1745,7 +1745,7 @@ impl HorizonClient {
) -> Result<AllTransactionsResponse, String> {
self.get::<AllTransactionsResponse>(request).await
}

/// 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
Expand Down Expand Up @@ -1851,6 +1851,216 @@ impl HorizonClient {
) -> Result<AllTransactionsResponse, String> {
self.get::<AllTransactionsResponse>(request).await
}

/// Retrieves a list of all payments from the Horizon server.
///
/// This asynchronous method fetches a list of all payments from the Horizon server.
/// It requires an [`AllPaymentsRequest`] to specify the optional query parameters.
///
/// # Arguments
/// * `request` - A reference to an [`AllPaymentsRequest`] instance, containing the
/// parameters for the payments request.
///
/// # Returns
///
/// On successful execution, returns a `Result` containing a [`PaymentsResponse`], which includes
/// the list of all payments obtained from the Horizon server. If the request fails, it returns an error within `Result`.
///
/// # Usage
/// To use this method, create an instance of [`AllPaymentsRequest`] and set any desired
/// filters or parameters.
///
/// ```
/// # use stellar_rs::payments::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 = AllPaymentsRequest::new()
/// .set_limit(2).unwrap();
///
/// let response = horizon_client.get_all_payments(&request).await;
///
/// // Access the payments
/// if let Ok(payments_response) = response {
/// for payment in payments_response.embedded().records() {
/// println!("Payment ID: {}", payment.id());
/// // Further processing...
/// }
/// }
/// # Ok({})
/// # }
/// ```
///
pub async fn get_all_payments(
&self,
request: &AllPaymentsRequest,
) -> Result<PaymentsResponse, String> {
self.get::<PaymentsResponse>(request).await
}

/// Retrieves a list of all payments for an account from the Horizon server.
///
/// This asynchronous method fetches a list of all payments for an account from the Horizon server.
/// It requires an [`PaymentsForAccountRequest`] to specify the optional query parameters.
///
/// # Arguments
/// * `request` - A reference to an [`PaymentsForAccountRequest`] instance, containing the
/// parameters for the payments for account request.
///
/// # Returns
///
/// On successful execution, returns a `Result` containing a [`PaymentsResponse`], which includes
/// the list of all payments obtained from the Horizon server. If the request fails, it returns an error within `Result`.
///
/// # Usage
/// To use this method, create an instance of [`PaymentsForAccountRequest`] and set any desired
/// filters or parameters.
///
/// ```
/// # use stellar_rs::payments::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 = PaymentsForAccountRequest::new()
/// .set_limit(2).unwrap();
///
/// let response = horizon_client.get_payments_for_account(&request).await;
///
/// // Access the payments
/// if let Ok(payments_response) = response {
/// for payment in payments_response.embedded().records() {
/// println!("Payment ID: {}", payment.id());
/// // Further processing...
/// }
/// }
/// # Ok({})
/// # }
/// ```
///
pub async fn get_payments_for_account(
&self,
request: &PaymentsForAccountRequest,
) -> Result<PaymentsResponse, String> {
self.get::<PaymentsResponse>(request).await
}

/// Retrieves a list of all payments for a specific ledger from the Horizon server.
///
/// This asynchronous method fetches a list of all payments for a specific ledger from the Horizon server.
/// It requires an [`PaymentsForLedgerRequest`] to specify the ledger sequence number and optional query parameters.
///
/// # Arguments
/// * `request` - A reference to an [`PaymentsForLedgerRequest`] instance, containing the
/// parameters for the payments for ledger request.
///
/// # Returns
///
/// On successful execution, returns a `Result` containing a [`PaymentsResponse`], which includes
/// the list of all payments obtained from the Horizon server. If the request fails, it returns an error within `Result`.
///
/// # Usage
/// To use this method, create an instance of [`PaymentsForLedgerRequest`] and set the ledger sequence number and any desired
/// filters or parameters.
///
/// ```
/// # use stellar_rs::payments::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 = PaymentsForLedgerRequest::new()
/// .set_ledger_sequence("48483".to_string());
///
/// let response = horizon_client.get_payments_for_ledger(&request).await;
///
/// // Access the payments
/// if let Ok(payments_response) = response {
/// for payment in payments_response.embedded().records() {
/// println!("Payment ID: {}", payment.id());
///
/// // Further processing...
/// }
/// }
/// # Ok({})
/// # }
/// ```
///
pub async fn get_payments_for_ledger(
&self,
request: &PaymentsForLedgerRequest,
) -> Result<PaymentsResponse, String> {
self.get::<PaymentsResponse>(request).await
}

/// Retrieves a list of all payments for a specific transaction from the Horizon server.
///
/// This asynchronous method fetches a list of all payments for a specific transaction from the Horizon server.
/// It requires an [`PaymentsForTransactionRequest`] to specify the transaction hash and optional query parameters.
///
/// # Arguments
/// * `request` - A reference to an [`PaymentsForTransactionRequest`] instance, containing the
/// parameters for the payments for transaction request.
///
/// # Returns
///
/// On successful execution, returns a `Result` containing a [`PaymentsResponse`], which includes
/// the list of all payments obtained from the Horizon server. If the request fails, it returns an error within `Result`.
///
/// # Usage
/// To use this method, create an instance of [`PaymentsForTransactionRequest`] and set the transaction hash and any desired
/// filters or parameters.
///
/// ```
/// # use stellar_rs::payments::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 = PaymentsForTransactionRequest::new()
/// .set_transaction_hash("be0d59c8706e8fd525d2ab10910a55ec57323663858c65b330a3f93afb13ab0f".to_string());
///
/// let response = horizon_client.get_payments_for_transaction(&request).await;
///
/// // Access the payments
/// if let Ok(payments_response) = response {
/// for payment in payments_response.embedded().records() {
/// println!("Payment ID: {}", payment.id());
///
/// // Further processing...
/// }
/// }
/// # Ok({})
/// # }
/// ```
///
pub async fn get_payments_for_transaction(
&self,
request: &PaymentsForTransactionRequest,
) -> Result<PaymentsResponse, String> {
self.get::<PaymentsResponse>(request).await
}
}

/// Handles the response received from an HTTP request made to the Horizon server.
Expand Down
Loading