Skip to content

Commit

Permalink
fixed CloudFlare.exceptions in raise/except
Browse files Browse the repository at this point in the history
  • Loading branch information
mahtin committed Oct 19, 2016
1 parent 5c59702 commit c6c3175
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 36 deletions.
2 changes: 1 addition & 1 deletion CloudFlare/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

from cloudflare import CloudFlare

__version__ = '1.2.3'
__version__ = '1.2.4'
33 changes: 29 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ if __name__ == '__main__':
A more complex example follows.

```python
import sys
import CloudFlare
import CloudFlare.exceptions

def main():
zone_name = 'example.com'
Expand All @@ -79,19 +79,22 @@ def main():
# query for the zone name and expect only one value back
try:
zones = cf.zones.get(params = {'name':zone_name,'per_page':1})
except CloudFlare.CloudFlareAPIError as e:
except CloudFlare.exceptions.CloudFlareAPIError as e:
exit('/zones.get %d %s - api call failed' % (e, e))
except Exception as e:
exit('/zones.get - %s - api call failed' % (e))

if len(zones) == 0:
exit('No zones found')

# extract the zone_id which is needed to process that zone
zone = zones[0]
zone_id = zone['id']

# request the DNS records from that zone
try:
dns_records = cf.zones.dns_records.get(zone_id)
except CloudFlare.CloudFlareAPIError as e:
except CloudFlare.exceptions.CloudFlareAPIError as e:
exit('/zones/dns_records.get %d %s - api call failed' % (e, e))

# print the results - first the zone name
Expand Down Expand Up @@ -171,7 +174,9 @@ You can leave *extras* in the configuration with a blank value (or omit the opti

## Exceptions and return values

The response is build from the JSON in the API call.
### Response data

The response is build from the JSON in the API call.
It contains the **results** values; but does not contain the paging values.

You can return all the paging values by calling the class with raw=True. Here's an example without paging.
Expand Down Expand Up @@ -235,6 +240,26 @@ This produces.

A full example of paging is provided below.


### Exceptions

The library will raise **CloudFlareAPIError** when the API call fails.
The exception returns both an integer and textual message in one value.

```python
import CloudFlare
import CloudFlare.exceptions

