1-
21""" CloudFlare v4 API"""
32
43import json
@@ -33,15 +32,28 @@ def __init__(self, email, token, certtoken, base_url, debug):
3332 else :
3433 self .logger = None
3534
36- def _call_with_no_auth (self , method , api_call_part1 , api_call_part2 = None , api_call_part3 = None , identifier1 = None , identifier2 = None , params = None , data = None ):
35+ def _call_with_no_auth (self , method ,
36+ api_call_part1 ,
37+ api_call_part2 = None ,
38+ api_call_part3 = None ,
39+ identifier1 = None , identifier2 = None ,
40+ params = None , data = None ):
3741 """ CloudFlare v4 API"""
3842
3943 headers = {
4044 'Content-Type' : 'application/json'
4145 }
42- return self ._call (method , headers , api_call_part1 , api_call_part2 , api_call_part3 , identifier1 , identifier2 , params , data )
43-
44- def _call_with_auth (self , method , api_call_part1 , api_call_part2 = None , api_call_part3 = None , identifier1 = None , identifier2 = None , params = None , data = None ):
46+ return self ._call (method , headers ,
47+ api_call_part1 , api_call_part2 , api_call_part3 ,
48+ identifier1 , identifier2 ,
49+ params , data )
50+
51+ def _call_with_auth (self , method ,
52+ api_call_part1 ,
53+ api_call_part2 = None ,
54+ api_call_part3 = None ,
55+ identifier1 = None , identifier2 = None ,
56+ params = None , data = None ):
4557 """ CloudFlare v4 API"""
4658
4759 if self .email is '' or self .token is '' :
@@ -51,9 +63,17 @@ def _call_with_auth(self, method, api_call_part1, api_call_part2=None, api_call_
5163 "X-Auth-Key" : self .token ,
5264 'Content-Type' : 'application/json'
5365 }
54- return self ._call (method , headers , api_call_part1 , api_call_part2 , api_call_part3 , identifier1 , identifier2 , params , data )
55-
56- def _call_with_certauth (self , method , api_call_part1 , api_call_part2 = None , api_call_part3 = None , identifier1 = None , identifier2 = None , params = None , data = None ):
66+ return self ._call (method , headers ,
67+ api_call_part1 , api_call_part2 , api_call_part3 ,
68+ identifier1 , identifier2 ,
69+ params , data )
70+
71+ def _call_with_certauth (self , method ,
72+ api_call_part1 ,
73+ api_call_part2 = None ,
74+ api_call_part3 = None ,
75+ identifier1 = None , identifier2 = None ,
76+ params = None , data = None ):
5777 """ CloudFlare v4 API"""
5878
5979 if self .certtoken is '' :
@@ -62,33 +82,57 @@ def _call_with_certauth(self, method, api_call_part1, api_call_part2=None, api_c
6282 "X-Auth-User-Service-Key" : self .certtoken ,
6383 'Content-Type' : 'application/json'
6484 }
65- return self ._call (method , headers , api_call_part1 , api_call_part2 , api_call_part3 , identifier1 , identifier2 , params , data )
66-
67- def _call (self , method , headers , api_call_part1 , api_call_part2 = None , api_call_part3 = None , identifier1 = None , identifier2 = None , params = None , data = None ):
85+ return self ._call (method , headers ,
86+ api_call_part1 , api_call_part2 , api_call_part3 ,
87+ identifier1 , identifier2 ,
88+ params , data )
89+
90+ def _call (self , method , headers ,
91+ api_call_part1 ,
92+ api_call_part2 = None ,
93+ api_call_part3 = None , identifier1 = None ,
94+ identifier2 = None , params = None ,
95+ data = None ):
6896 """ CloudFlare v4 API"""
6997
7098 if api_call_part2 is not None or (data is not None and method == 'GET' ):
7199 if identifier2 is None :
72- url = self .base_url + '/' + api_call_part1 + '/' + identifier1 + '/' + api_call_part2
100+ url = self .base_url + '/' + \
101+ api_call_part1 + '/' + \
102+ identifier1 + '/' + \
103+ api_call_part2
73104 else :
74- url = self .base_url + '/' + api_call_part1 + '/' + identifier1 + '/' + api_call_part2 + '/' + identifier2
105+ url = self .base_url + '/' + \
106+ api_call_part1 + '/' + \
107+ identifier1 + '/' + \
108+ api_call_part2 + '/' + \
109+ identifier2
75110 else :
76111 if identifier1 is None :
77- url = self .base_url + '/' + api_call_part1
112+ url = self .base_url + '/' + \
113+ api_call_part1
78114 else :
79- url = self .base_url + '/' + api_call_part1 + '/' + identifier1
115+ url = self .base_url + '/' + \
116+ api_call_part1 + '/' + \
117+ identifier1
80118 if api_call_part3 :
81119 url += '/' + api_call_part3
82120
83121 if self .logger :
84- self .logger .debug ("Call: %s,%s,%s,%s,%s" % (str (api_call_part1 ), str (identifier1 ), str (api_call_part2 ), str (identifier2 ), str (api_call_part3 )))
85- self .logger .debug ("Call: optional params and data: %s %s" % (str (params ), str (data )))
122+ self .logger .debug ("Call: %s,%s,%s,%s,%s" % (str (api_call_part1 ),
123+ str (identifier1 ),
124+ str (api_call_part2 ),
125+ str (identifier2 ),
126+ str (api_call_part3 )))
127+ self .logger .debug ("Call: optional params and data: %s %s" % (str (params ),
128+ str (data )))
86129 self .logger .debug ("Call: url is: %s" % (str (url )))
87130 self .logger .debug ("Call: method is: %s" % (str (method )))
88131 self .logger .debug ("Call: headers %s" % str (utils .sanitize_secrets (headers )))
89132
90133 if (method is None ) or (api_call_part1 is None ):
91- raise CloudFlareInternalError ('You must specify a method and endpoint' ) # should never happen
134+ # should never happen
135+ raise CloudFlareInternalError ('You must specify a method and endpoint' )
92136
93137 method = method .upper ()
94138
@@ -109,7 +153,8 @@ def _call(self, method, headers, api_call_part1, api_call_part2=None, api_call_p
109153 else :
110154 response = requests .request ('PATCH' , url , headers = headers , params = params )
111155 else :
112- raise CloudFlareAPIError (0 , 'method not supported' ) # should never happen
156+ # should never happen
157+ raise CloudFlareAPIError (0 , 'method not supported' )
113158
114159 if self .logger :
115160 self .logger .debug ("Response url: %s" , response .url )
@@ -124,7 +169,8 @@ def _call(self, method, headers, api_call_part1, api_call_part2=None, api_call_p
124169
125170 if response_data ['success' ] is False :
126171 if self .logger :
127- self .logger .debug ("response_data error: %d %s" % (response_data ['errors' ][0 ]['code' ], response_data ['errors' ][0 ]['message' ]))
172+ self .logger .debug ("response_data error: %d %s" % (response_data ['errors' ][0 ]['code' ],
173+ response_data ['errors' ][0 ]['message' ]))
128174 raise CloudFlareAPIError (response_data ['errors' ][0 ]['code' ], response_data ['errors' ][0 ]['message' ])
129175
130176 return response_data ['result' ]
0 commit comments