Skip to content

Commit d2d2f63

Browse files
committed
added javascript content-type for Cloudflare workers, added support for python2/python3 unicode differences, added params for DELETE method
1 parent c7f6759 commit d2d2f63

File tree

1 file changed

+68
-16
lines changed

1 file changed

+68
-16
lines changed

CloudFlare/cloudflare.py

Lines changed: 68 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ def call_with_auth(self, method, parts,
6262
'X-Auth-Key': self.token,
6363
'Content-Type': 'application/json'
6464
}
65+
if type(data) == str:
66+
# passing javascript vs JSON
67+
headers['Content-Type'] = 'application/javascript'
6568
if files:
6669
# overwrite Content-Type as we are uploading data
6770
headers['Content-Type'] = 'multipart/form-data'
@@ -84,6 +87,9 @@ def call_with_auth_unwrapped(self, method, parts,
8487
'X-Auth-Key': self.token,
8588
'Content-Type': 'application/json'
8689
}
90+
if type(data) == str:
91+
# passing javascript vs JSON
92+
headers['Content-Type'] = 'application/javascript'
8793
if files:
8894
# overwrite Content-Type as we are uploading data
8995
headers['Content-Type'] = 'multipart/form-data'
@@ -176,24 +182,54 @@ def _network(self, method, headers, parts,
176182
if method == 'GET':
177183
response = self.session.get(url,
178184
headers=headers,
179-
params=params, data=data)
185+
params=params,
186+
data=data)
180187
elif method == 'POST':
181-
response = self.session.post(url,
182-
headers=headers,
183-
params=params, json=data,
184-
files=files)
188+
if type(data) == str:
189+
response = self.session.post(url,
190+
headers=headers,
191+
params=params,
192+
data=data,
193+
files=files)
194+
else:
195+
response = self.session.post(url,
196+
headers=headers,
197+
params=params,
198+
json=data,
199+
files=files)
185200
elif method == 'PUT':
186-
response = self.session.put(url,
187-
headers=headers,
188-
params=params, json=data)
201+
if type(data) == str:
202+
response = self.session.put(url,
203+
headers=headers,
204+
params=params,
205+
data=data)
206+
else:
207+
response = self.session.put(url,
208+
headers=headers,
209+
params=params,
210+
json=data)
189211
elif method == 'DELETE':
190-
response = self.session.delete(url,
191-
headers=headers,
192-
json=data)
212+
if type(data) == str:
213+
response = self.session.delete(url,
214+
headers=headers,
215+
params=params,
216+
data=data)
217+
else:
218+
response = self.session.delete(url,
219+
headers=headers,
220+
params=params,
221+
json=data)
193222
elif method == 'PATCH':
194-
response = self.session.request('PATCH', url,
195-
headers=headers,
196-
params=params, json=data)
223+
if type(data) == str:
224+
response = self.session.request('PATCH', url,
225+
headers=headers,
226+
params=params,
227+
data=data)
228+
else:
229+
response = self.session.request('PATCH', url,
230+
headers=headers,
231+
params=params,
232+
json=data)
197233
else:
198234
# should never happen
199235
raise CloudFlareAPIError(0, 'method not supported')
@@ -219,6 +255,8 @@ def _network(self, method, headers, parts,
219255
response_type = 'application/octet-stream'
220256
response_code = response.status_code
221257
response_data = response.content
258+
if type(response_data) != str:
259+
response_data = response_data.decode("utf-8")
222260

223261
if self.logger:
224262
self.logger.debug('Response: %d, %s, %s' % (response_code, response_type, response_data))
@@ -278,7 +316,10 @@ def _raw(self, method, headers, parts,
278316
if response_type == 'application/json':
279317
# API says it's JSON; so it better be parsable as JSON
280318
try:
281-
response_data = json.loads(response_data.decode('utf-8'))
319+
if hasattr(response_data, 'decode'):
320+
response_data = json.loads(response_data.decode('utf-8'))
321+
else:
322+
response_data = json.loads(response_data)
282323
except ValueError:
283324
if response_data == '':
284325
# This should really be 'null' but it isn't. Even then, it's wrong!
@@ -303,7 +344,10 @@ def _raw(self, method, headers, parts,
303344
elif response_type == 'text/plain' or response_type == 'application/octet-stream':
304345
# API says it's text; but maybe it's actually JSON? - should be fixed in API
305346
try:
306-
response_data = json.loads(response_data)
347+
if hasattr(response_data, 'decode'):
348+
response_data = json.loads(response_data.decode('utf-8'))
349+
else:
350+
response_data = json.loads(response_data)
307351
except ValueError:
308352
# So it wasn't JSON - moving on as if it's text!
309353
# A single value is returned (vs an array or object)
@@ -313,6 +357,14 @@ def _raw(self, method, headers, parts,
313357
else:
314358
# 3xx & 4xx errors
315359
response_data = {'success': False, 'code': response_code, 'result': str(response_data)}
360+
elif response_type == 'text/javascript' or response_type == 'application/javascript':
361+
# used by Cloudflare workers
362+
if response_code == requests.codes.ok:
363+
# 200 ok
364+
response_data = {'success': True, 'result': str(response_data)}
365+
else:
366+
# 3xx & 4xx errors
367+
response_data = {'success': False, 'code': response_code, 'result': str(response_data)}
316368
else:
317369
# Assuming nothing - but continuing anyway
318370
# A single value is returned (vs an array or object)

0 commit comments

Comments
 (0)