...
try
r = ...
except CloudFlare.exceptions.CloudFlareAPIError as e:
exit('api error: %d %s' % (e, e))
...
```

The other raised response is **CloudFlareInternalError** which can happen when calling an invalid method.

## Included example code

The [examples](https://github.com/cloudflare/python-cloudflare/tree/master/examples) folder contains many examples in both simple and verbose formats.
Expand Down
33 changes: 30 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ A more complex example follows.

.. code:: python
import sys
import CloudFlare
import CloudFlare.exceptions
def main():
zone_name = 'example.com'
Expand All @@ -95,19 +95,22 @@ A more complex example follows.
# query for the zone name and expect only one value back
try:
zones = cf.zones.get(params = {'name':zone_name,'per_page':1})
except CloudFlare.CloudFlareAPIError as e:
except CloudFlare.exceptions.CloudFlareAPIError as e:
exit('/zones.get %d %s - api call failed' % (e, e))
except Exception as e:
exit('/zones.get - %s - api call failed' % (e))
if len(zones) == 0:
exit('No zones found')
# extract the zone_id which is needed to process that zone
zone = zones[0]
zone_id = zone['id']
# request the DNS records from that zone
try:
dns_records = cf.zones.dns_records.get(zone_id)
except CloudFlare.CloudFlareAPIError as e:
except CloudFlare.exceptions.CloudFlareAPIError as e:
exit('/zones/dns_records.get %d %s - api call failed' % (e, e))
# print the results - first the zone name
Expand Down Expand Up @@ -199,6 +202,9 @@ value (or omit the option variable fully).
Exceptions and return values
----------------------------

Response data
~~~~~~~~~~~~~

The response is build from the JSON in the API call. It contains the
**results** values; but does not contain the paging values.

Expand Down Expand Up @@ -265,6 +271,27 @@ This produces.

A full example of paging is provided below.

Exceptions
~~~~~~~~~~

The library will raise **CloudFlareAPIError** when the API call fails.
The exception returns both an integer and textual message in one value.

.. code:: python
import CloudFlare
import CloudFlare.exceptions
...
try
r = ...
except CloudFlare.exceptions.CloudFlareAPIError as e:
exit('api error: %d %s' % (e, e))
...
The other raised response is **CloudFlareInternalError** which can
happen when calling an invalid method.

Included example code
---------------------

Expand Down
7 changes: 4 additions & 3 deletions examples/example_are_zones_ipv6.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

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

def main():
"""Cloudflare API code - example"""
Expand All @@ -31,7 +32,7 @@ def main():
# grab the zone identifier
try:
zones = cf.zones.get(params=params)
except CloudFlare.CloudFlareAPIError as e:
except CloudFlare.exceptions.CloudFlareAPIError as e:
exit('/zones.get %d %s - api call failed' % (e, e))
except Exception as e:
exit('/zones - %s - api call failed' % (e))
Expand All @@ -41,15 +42,15 @@ def main():
zone_id = zone['id']
try:
ipv6 = cf.zones.settings.ipv6.get(zone_id)
except CloudFlare.CloudFlareAPIError as e:
except CloudFlare.exceptions.CloudFlareAPIError as e:
exit('/zones.settings.ipv6.get %d %s - api call failed' % (e, e))

ipv6_value = ipv6['value']
if update_ipv6 and ipv6_value == 'off':
print zone_id, ipv6_value, zone_name, '(now updating... off -> on)'
try:
ipv6 = cf.zones.settings.ipv6.patch(zone_id, data={'value':'on'})
except CloudFlare.CloudFlareAPIError as e:
except CloudFlare.exceptions.CloudFlareAPIError as e:
exit('/zones.settings.ipv6.patch %d %s - api call failed' % (e, e))
ipv6_value = ipv6['value']
if ipv6_value == 'on':
Expand Down
5 changes: 3 additions & 2 deletions examples/example_certificates.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

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

def main():
"""Cloudflare API code - example"""
Expand All @@ -23,7 +24,7 @@ def main():
# grab the zone identifier
try:
zones = cf.zones.get(params=params)
except CloudFlare.CloudFlareAPIError as e:
except CloudFlare.exceptions.CloudFlareAPIError as e:
exit('/zones %d %s - api call failed' % (e, e))
except Exception as e:
exit('/zones - %s - api call failed' % (e))
Expand All @@ -34,7 +35,7 @@ def main():
zone_id = zone['id']
try:
certificates = cf.zones.ssl.certificate_packs.get(zone_id)
except CloudFlare.CloudFlareAPIError as e:
except CloudFlare.exceptions.CloudFlareAPIError as e:
exit('/zones.ssl.certificate_packs %d %s - api call failed' % (e, e))

for certificate in certificates:
Expand Down
9 changes: 5 additions & 4 deletions examples/example_create_zone_and_populate.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

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

def main():
"""Cloudflare API code - example"""
Expand All @@ -23,7 +24,7 @@ def main():
print 'Create zone %s ...' % (zone_name)
try:
zone_info = cf.zones.post(data={'jump_start':False, 'name': zone_name})
except CloudFlare.CloudFlareAPIError as e:
except CloudFlare.exceptions.CloudFlareAPIError as e:
exit('/zones.post %s - %d %s' % (zone_name, e, e))
except Exception as e:
exit('/zones.post %s - %s' % (zone_name, e))
Expand Down Expand Up @@ -58,7 +59,7 @@ def main():
# Create DNS record
try:
r = cf.zones.dns_records.post(zone_id, data=dns_record)
except CloudFlare.CloudFlareAPIError as e:
except CloudFlare.exceptions.CloudFlareAPIError as e:
exit('/zones.dns_records.post %s %s - %d %s' % (zone_name, dns_record['name'], e, e))
# Print respose info - they should be the same
dns_record = r
Expand Down Expand Up @@ -86,7 +87,7 @@ def main():

try:
dns_record = cf.zones.dns_records.put(zone_id, dns_record_id, data=new_dns_record)
except CloudFlare.CloudFlareAPIError as e:
except CloudFlare.exceptions.CloudFlareAPIError as e:
exit('/zones/dns_records.put %d %s - api call failed' % (e, e))

print ''
Expand All @@ -95,7 +96,7 @@ def main():
print 'Read back DNS records ...'
try:
dns_records = cf.zones.dns_records.get(zone_id)
except CloudFlare.CloudFlareAPIError as e:
except CloudFlare.exceptions.CloudFlareAPIError as e:
exit('/zones.dns_records.get %s - %d %s' % (zone_name, e, e))

for dns_record in sorted(dns_records, key=lambda v: v['name']):
Expand Down
5 changes: 3 additions & 2 deletions examples/example_dnssec_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

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

def main():
"""Cloudflare API code - example"""
Expand All @@ -22,7 +23,7 @@ def main():
# grab the zone identifier
try:
zones = cf.zones.get(params=params)
except CloudFlare.CloudFlareAPIError as e:
except CloudFlare.exceptions.CloudFlareAPIError as e:
exit('/zones.get %d %s - api call failed' % (e, e))
except Exception as e:
exit('/zones.get - %s - api call failed' % (e))
Expand All @@ -34,7 +35,7 @@ def main():
# grab the DNSSEC settings
try:
settings = cf.zones.dnssec.get(zone_id)
except CloudFlare.CloudFlareAPIError as e:
except CloudFlare.exceptions.CloudFlareAPIError as e:
exit('/zones.dnssec.get %d %s - api call failed' % (e, e))

print zone_id, zone_name
Expand Down
3 changes: 2 additions & 1 deletion examples/example_ips.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@

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

def main():
"""Cloudflare API code - example"""

cf = CloudFlare.CloudFlare()
try:
ips = cf.ips.get()
except CloudFlare.CloudFlareAPIError as e:
except CloudFlare.exceptions.CloudFlareAPIError as e:
exit('/ips - %d %s' % (e, e))
except Exception as e:
exit('/ips - %s - api call connection failed' % (e))
Expand Down
7 changes: 4 additions & 3 deletions examples/example_proxied.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

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

def main():
"""Change the proxied value on a FQDN"""
Expand All @@ -30,7 +31,7 @@ def main():
try:
params = {'name':zone_name, 'per_page':1}
zones = cf.zones.get(params=params)
except CloudFlare.CloudFlareAPIError as e:
except CloudFlare.exceptions.CloudFlareAPIError as e:
exit('/zones.get %d %s - api call failed' % (e, e))
except Exception as e:
exit('/zones.get - %s - api call failed' % (e))
Expand All @@ -49,7 +50,7 @@ def main():
try:
params = {'name': dns_name}
dns_records = cf.zones.dns_records.get(zone_id, params=params)
except CloudFlare.CloudFlareAPIError as e:
except CloudFlare.exceptions.CloudFlareAPIError as e:
exit('/zones/dns_records.get %d %s - api call failed' % (e, e))

if len(dns_records) == 0:
Expand Down Expand Up @@ -86,7 +87,7 @@ def main():

try:
dns_record = cf.zones.dns_records.put(zone_id, dns_record_id, data=new_dns_record)
except CloudFlare.CloudFlareAPIError as e:
except CloudFlare.exceptions.CloudFlareAPIError as e:
exit('/zones/dns_records.put %d %s - api call failed' % (e, e))

r_zone_id = dns_record['zone_id']
Expand Down
5 changes: 3 additions & 2 deletions examples/example_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

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

def main():
"""Cloudflare API code - example"""
Expand All @@ -22,7 +23,7 @@ def main():
# grab the zone identifier
try:
zones = cf.zones.get(params=params)
except CloudFlare.CloudFlareAPIError as e:
except CloudFlare.exceptions.CloudFlareAPIError as e:
exit('/zones.get %d %s - api call failed' % (e, e))
except Exception as e:
exit('/zones.get - %s - api call failed' % (e))
Expand All @@ -33,7 +34,7 @@ def main():
zone_id = zone['id']
try:
settings = cf.zones.settings.get(zone_id)
except CloudFlare.CloudFlareAPIError as e:
except CloudFlare.exceptions.CloudFlareAPIError as e:
exit('/zones.settings.get %d %s - api call failed' % (e, e))

print zone_id, zone_name
Expand Down
Loading

0 comments on commit c6c3175

Please sign in to comment.