Skip to content

Commit

Permalink
Add to transactions for account request
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-pease committed Jul 19, 2024
1 parent e9dd0c6 commit c78fd9d
Showing 1 changed file with 33 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::models::*;
use crate::{models::*, BuildQueryParametersExt};
use stellar_rust_sdk_derive::Pagination;
use crate::Paginatable;

Expand All @@ -14,6 +14,8 @@ pub struct NoTransactionsAccountId;
pub struct TransactionsForAccountRequest<I> {
/// The ID of the account for which the transactions are to be retrieved.
account_id: I,
// Indicates whether or not to include failed operations in the response.
include_failed: Option<bool>,
/// A pointer to a specific location in a collection of responses, derived from the
/// `paging_token` value of a record. Used for pagination control in the API response.
pub cursor: Option<u32>,
Expand Down Expand Up @@ -43,12 +45,24 @@ impl TransactionsForAccountRequest<NoTransactionsAccountId> {
self,
account_id: String,
) -> Result<TransactionsForAccountRequest<TransactionsAccountId>, String> {
if let Err(e) = is_public_key(&account_id) {
return Err(e.to_string());
}

Ok(TransactionsForAccountRequest {
account_id: TransactionsAccountId(account_id),
include_failed: self.include_failed,
cursor: self.cursor,
limit: self.limit,
order: self.order,
})
}
}

impl TransactionsForAccountRequest<TransactionsAccountId> {
pub fn set_include_failed(
self,
include_failed: bool,
) -> Result<TransactionsForAccountRequest<TransactionsAccountId>, String> {
Ok(TransactionsForAccountRequest {
account_id: self.account_id,
include_failed: Some(include_failed),
cursor: self.cursor,
limit: self.limit,
order: self.order,
Expand All @@ -58,23 +72,28 @@ impl TransactionsForAccountRequest<NoTransactionsAccountId> {

impl Request for TransactionsForAccountRequest<TransactionsAccountId> {
fn get_query_parameters(&self) -> String {
let mut query = String::new();
query.push_str(&format!("{}", self.account_id.0));

query.trim_end_matches('&').to_string()
vec![
self.cursor.as_ref().map(|c| format!("cursor={}", c)),
self.limit.as_ref().map(|l| format!("limit={}", l)),
self.order.as_ref().map(|o| format!("order={}", o)),
self.include_failed
.as_ref()
.map(|i| format!("include_failed={}", i)),
]
.build_query_parameters()
}

fn build_url(&self, base_url: &str) -> String {
// This URL is not built with query paramaters, but with the account ID as addition to the path.
// Therefore there is no `?` but a `/` in the formatted string.
// Additionally, this request uses the API endpoint for `accounts`.
// TODO: Documentation
let account_id = &self.account_id.0;
use crate::accounts::ACCOUNTS_PATH;
format!(
"{}/{}/{}/{}",
"{}/{}/{}/{}{}",
base_url,
ACCOUNTS_PATH,
account_id,
super::TRANSACTIONS_PATH,
self.get_query_parameters(),
super::TRANSACTIONS_PATH
)
}
}

0 comments on commit c78fd9d

Please sign in to comment.