Skip to content

Commit 4655037

Browse files
committed
Support pagination in CLI for ListPayments API.
1 parent 74f8ac2 commit 4655037

1 file changed

Lines changed: 32 additions & 5 deletions

File tree

ldk-server-cli/src/main.rs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ use ldk_server_client::ldk_server_protos::api::{
1010
OnchainReceiveRequest, OnchainSendRequest, OpenChannelRequest,
1111
};
1212
use ldk_server_client::ldk_server_protos::types::{
13-
bolt11_invoice_description, Bolt11InvoiceDescription,
13+
bolt11_invoice_description, Bolt11InvoiceDescription, PageToken, Payment,
1414
};
15+
use std::fmt::Debug;
1516

1617
#[derive(Parser, Debug)]
1718
#[command(version, about, long_about = None)]
@@ -87,7 +88,13 @@ enum Commands {
8788
announce_channel: bool,
8889
},
8990
ListChannels,
90-
ListPayments,
91+
ListPayments {
92+
#[arg(short, long)]
93+
#[arg(
94+
help = "Minimum number of payments to return. If not provided, only the first page of the paginated list is returned."
95+
)]
96+
number_of_payments: Option<u64>,
97+
},
9198
}
9299

93100
#[tokio::main]
@@ -186,13 +193,33 @@ async fn main() {
186193
Commands::ListChannels => {
187194
handle_response_result(client.list_channels(ListChannelsRequest {}).await);
188195
},
189-
Commands::ListPayments => {
190-
handle_response_result(client.list_payments(ListPaymentsRequest {}).await);
196+
Commands::ListPayments { number_of_payments } => {
197+
handle_response_result(list_n_payments(client, number_of_payments).await);
191198
},
192199
}
193200
}
194201

195-
fn handle_response_result<Rs: ::prost::Message>(response: Result<Rs, LdkServerError>) {
202+
async fn list_n_payments(
203+
client: LdkServerClient, number_of_payments: Option<u64>,
204+
) -> Result<Vec<Payment>, LdkServerError> {
205+
let mut payments = Vec::new();
206+
let mut page_token: Option<PageToken> = None;
207+
// If no count is specified, just list the first page.
208+
let target_count = number_of_payments.unwrap_or(0);
209+
210+
loop {
211+
let response = client.list_payments(ListPaymentsRequest { page_token }).await?;
212+
213+
payments.extend(response.payments);
214+
if payments.len() >= target_count as usize || response.next_page_token.is_none() {
215+
break;
216+
}
217+
page_token = response.next_page_token;
218+
}
219+
Ok(payments)
220+
}
221+
222+
fn handle_response_result<Rs: Debug>(response: Result<Rs, LdkServerError>) {
196223
match response {
197224
Ok(response) => {
198225
println!("Success: {:?}", response);

0 commit comments

Comments
 (0)