From aadd1760cf54d2d465f60d47fccdfd82d2cee115 Mon Sep 17 00:00:00 2001 From: Neshura Date: Wed, 11 Dec 2024 23:47:21 +0100 Subject: [PATCH] Add Pagination Capability --- src/cloudflare.rs | 21 ++++++++++++++++++--- src/main.rs | 2 +- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/cloudflare.rs b/src/cloudflare.rs index 7e5335a..37e3933 100644 --- a/src/cloudflare.rs +++ b/src/cloudflare.rs @@ -17,6 +17,15 @@ const API_BASE: &str = "https://api.cloudflare.com/client/v4"; struct CloudflareApiResults { result: Vec, success: bool, + result_info: CloudflareApiPagination, +} + +#[derive(Serialize, Deserialize, Debug)] +struct CloudflareApiPagination { + count: u8, + page: u8, + per_page: u8, + total_count: u16 } #[derive(Serialize, Deserialize, Debug)] @@ -55,14 +64,20 @@ impl CloudflareZone { headers } - pub(crate) fn get_entries(&self) -> Result, ()> { - let endpoint = format!("{}/zones/{}/dns_records", API_BASE, self.id); + pub(crate) fn get_entries(&self, page: Option) -> Result, ()> { + let endpoint = format!("{}/zones/{}/dns_records?page={}", API_BASE, self.id, page.unwrap_or(1)); match self.get(&endpoint) { Ok(response) => { if response.status().is_success() { let entries = match response.json::() { - Ok(data) => data, + Ok(mut data) => { + let actual_count: u16 = (data.result_info.per_page * (data.result_info.page - 1) + data.result_info.count) as u16; + if actual_count < data.result_info.total_count { + data.result.append(&mut self.get_entries(Some(page.unwrap_or(1) + 1)).expect("Pagination indicates more results")); + } + data + }, Err(e) => { let err_msg = format!("Unable to parse API response: {e}"); match connected_to_journal() { diff --git a/src/main.rs b/src/main.rs index f70a0a2..9bf43bf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -579,7 +579,7 @@ fn main() { for zone in &zone_cfgs { let cf_zone = CloudflareZone::new(zone, &config); - let cf_entries = match cf_zone.get_entries() { + let cf_entries = match cf_zone.get_entries(None) { Ok(entries) => entries, Err(_) => { continue