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

Commit 0820e21

Browse files
committed
added code to handle python with construct, cleaned-up parts variable, allowed simple get() call without get method
1 parent 84ece92 commit 0820e21

1 file changed

Lines changed: 113 additions & 97 deletions

File tree

CloudFlare/cloudflare.py

Lines changed: 113 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@ def __init__(self, email, token, certtoken, base_url, debug, raw):
3434
else:
3535
self.logger = None
3636

37-
def call_with_no_auth(self, method,
38-
api_call_part1,
39-
api_call_part2=None,
40-
api_call_part3=None,
37+
def call_with_no_auth(self, method, parts,
4138
identifier1=None, identifier2=None, identifier3=None,
4239
params=None, data=None, files=None):
4340
""" Cloudflare v4 API"""
@@ -46,15 +43,11 @@ def call_with_no_auth(self, method,
4643
'User-Agent': self.user_agent,
4744
'Content-Type': 'application/json'
4845
}
49-
return self._call(method, headers,
50-
api_call_part1, api_call_part2, api_call_part3,
46+
return self._call(method, headers, parts,
5147
identifier1, identifier2, identifier3,
5248
params, data, files)
5349

54-
def call_with_auth(self, method,
55-
api_call_part1,
56-
api_call_part2=None,
57-
api_call_part3=None,
50+
def call_with_auth(self, method, parts,
5851
identifier1=None, identifier2=None, identifier3=None,
5952
params=None, data=None, files=None):
6053
""" Cloudflare v4 API"""
@@ -72,15 +65,11 @@ def call_with_auth(self, method,
7265
headers['Content-Type'] = 'multipart/form-data'
7366
# however something isn't right and this works ... look at again later!
7467
del headers['Content-Type']
75-
return self._call(method, headers,
76-
api_call_part1, api_call_part2, api_call_part3,
68+
return self._call(method, headers, parts,
7769
identifier1, identifier2, identifier3,
7870
params, data, files)
7971

80-
def call_with_certauth(self, method,
81-
api_call_part1,
82-
api_call_part2=None,
83-
api_call_part3=None,
72+
def call_with_certauth(self, method, parts,
8473
identifier1=None, identifier2=None, identifier3=None,
8574
params=None, data=None, files=None):
8675
""" Cloudflare v4 API"""
@@ -92,57 +81,55 @@ def call_with_certauth(self, method,
9281
'X-Auth-User-Service-Key': self.certtoken,
9382
'Content-Type': 'application/json'
9483
}
95-
return self._call(method, headers,
96-
api_call_part1, api_call_part2, api_call_part3,
84+
return self._call(method, headers, parts,
9785
identifier1, identifier2, identifier3,
9886
params, data, files)
9987

100-
def _raw(self, method, headers,
101-
api_call_part1, api_call_part2=None, api_call_part3=None,
88+
def _raw(self, method, headers, parts,
10289
identifier1=None, identifier2=None, identifier3=None,
10390
params=None, data=None, files=None):
10491
""" Cloudflare v4 API"""
10592

10693
if self.logger:
107-
self.logger.debug('Call: %s,%s,%s,%s,%s,%s' % (str(api_call_part1),
94+
self.logger.debug('Call: %s,%s,%s,%s,%s,%s' % (str(parts[0]),
10895
str(identifier1),
109-
str(api_call_part2),
96+
str(parts[1]),
11097
str(identifier2),
111-
str(api_call_part3),
98+
str(parts[2]),
11299
str(identifier3)))
113100
self.logger.debug('Call: optional params and data %s %s' % (str(params),
114101
str(data)))
115102
if files:
116103
self.logger.debug('Call: upload file %r' % (files))
117104

118-
if (method is None) or (api_call_part1 is None):
105+
if (method is None) or (parts[0] is None):
119106
# should never happen
120107
raise CloudFlareInternalError(0, 'You must specify a method and endpoint')
121108

122-
if api_call_part2 is not None or (data is not None and method == 'GET'):
109+
if parts[1] is not None or (data is not None and method == 'GET'):
123110
if identifier1 is None:
124111
raise CloudFlareAPIError(0, 'You must specify identifier1')
125112
if identifier2 is None:
126113
url = (self.base_url + '/'
127-
+ api_call_part1 + '/'
114+
+ parts[0] + '/'
128115
+ identifier1 + '/'
129-
+ api_call_part2)
116+
+ parts[1])
130117
else:
131118
url = (self.base_url + '/'
132-
+ api_call_part1 + '/'
119+
+ parts[0] + '/'
133120
+ identifier1 + '/'
134-
+ api_call_part2 + '/'
121+
+ parts[1] + '/'
135122
+ identifier2)
136123
else:
137124
if identifier1 is None:
138125
url = (self.base_url + '/'
139-
+ api_call_part1)
126+
+ parts[0])
140127
else:
141128
url = (self.base_url + '/'
142-
+ api_call_part1 + '/'
129+
+ parts[0] + '/'
143130
+ identifier1)
144-
if api_call_part3:
145-
url += '/' + api_call_part3
131+
if parts[2]:
132+
url += '/' + parts[2]
146133
if identifier3:
147134
url += '/' + identifier3
148135

@@ -194,7 +181,7 @@ def _raw(self, method, headers,
194181
response_data = response.text
195182

196183
if self.logger:
197-
self.logger.debug('Response: %d, %s %s' % (response_code, response_type, response_data))
184+
self.logger.debug('Response: %d, %s, %s' % (response_code, response_type, response_data))
198185

199186
if response_code >= 500 and response_code <= 599:
200187
# 500 Internal Server Error
@@ -277,16 +264,12 @@ def _raw(self, method, headers,
277264
# it would be nice to return the error code and content type values; but not quite yet
278265
return response_data
279266

280-
def _call(self, method, headers,
281-
api_call_part1,
282-
api_call_part2=None,
283-
api_call_part3=None,
267+
def _call(self, method, headers, parts,
284268
identifier1=None, identifier2=None, identifier3=None,
285269
params=None, data=None, files=None):
286270
""" Cloudflare v4 API"""
287271

288-
response_data = self._raw(method, headers,
289-
api_call_part1, api_call_part2, api_call_part3,
272+
response_data = self._raw(method, headers, parts,
290273
identifier1, identifier2, identifier3,
291274
params, data, files)
292275

@@ -356,9 +339,18 @@ def __init__(self, base, api_call_part1, api_call_part2=None, api_call_part3=Non
356339
""" Cloudflare v4 API"""
357340

358341
self._base = base
359-
# self.api_call_part1 = api_call_part1
360-
# self.api_call_part2 = api_call_part2
361-
# self.api_call_part3 = api_call_part3
342+
self._parts = [api_call_part1, api_call_part2, api_call_part3]
343+
344+
def __call__(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None):
345+
""" Cloudflare v4 API"""
346+
347+
# This is the same as a get()
348+
return self.get(identifier1, identifier2, identifier3, params, data)
349+
350+
def __str__(self):
351+
""" Cloudflare v4 API"""
352+
353+
return '[%s]' % ('/' + '/:id/'.join(filter(None, self._parts)))
362354

363355
def get(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None):
364356
""" Cloudflare v4 API"""
@@ -392,17 +384,23 @@ def __init__(self, base, api_call_part1, api_call_part2=None, api_call_part3=Non
392384
""" Cloudflare v4 API"""
393385

394386
self._base = base
395-
self.api_call_part1 = api_call_part1
396-
self.api_call_part2 = api_call_part2
397-
self.api_call_part3 = api_call_part3
387+
self._parts = [api_call_part1, api_call_part2, api_call_part3]
388+
389+
def __call__(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None):
390+
""" Cloudflare v4 API"""
391+
392+
# This is the same as a get()
393+
return self.get(identifier1, identifier2, identifier3, params, data)
394+
395+
def __str__(self):
396+
""" Cloudflare v4 API"""
397+
398+
return '[%s]' % ('/' + '/:id/'.join(filter(None, self._parts)))
398399

399400
def get(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None):
400401
""" Cloudflare v4 API"""
401402

402-
return self._base.call_with_no_auth('GET',
403-
self.api_call_part1,
404-
self.api_call_part2,
405-
self.api_call_part3,
403+
return self._base.call_with_no_auth('GET', self._parts,
406404
identifier1, identifier2, identifier3,
407405
params, data)
408406

@@ -433,57 +431,51 @@ def __init__(self, base, api_call_part1, api_call_part2=None, api_call_part3=Non
433431
""" Cloudflare v4 API"""
434432

435433
self._base = base
436-
self.api_call_part1 = api_call_part1
437-
self.api_call_part2 = api_call_part2
438-
self.api_call_part3 = api_call_part3
434+
self._parts = [api_call_part1, api_call_part2, api_call_part3]
435+
436+
def __call__(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None):
437+
""" Cloudflare v4 API"""
438+
439+
# This is the same as a get()
440+
return self.get(identifier1, identifier2, identifier3, params, data)
441+
442+
def __str__(self):
443+
""" Cloudflare v4 API"""
444+
445+
return '[%s]' % ('/' + '/:id/'.join(filter(None, self._parts)))
439446

440447
def get(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None):
441448
""" Cloudflare v4 API"""
442449

443-
return self._base.call_with_auth('GET',
444-
self.api_call_part1,
445-
self.api_call_part2,
446-
self.api_call_part3,
450+
return self._base.call_with_auth('GET', self._parts,
447451
identifier1, identifier2, identifier3,
448452
params, data)
449453

450454
def patch(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None):
451455
""" Cloudflare v4 API"""
452456

453-
return self._base.call_with_auth('PATCH',
454-
self.api_call_part1,
455-
self.api_call_part2,
456-
self.api_call_part3,
457+
return self._base.call_with_auth('PATCH', self._parts,
457458
identifier1, identifier2, identifier3,
458459
params, data)
459460

460461
def post(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None, files=None):
461462
""" Cloudflare v4 API"""
462463

463-
return self._base.call_with_auth('POST',
464-
self.api_call_part1,
465-
self.api_call_part2,
466-
self.api_call_part3,
464+
return self._base.call_with_auth('POST', self._parts,
467465
identifier1, identifier2, identifier3,
468466
params, data, files)
469467

470468
def put(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None):
471469
""" Cloudflare v4 API"""
472470

473-
return self._base.call_with_auth('PUT',
474-
self.api_call_part1,
475-
self.api_call_part2,
476-
self.api_call_part3,
471+
return self._base.call_with_auth('PUT', self._parts,
477472
identifier1, identifier2, identifier3,
478473
params, data)
479474

480475
def delete(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None):
481476
""" Cloudflare v4 API"""
482477

483-
return self._base.call_with_auth('DELETE',
484-
self.api_call_part1,
485-
self.api_call_part2,
486-
self.api_call_part3,
478+
return self._base.call_with_auth('DELETE', self._parts,
487479
identifier1, identifier2, identifier3,
488480
params, data)
489481

@@ -494,57 +486,51 @@ def __init__(self, base, api_call_part1, api_call_part2=None, api_call_part3=Non
494486
""" Cloudflare v4 API"""
495487

496488
self._base = base
497-
self.api_call_part1 = api_call_part1
498-
self.api_call_part2 = api_call_part2
499-
self.api_call_part3 = api_call_part3
489+
self._parts = [api_call_part1, api_call_part2, api_call_part3]
490+
491+
def __call__(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None):
492+
""" Cloudflare v4 API"""
493+
494+
# This is the same as a get()
495+
return self.get(identifier1, identifier2, identifier3, params, data)
496+
497+
def __str__(self):
498+
""" Cloudflare v4 API"""
499+
500+
return '[%s]' % ('/' + '/:id/'.join(filter(None, self._parts)))
500501

501502
def get(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None):
502503
""" Cloudflare v4 API"""
503504

504-
return self._base.call_with_certauth('GET',
505-
self.api_call_part1,
506-
self.api_call_part2,
507-
self.api_call_part3,
505+
return self._base.call_with_certauth('GET', self._parts,
508506
identifier1, identifier2, identifier3,
509507
params, data)
510508

511509
def patch(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None):
512510
""" Cloudflare v4 API"""
513511

514-
return self._base.call_with_certauth('PATCH',
515-
self.api_call_part1,
516-
self.api_call_part2,
517-
self.api_call_part3,
512+
return self._base.call_with_certauth('PATCH', self._parts,
518513
identifier1, identifier2, identifier3,
519514
params, data)
520515

521516
def post(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None, files=None):
522517
""" Cloudflare v4 API"""
523518

524-
return self._base.call_with_certauth('POST',
525-
self.api_call_part1,
526-
self.api_call_part2,
527-
self.api_call_part3,
519+
return self._base.call_with_certauth('POST', self._parts,
528520
identifier1, identifier2, identifier3,
529521
params, data, files)
530522

531523
def put(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None):
532524
""" Cloudflare v4 API"""
533525

534-
return self._base.call_with_certauth('PUT',
535-
self.api_call_part1,
536-
self.api_call_part2,
537-
self.api_call_part3,
526+
return self._base.call_with_certauth('PUT', self._parts,
538527
identifier1, identifier2, identifier3,
539528
params, data)
540529

541530
def delete(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None):
542531
""" Cloudflare v4 API"""
543532

544-
return self._base.call_with_certauth('DELETE',
545-
self.api_call_part1,
546-
self.api_call_part2,
547-
self.api_call_part3,
533+
return self._base.call_with_certauth('DELETE', self._parts,
548534
identifier1, identifier2, identifier3,
549535
params, data)
550536

@@ -566,7 +552,7 @@ def api_list(self, m=None, s=''):
566552
# it's a known api call - lets show the result and continue down the tree
567553
if 'delete' in d or 'get' in d or 'patch' in d or 'post' in d or 'put' in d:
568554
# only show the result if a call exists for this part
569-
if 'api_call_part1' in d:
555+
if '_parts' in d:
570556
w.append(s + '/' + n)
571557
w = w + self.api_list(a, s + '/' + n)
572558
return w
@@ -592,3 +578,33 @@ def __init__(self, email=None, token=None, certtoken=None, debug=False, raw=Fals
592578
api_v4(self)
593579
if extras:
594580
api_extras(self, extras)
581+
582+
def __call__(self):
583+
""" Cloudflare v4 API"""
584+
585+
raise TypeError('object is not callable')
586+
587+
def __enter__(self):
588+
""" Cloudflare v4 API"""
589+
return self
590+
591+
def __exit__(self, t, v, tb):
592+
""" Cloudflare v4 API"""
593+
if t is None:
594+
return True
595+
# pretend we didn't deal with raised error - which is true
596+
return False
597+
598+
def __str__(self):
599+
""" Cloudflare v4 API"""
600+
601+
return '["%s","%s"]' % (self._base.email, "REDACTED")
602+
603+
def __repr__(self):
604+
""" Cloudflare v4 API"""
605+
606+
return '%s,%s(%s,"%s","%s","%s",%s,"%s")' % (
607+
self.__module__, type(self).__name__,
608+
self._base.email, 'REDACTED', 'REDACTED',
609+
self._base.base_url, self._base.raw, self._base.user_agent
610+
)

0 commit comments

Comments
 (0)