Skip to content

Commit

Permalink
Added more examples of using raw mode and updated README
Browse files Browse the repository at this point in the history
  • Loading branch information
mahtin committed Oct 29, 2016
1 parent 8f7b510 commit e179521
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 12 deletions.
59 changes: 55 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,66 @@ import CloudFlare

def main():
cf = CloudFlare.CloudFlare()
zones = cf.zones.get(params = {'per_page':50})
zones = cf.zones.get()
for zone in zones:
zone_id = zone['id']
zone_name = zone['name']
print zone_id, zone_name

if __name__ == '__main__':
main()
```

This example works when there are less than 50 zones (50 is the default number of values returned from a query like this).

Now lets expand on that and add code to show the IPv6 and SSL status of the zones. Lets also query 100 zones.

```python
import CloudFlare

def main():
cf = CloudFlare.CloudFlare()
zones = cf.zones.get(params = {'per_page':100})
for zone in zones:
zone_id = zone['id']
settings_ipv6 = cf.zones.settings.ipv6.get(zone_id)
ipv6_status = settings_ipv6['value']
zone_name = zone['name']

settings_ssl = cf.zones.settings.ssl.get(zone_id)
ssl_status = settings_ssl['value']
print zone_id, ssl_status, ipv6_status, zone_name

settings_ipv6 = cf.zones.settings.ipv6.get(zone_id)
ipv6_status = settings_ipv6['value']

print zone_id, zone_name, ssl_status, ipv6_status

if __name__ == '__main__':
main()
```

In order to query more than a single page of zones, we would have to use the raw mode (decribed more below).
We can loop over many get calls and pass the page paramater to facilitate the paging.

Raw mode is only needed when a get request has the possibility of returning many items.

```python
import CloudFlare

def main():
cf = CloudFlare.CloudFlare(raw=True)
page_number = 0
while True:
raw_results = cf.zones.get(params={'per_page':5,'page':page_number})
zones = raw_results['result']

for zone in zones:
zone_id = zone['id']
zone_name = zone['name']
print zone_id, zone_name

total_pages = raw_results['result_info']['total_pages']
page_number += 1
if page_number == total_pages:
break

if __name__ == '__main__':
main()
Expand Down
63 changes: 59 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,70 @@ status of the zone.
def main():
cf = CloudFlare.CloudFlare()
zones = cf.zones.get(params = {'per_page':50})
zones = cf.zones.get()
for zone in zones:
zone_id = zone['id']
zone_name = zone['name']
print zone_id, zone_name
if __name__ == '__main__':
main()
This example works when there are less than 50 zones (50 is the default
number of values returned from a query like this).

Now lets expand on that and add code to show the IPv6 and SSL status of
the zones. Lets also query 100 zones.

.. code:: python
import CloudFlare
def main():
cf = CloudFlare.CloudFlare()
zones = cf.zones.get(params = {'per_page':100})
for zone in zones:
zone_id = zone['id']
settings_ipv6 = cf.zones.settings.ipv6.get(zone_id)
ipv6_status = settings_ipv6['value']
zone_name = zone['name']
settings_ssl = cf.zones.settings.ssl.get(zone_id)
ssl_status = settings_ssl['value']
print zone_id, ssl_status, ipv6_status, zone_name
settings_ipv6 = cf.zones.settings.ipv6.get(zone_id)
ipv6_status = settings_ipv6['value']
print zone_id, zone_name, ssl_status, ipv6_status
if __name__ == '__main__':
main()
In order to query more than a single page of zones, we would have to use
the raw mode (decribed more below). We can loop over many get calls and
pass the page paramater to facilitate the paging.

Raw mode is only needed when a get request has the possibility of
returning many items.

.. code:: python
import CloudFlare
def main():
cf = CloudFlare.CloudFlare(raw=True)
page_number = 0
while True:
raw_results = cf.zones.get(params={'per_page':5,'page':page_number})
zones = raw_results['result']
for zone in zones:
zone_id = zone['id']
zone_name = zone['name']
print zone_id, zone_name
total_pages = raw_results['result_info']['total_pages']
page_number += 1
if page_number == total_pages:
break
if __name__ == '__main__':
main()
Expand Down
42 changes: 42 additions & 0 deletions examples/example_paging_thru_zones.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env python
"""Cloudflare API code - example"""

import os
import sys

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

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

page_number = 0
while True:
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
page_number += 1

if __name__ == '__main__':
main()

8 changes: 4 additions & 4 deletions examples/example_paging_thru_zones.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
tmp=/tmp/$$_
trap "rm ${tmp}; exit 0" 0 1 2 15

PAGE=0
PAGE_NUMBER=0

while true
do
cli4 --raw per_page=5 page=${PAGE} /zones > ${tmp}
cli4 --raw per_page=5 page=${PAGE_NUMBER} /zones > ${tmp}

domains=`jq -c '.|.result|.[]|.name' < ${tmp} | tr -d '"'`
result_info=`jq -c '.|.result_info' < ${tmp}`
Expand All @@ -20,13 +20,13 @@ do

echo COUNT=${COUNT} PAGE=${PAGE} PER_PAGE=${PER_PAGE} TOTAL_COUNT=${TOTAL_COUNT} TOTAL_PAGES=${TOTAL_PAGES} -- ${domains}

if [ "${PAGE}" == "${TOTAL_PAGES}" ]
if [ "${PAGE_NUMBER}" == "${TOTAL_PAGES}" ]
then
## last section
break
fi

# grab the next page
PAGE=`expr ${PAGE} + 1`
PAGE_NUMBER=`expr ${PAGE_NUMBER} + 1`
done

0 comments on commit e179521

Please sign in to comment.