Add Pagination Capability
This commit is contained in:
parent
10f6aa8455
commit
aadd1760cf
2 changed files with 19 additions and 4 deletions
|
@ -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() {
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue