FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[Original HTTPS Page]
python-cloudflare/examples/example_graphql.py at master · cloudflare/python-cloudflare · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
This repository was archived by the owner on Nov 22, 2024. It is now read-only.
cloudflare
/
python-cloudflare
Public archive
forked from
cloudflare-api/python-cloudflare-v4
Notifications
You must be signed in to change notification settings
Fork
148
Star
692
Code
Pull requests
0
Actions
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
python-cloudflare
/
examples
/
example_graphql.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
executable file
·
74 lines (60 loc) · 2.32 KB
Breadcrumbs
python-cloudflare
/
examples
/
example_graphql.py
Copy path
File metadata and controls
executable file
·
74 lines (60 loc) · 2.32 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python
"""Cloudflare API code - example"""
import
os
import
sys
import
datetime
sys
.
path
.
insert
(
0
,
os
.
path
.
abspath
(
'..'
))
import
CloudFlare
def
rfc3339_iso8601_time
(
hour_delta
=
0
,
with_hms
=
False
):
""" rfc3339_iso8601_time """
# format time (with an hour offset in RFC3339 ISO8601 format (and do it UTC time)
dt
=
(
datetime
.
datetime
.
now
(
datetime
.
UTC
).
replace
(
microsecond
=
0
)
+
datetime
.
timedelta
(
hours
=
hour_delta
))
if
with_hms
:
return
dt
.
isoformat
().
replace
(
'+00:00'
,
'Z'
)
return
dt
.
strftime
(
'%Y-%m-%d'
)
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
:
sys
.
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
:
sys
.
exit
(
'/zones.get %d %s - api call failed'
%
(
int
(
e
),
str
(
e
)))
date_before
=
rfc3339_iso8601_time
(
0
)
# now
date_after
=
rfc3339_iso8601_time
(
-
7
*
24
)
# 7 previous 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
,
date_after
)
# query - always a post
try
:
r
=
cf
.
graphql
.
post
(
data
=
{
'query'
:
query
})
except
CloudFlare
.
exceptions
.
CloudFlareAPIError
as
e
:
sys
.
exit
(
'/graphql.post %d %s - api call failed'
%
(
int
(
e
),
str
(
e
)))
# only one zone, so use zero'th element!
zone_info
=
r
[
'data'
][
'viewer'
][
'zones'
][
0
]
http_requests1d_groups
=
zone_info
[
'httpRequests1dGroups'
]
for
h
in
sorted
(
http_requests1d_groups
,
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
()
sys
.
exit
(
0
)
Back
|
FazBrowse Home
|
New Git URL