File: example_paging_thru_zones.py

package info (click to toggle)
python-cloudflare 2.20.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,048 kB
  • sloc: python: 6,932; makefile: 138; sh: 76
file content (40 lines) | stat: -rwxr-xr-x 1,203 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env python3
"""Cloudflare API code - example"""

import os
import sys

sys.path.insert(0, os.path.abspath('..'))
import CloudFlare

def main():
    cf = CloudFlare.CloudFlare(raw=True)

    page_number = 0
    while True:
        page_number += 1
        try:
            raw_results = cf.zones.get(params={'per_page':5,'page':page_number})
        except CloudFlare.exceptions.CloudFlareAPIError as e:
            exit('/zones.get %d %s - api call failed' % (e, e))

        zones = raw_results['result']
        domains = []
        for zone in zones:
            zone_id = zone['id']
            zone_name = zone['name']
            domains.append(zone_name)

        count = raw_results['result_info']['count']
        page = raw_results['result_info']['page']
        per_page = raw_results['result_info']['per_page']
        total_count = raw_results['result_info']['total_count']
        total_pages = raw_results['result_info']['total_pages']

        print("COUNT=%d PAGE=%d PER_PAGE=%d TOTAL_COUNT=%d TOTAL_PAGES=%d -- %s" % (count, page, per_page, total_count, total_pages, domains))

        if page_number == total_pages:
            break

if __name__ == '__main__':
    main()