Skip to content

Commit

Permalink
Apply pagination macro
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-pease authored and LeonardTibben committed May 3, 2024
1 parent 81306c5 commit 8279317
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 54 deletions.
2 changes: 2 additions & 0 deletions stellar_rust_sdk/src/horizon_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,8 @@ impl HorizonClient {
/// # use stellar_rs::offers::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();
Expand Down
49 changes: 2 additions & 47 deletions stellar_rust_sdk/src/offers/all_offers_request.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::{models::*, BuildQueryParametersExt};

use stellar_rust_sdk_derive::Pagination;
use crate::Paginatable;

Expand All @@ -20,6 +19,8 @@ use crate::Paginatable;
/// ```
/// use stellar_rs::offers::all_offers_request::AllOffersRequest;
/// use stellar_rs::models::{Asset, NativeAsset, Order};
/// use stellar_rust_sdk_derive::Pagination;
/// use stellar_rs::Paginatable;
///
/// let request = AllOffersRequest::new()
/// .set_sponsor("GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5".to_string()).unwrap() // Optional sponsor filter
Expand All @@ -43,11 +44,9 @@ pub struct AllOffersRequest {
seller: Option<String>,
/// Optional. Indicates an selling asset for which offers are being queried.
/// When set, the response will filter the offers that hold this specific asset.
// TODO: Make `NativeAsset` also possible
selling: Option<Asset<IssuedAsset>>,
/// Optional. Indicates a buying asset for which offers are being queried.
/// When set, the response will filter the offers that hold this specific asset.
// TODO: Make `NativeAsset` also possible
buying: Option<Asset<IssuedAsset>>,
/// 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.
Expand Down Expand Up @@ -145,48 +144,4 @@ impl AllOffersRequest {
..self
}
}

/// Sets the cursor for pagination.
///
/// # Arguments
/// * `cursor` - A `u32` value pointing to a specific location in a collection of responses.
///
pub fn set_cursor(self, cursor: u32) -> Result<AllOffersRequest, String> {
if cursor < 1 {
return Err("cursor must be greater than or equal to 1".to_string());
}

Ok(AllOffersRequest {
cursor: Some(cursor),
..self
})
}

/// Sets the maximum number of records to return.
///
/// # Arguments
/// * `limit` - A `u8` value specifying the maximum number of records. Range: 1 to 200. Defaults to 10.
///
pub fn set_limit(self, limit: u8) -> Result<AllOffersRequest, String> {
if limit < 1 || limit > 200 {
return Err("limit must be between 1 and 200".to_string());
}

Ok(AllOffersRequest {
limit: Some(limit),
..self
})
}

/// Sets the order of the returned records.
///
/// # Arguments
/// * `order` - An [`Order`] enum value specifying the order (ascending or descending).
///
pub fn set_order(self, order: Order) -> AllOffersRequest {
AllOffersRequest {
order: Some(order),
..self
}
}
}
12 changes: 5 additions & 7 deletions stellar_rust_sdk/src/offers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ pub mod prelude {
#[cfg(test)]
pub mod test {
use super::prelude::*;
use crate::horizon_client::HorizonClient;
use crate::models::*;
use crate::{horizon_client::HorizonClient, models::*, Paginatable};

const LINK_SELF: &str = "https://horizon-testnet.stellar.org/offers/1";
const LINK_OFFER_MAKER: &str = "https://horizon-testnet.stellar.org/accounts/GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5";
Expand Down Expand Up @@ -180,26 +179,25 @@ pub mod test {
const BUYING_ASSET_TYPE: &str = "credit_alphanum12";
const BUYING_ASSET_CODE: &str = "EURCAllow";
const BUYING_ASSET_ISSUER: &str = "GA6HVGLFUF3BHHGR5CMYXIVZ3RYVUH5EUYAOAY4T3OKI5OQVIWVRK24R";
const AMOUNT: &str = "922320116343.5775807";
const AMOUNT: &str = "922320116343.0475807";
const PRICE_R_N: &u32 = &1;
const PRICE_R_D: &u32 = &1;
const PRICE: &str = "1.0000000";
const LAST_MODIFIED_LEDGER: &u32 = &1265238;
const LAST_MODIFIED_TIME: &str = "2024-04-23T16:33:24Z";
const LAST_MODIFIED_LEDGER: &u32 = &1384257;
const LAST_MODIFIED_TIME: &str = "2024-04-30T22:19:21Z";

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

// Create a request and supply values for optional filters.
// TODO: Try to supply more filters and produce testable response results
let all_offers_request =
AllOffersRequest::new()
.set_seller(SELLER.to_string()).unwrap()
.set_cursor(1).unwrap()
.set_limit(100).unwrap()
.set_order(Order::Asc);
.set_order(Order::Asc).unwrap();

let all_offers_response = horizon_client
.get_all_offers(&all_offers_request)
Expand Down

0 comments on commit 8279317

Please sign in to comment.