/usr/share/doc/python3-cloudflare/examples
NameSizeModeActions
example_always_use_https.py19890644editdlrm
example_are_zones_ipv6.py18760644editdlrm
example_are_zones_ipv6_simple.py5580644editdlrm
example_certificates.py26730644editdlrm
example_create_zone_and_populate.py39350644editdlrm
example_custom_hostnames.py22800644editdlrm
example_delete_zone_entry.py20340644editdlrm
example_dnssec_settings.py14590644editdlrm
example_dns_export.py13660644editdlrm
example_graphql.py23280644editdlrm
example_graphql.sh9810644editdlrm
example_ips.py7630644editdlrm
example_list_api_from_web.py4540644editdlrm
example_page_rules.sh4400644editdlrm
example_paging_thru_zones.py12040644editdlrm
example_paging_thru_zones.sh7800644editdlrm
example_proxied.py33880644editdlrm
example_settings.py18270644editdlrm
example_update_dynamic_dns.py42730644editdlrm
example_user.py55120644editdlrm
example_with_usage.py6770644editdlrm
example_zones.py24360644editdlrm
example_zone_search.sh4200644editdlrm
__init__.py00644editdlrm
Edit: /usr/share/doc/python3-cloudflare/examples/example_graphql.py (2328B)
#!/usr/bin/env python """Cloudflare API code - example""" import os import sys import time import datetime import pytz sys.path.insert(0, os.path.abspath('..')) import CloudFlare def now_iso8601_time(h_delta): """Cloudflare API code - example""" t = time.time() - (h_delta * 3600) r = datetime.datetime.fromtimestamp(int(t), tz=pytz.timezone("UTC")).strftime('%Y-%m-%dT%H:%M:%SZ') return r def main(): """Cloudflare API code - example""" # Grab the zone name try: zone_name = sys.argv[1] params = {'name':zone_name, 'per_page':1} except IndexError: exit('usage: example_graphql zone') cf = CloudFlare.CloudFlare() # grab the zone identifier try: zones = cf.zones.get(params=params) 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)) date_before = now_iso8601_time(0) # now date_after = now_iso8601_time(7 * 24) # 7 days worth zone_id = zones[0]['id'] query=""" query { viewer { zones(filter: {zoneTag: "%s"} ) { httpRequests1dGroups(limit:40, filter:{date_lt: "%s", date_gt: "%s"}) { sum { countryMap { bytes, requests, clientCountryName } } dimensions { date } } } } } """ % (zone_id, date_before[0:10], date_after[0:10]) # only use yyyy-mm-dd part for httpRequests1dGroups # query - always a post try: r = cf.graphql.post(data={'query':query}) except CloudFlare.exceptions.CloudFlareAPIError as e: exit('/graphql.post %d %s - api call failed' % (e, e)) ## only one zone, so use zero'th element! zone_info = r['data']['viewer']['zones'][0] httpRequests1dGroups = zone_info['httpRequests1dGroups'] for h in sorted(httpRequests1dGroups, key=lambda v: v['dimensions']['date']): result_date = h['dimensions']['date'] result_info = h['sum']['countryMap'] print(result_date) for element in sorted(result_info, key=lambda v: -v['bytes']): print(" %7d %7d %2s" % (element['bytes'], element['requests'], element['clientCountryName'])) if __name__ == '__main__': main() exit(0)