Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

Commit 4aa1148

Browse files
committed
Added working GraphQL examples
1 parent 5c25aa8 commit 4aa1148

4 files changed

Lines changed: 223 additions & 0 deletions

File tree

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,59 @@ $
961961

962962
Refer to the Cloudflare Workers API documentation for more information.
963963

964+
## Cloudflare GraphQL
965+
966+
The GraphQL interface can be accessed via the command line or via Python.
967+
968+
```
969+
query="""
970+
query {
971+
viewer {
972+
zones(filter: {zoneTag: "%s"} ) {
973+
httpRequests1dGroups(limit:40, filter:{date_lt: "%s", date_gt: "%s"}) {
974+
sum { countryMap { bytes, requests, clientCountryName } }
975+
dimensions { date }
976+
}
977+
}
978+
}
979+
}
980+
""" % (zone_id, date_before[0:10], date_after[0:10])
981+
982+
r = cf.graphql.post(data={'query':query})
983+
984+
httpRequests1dGroups = zone_info = r['data']['viewer']['zones'][0]['httpRequests1dGroups']
985+
```
986+
987+
See the [examples/example_graphql.sh](examples/example_graphql.sh) and [examples/example_graphql.py](examples/example_graphql.py) files for working examples.
988+
Here is the working example of the shell version:
989+
990+
```
991+
$ examples/example_graphql.sh example.com
992+
2020-07-14T02:00:00Z 34880
993+
2020-07-14T03:00:00Z 18953
994+
2020-07-14T04:00:00Z 28700
995+
2020-07-14T05:00:00Z 2358
996+
2020-07-14T06:00:00Z 34905
997+
2020-07-14T07:00:00Z 779
998+
2020-07-14T08:00:00Z 35450
999+
2020-07-14T10:00:00Z 17803
1000+
2020-07-14T11:00:00Z 32678
1001+
2020-07-14T12:00:00Z 19947
1002+
2020-07-14T13:00:00Z 4956
1003+
2020-07-14T14:00:00Z 34585
1004+
2020-07-14T15:00:00Z 3022
1005+
2020-07-14T16:00:00Z 5224
1006+
2020-07-14T18:00:00Z 79482
1007+
2020-07-14T21:00:00Z 10609
1008+
2020-07-14T22:00:00Z 5740
1009+
2020-07-14T23:00:00Z 2545
1010+
2020-07-15T01:00:00Z 10777
1011+
$
1012+
```
1013+
1014+
For more information on how to use GraphQL at Cloudflare, refer to the [Cloudflare GraphQL Analytics API](https://developers.cloudflare.com/analytics/graphql-api).
1015+
It contains a full overview of Cloudflare's GraphQL features and keywords.
1016+
9641017
## Implemented API calls
9651018

9661019
The **--dump** argument to cli4 will produce a list of all the call implemented within the library.

README.rst

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,64 @@ state.
10611061

10621062
Refer to the Cloudflare Workers API documentation for more information.
10631063

1064+
Cloudflare GraphQL
1065+
------------------
1066+
1067+
The GraphQL interface can be accessed via the command line or via
1068+
Python.
1069+
1070+
::
1071+
1072+
query="""
1073+
query {
1074+
viewer {
1075+
zones(filter: {zoneTag: "%s"} ) {
1076+
httpRequests1dGroups(limit:40, filter:{date_lt: "%s", date_gt: "%s"}) {
1077+
sum { countryMap { bytes, requests, clientCountryName } }
1078+
dimensions { date }
1079+
}
1080+
}
1081+
}
1082+
}
1083+
""" % (zone_id, date_before[0:10], date_after[0:10])
1084+
1085+
r = cf.graphql.post(data={'query':query})
1086+
1087+
httpRequests1dGroups = zone_info = r['data']['viewer']['zones'][0]['httpRequests1dGroups']
1088+
1089+
See the `examples/example\_graphql.sh <examples/example_graphql.sh>`__
1090+
and `examples/example\_graphql.py <examples/example_graphql.py>`__ files
1091+
for working examples. Here is the working example of the shell version:
1092+
1093+
::
1094+
1095+
$ examples/example_graphql.sh example.com
1096+
2020-07-14T02:00:00Z 34880
1097+
2020-07-14T03:00:00Z 18953
1098+
2020-07-14T04:00:00Z 28700
1099+
2020-07-14T05:00:00Z 2358
1100+
2020-07-14T06:00:00Z 34905
1101+
2020-07-14T07:00:00Z 779
1102+
2020-07-14T08:00:00Z 35450
1103+
2020-07-14T10:00:00Z 17803
1104+
2020-07-14T11:00:00Z 32678
1105+
2020-07-14T12:00:00Z 19947
1106+
2020-07-14T13:00:00Z 4956
1107+
2020-07-14T14:00:00Z 34585
1108+
2020-07-14T15:00:00Z 3022
1109+
2020-07-14T16:00:00Z 5224
1110+
2020-07-14T18:00:00Z 79482
1111+
2020-07-14T21:00:00Z 10609
1112+
2020-07-14T22:00:00Z 5740
1113+
2020-07-14T23:00:00Z 2545
1114+
2020-07-15T01:00:00Z 10777
1115+
$
1116+
1117+
For more information on how to use GraphQL at Cloudflare, refer to the
1118+
`Cloudflare GraphQL Analytics
1119+
API <https://developers.cloudflare.com/analytics/graphql-api>`__. It
1120+
contains a full overview of Cloudflare's GraphQL features and keywords.
1121+
10641122
Implemented API calls
10651123
---------------------
10661124

examples/example_graphql.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/env python
2+
"""Cloudflare API code - example"""
3+
4+
import os
5+
import sys
6+
import time
7+
import datetime
8+
import pytz
9+
10+
sys.path.insert(0, os.path.abspath('..'))
11+
import CloudFlare
12+
13+
def now_iso8601_time(h_delta):
14+
"""Cloudflare API code - example"""
15+
16+
t = time.time() - (h_delta * 3600)
17+
r = datetime.datetime.fromtimestamp(int(t), tz=pytz.timezone("UTC")).strftime('%Y-%m-%dT%H:%M:%SZ')
18+
return r
19+
20+
def main():
21+
"""Cloudflare API code - example"""
22+
23+
# Grab the zone name
24+
try:
25+
zone_name = sys.argv[1]
26+
params = {'name':zone_name, 'per_page':1}
27+
except IndexError:
28+
exit('usage: example_graphql zone')
29+
30+
cf = CloudFlare.CloudFlare()
31+
32+
# grab the zone identifier
33+
try:
34+
zones = cf.zones.get(params=params)
35+
except CloudFlare.exceptions.CloudFlareAPIError as e:
36+
exit('/zones.get %d %s - api call failed' % (e, e))
37+
except Exception as e:
38+
exit('/zones - %s - api call failed' % (e))
39+
40+
date_before = now_iso8601_time(0) # now
41+
date_after = now_iso8601_time(7 * 24) # 7 days worth
42+
43+
zone_id = zones[0]['id']
44+
query="""
45+
query {
46+
viewer {
47+
zones(filter: {zoneTag: "%s"} ) {
48+
httpRequests1dGroups(limit:40, filter:{date_lt: "%s", date_gt: "%s"}) {
49+
sum { countryMap { bytes, requests, clientCountryName } }
50+
dimensions { date }
51+
}
52+
}
53+
}
54+
}
55+
""" % (zone_id, date_before[0:10], date_after[0:10]) # only use yyyy-mm-dd part for httpRequests1dGroups
56+
57+
# query - always a post
58+
try:
59+
r = cf.graphql.post(data={'query':query})
60+
except CloudFlare.exceptions.CloudFlareAPIError as e:
61+
exit('/graphql.post %d %s - api call failed' % (e, e))
62+
63+
## only one zone, so use zero'th element!
64+
zone_info = r['data']['viewer']['zones'][0]
65+
66+
httpRequests1dGroups = zone_info['httpRequests1dGroups']
67+
68+
for h in sorted(httpRequests1dGroups, key=lambda v: v['dimensions']['date']):
69+
result_date = h['dimensions']['date']
70+
result_info = h['sum']['countryMap']
71+
print(result_date)
72+
for element in sorted(result_info, key=lambda v: -v['bytes']):
73+
print(" %7d %7d %2s" % (element['bytes'], element['requests'], element['clientCountryName']))
74+
75+
if __name__ == '__main__':
76+
main()
77+
exit(0)
78+

examples/example_graphql.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
:
2+
3+
#
4+
# Show usage of GraphQL - see https://developers.cloudflare.com/analytics/graphql-api for all info
5+
#
6+
7+
# pass one argument - the zone
8+
ZONEID=`cli4 name="$1" /zones | jq -r '.[].id'`
9+
if [ "${ZONEID}" = "" ]
10+
then
11+
echo "$1: zone not found" 1>&2
12+
exit 1
13+
fi
14+
15+
# Just query the last 24 hours
16+
DATE_BEFORE=`date -u +%Y-%m-%dT%H:%M:%SZ`
17+
DATE_AFTER=`date -u -v -24H +%Y-%m-%dT%H:%M:%SZ`
18+
19+
# build the GraphQL query - this is just a simple example
20+
QUERY='
21+
query {
22+
viewer {
23+
zones(filter: {zoneTag: "'${ZONEID}'"} ) {
24+
httpRequests1hGroups(limit:100, orderBy:[datetime_ASC], filter:{datetime_gt:"'${DATE_AFTER}'", datetime_lt:"'${DATE_BEFORE}'"}) {
25+
dimensions { datetime }
26+
sum { bytes }
27+
}
28+
}
29+
}
30+
}
31+
'
32+
33+
# this not only does the query; but also drills down into the results to print the final data
34+
cli4 --post query="${QUERY}" /graphql | jq -cr '.data.viewer.zones[]|.httpRequests1hGroups[]|.dimensions.datetime,.sum.bytes' | paste - -

0 commit comments

Comments
 (0)