Add Pagination Capability

This commit is contained in:
Neshura 2024-12-11 23:47:21 +01:00
parent 10f6aa8455
commit aadd1760cf
Signed by: Neshura
GPG key ID: 4E2D47B1374C297D
2 changed files with 19 additions and 4 deletions

View file

@ -17,6 +17,15 @@ const API_BASE: &str = "https://api.cloudflare.com/client/v4";
struct CloudflareApiResults {
result: Vec<CloudflareDnsRecord>,
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<Vec<CloudflareDnsRecord>, ()> {
let endpoint = format!("{}/zones/{}/dns_records", API_BASE, self.id);
pub(crate) fn get_entries(&self, page: Option<u8>) -> Result<Vec<CloudflareDnsRecord>, ()> {
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::<CloudflareApiResults>() {
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() {

View file

@ -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