/usr/lib/python3/dist-packages/CloudFlare/cloudflare.py is in python3-cloudflare 2.0.4-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 | """ Cloudflare v4 API"""
from __future__ import absolute_import
import json
import requests
from .logging_helper import CFlogger
from .utils import user_agent, sanitize_secrets
from .read_configs import read_configs
from .api_v4 import api_v4
from .api_extras import api_extras
from .exceptions import CloudFlareError, CloudFlareAPIError, CloudFlareInternalError
BASE_URL = 'https://api.cloudflare.com/client/v4'
class CloudFlare(object):
""" Cloudflare v4 API"""
class _v4base(object):
""" Cloudflare v4 API"""
def __init__(self, email, token, certtoken, base_url, debug, raw, use_sessions):
""" Cloudflare v4 API"""
self.email = email
self.token = token
self.certtoken = certtoken
self.base_url = base_url
self.raw = raw
self.use_sessions = use_sessions
self.session = None
self.user_agent = user_agent()
if debug:
self.logger = CFlogger(debug).getLogger()
else:
self.logger = None
def call_with_no_auth(self, method, parts,
identifier1=None, identifier2=None, identifier3=None,
params=None, data=None, files=None):
""" Cloudflare v4 API"""
headers = {
'User-Agent': self.user_agent,
'Content-Type': 'application/json'
}
return self._call(method, headers, parts,
identifier1, identifier2, identifier3,
params, data, files)
def call_with_auth(self, method, parts,
identifier1=None, identifier2=None, identifier3=None,
params=None, data=None, files=None):
""" Cloudflare v4 API"""
if self.email is '' or self.token is '':
raise CloudFlareAPIError(0, 'no email and/or token defined')
headers = {
'User-Agent': self.user_agent,
'X-Auth-Email': self.email,
'X-Auth-Key': self.token,
'Content-Type': 'application/json'
}
if type(data) == str:
# passing javascript vs JSON
headers['Content-Type'] = 'application/javascript'
if files:
# overwrite Content-Type as we are uploading data
headers['Content-Type'] = 'multipart/form-data'
# however something isn't right and this works ... look at again later!
del headers['Content-Type']
return self._call(method, headers, parts,
identifier1, identifier2, identifier3,
params, data, files)
def call_with_auth_unwrapped(self, method, parts,
identifier1=None, identifier2=None, identifier3=None,
params=None, data=None, files=None):
""" Cloudflare v4 API"""
if self.email is '' or self.token is '':
raise CloudFlareAPIError(0, 'no email and/or token defined')
headers = {
'User-Agent': self.user_agent,
'X-Auth-Email': self.email,
'X-Auth-Key': self.token,
'Content-Type': 'application/json'
}
if type(data) == str:
# passing javascript vs JSON
headers['Content-Type'] = 'application/javascript'
if files:
# overwrite Content-Type as we are uploading data
headers['Content-Type'] = 'multipart/form-data'
# however something isn't right and this works ... look at again later!
del headers['Content-Type']
return self._call_unwrapped(method, headers, parts,
identifier1, identifier2, identifier3,
params, data, files)
def call_with_certauth(self, method, parts,
identifier1=None, identifier2=None, identifier3=None,
params=None, data=None, files=None):
""" Cloudflare v4 API"""
if self.certtoken is '' or self.certtoken is None:
raise CloudFlareAPIError(0, 'no cert token defined')
headers = {
'User-Agent': self.user_agent,
'X-Auth-User-Service-Key': self.certtoken,
'Content-Type': 'application/json'
}
return self._call(method, headers, parts,
identifier1, identifier2, identifier3,
params, data, files)
def _network(self, method, headers, parts,
identifier1=None, identifier2=None, identifier3=None,
params=None, data=None, files=None):
""" Cloudflare v4 API"""
if self.logger:
self.logger.debug('Call: %s,%s,%s,%s,%s,%s' % (str(parts[0]),
str(identifier1),
str(parts[1]),
str(identifier2),
str(parts[2]),
str(identifier3)))
self.logger.debug('Call: optional params and data %s %s' % (str(params),
str(data)))
if files:
self.logger.debug('Call: upload file %r' % (files))
if (method is None) or (parts[0] is None):
# should never happen
raise CloudFlareInternalError(0, 'You must specify a method and endpoint')
if parts[1] is not None or (data is not None and method == 'GET'):
if identifier1 is None:
raise CloudFlareAPIError(0, 'You must specify identifier1')
if identifier2 is None:
url = (self.base_url + '/'
+ parts[0] + '/'
+ identifier1 + '/'
+ parts[1])
else:
url = (self.base_url + '/'
+ parts[0] + '/'
+ identifier1 + '/'
+ parts[1] + '/'
+ identifier2)
else:
if identifier1 is None:
url = (self.base_url + '/'
+ parts[0])
else:
url = (self.base_url + '/'
+ parts[0] + '/'
+ identifier1)
if parts[2]:
url += '/' + parts[2]
if identifier3:
url += '/' + identifier3
if self.logger:
self.logger.debug('Call: method and url %s %s' % (str(method), str(url)))
self.logger.debug('Call: headers %s' % str(sanitize_secrets(headers)))
method = method.upper()
if self.logger:
self.logger.debug('Call: doit!')
if self.use_sessions:
if self.session is None:
self.session = requests.Session()
else:
self.session = requests
try:
if method == 'GET':
response = self.session.get(url,
headers=headers,
params=params,
data=data)
elif method == 'POST':
if type(data) == str:
response = self.session.post(url,
headers=headers,
params=params,
data=data,
files=files)
else:
response = self.session.post(url,
headers=headers,
params=params,
json=data,
files=files)
elif method == 'PUT':
if type(data) == str:
response = self.session.put(url,
headers=headers,
params=params,
data=data)
else:
response = self.session.put(url,
headers=headers,
params=params,
json=data)
elif method == 'DELETE':
if type(data) == str:
response = self.session.delete(url,
headers=headers,
params=params,
data=data)
else:
response = self.session.delete(url,
headers=headers,
params=params,
json=data)
elif method == 'PATCH':
if type(data) == str:
response = self.session.request('PATCH', url,
headers=headers,
params=params,
data=data)
else:
response = self.session.request('PATCH', url,
headers=headers,
params=params,
json=data)
else:
# should never happen
raise CloudFlareAPIError(0, 'method not supported')
if self.logger:
self.logger.debug('Call: done!')
except Exception as e:
if self.logger:
self.logger.debug('Call: exception!')
raise CloudFlareAPIError(0, 'connection failed.')
if self.logger:
self.logger.debug('Response: url %s', response.url)
# Create response_{type|code|data}
try:
response_type = response.headers['Content-Type']
if ';' in response_type:
# remove the ;paramaters part (like charset=, etc.)
response_type = response_type[0:response_type.rfind(';')]
response_type = response_type.strip().lower()
except:
# API should always response; but if it doesn't; here's the default
response_type = 'application/octet-stream'
response_code = response.status_code
response_data = response.content
if type(response_data) != str:
response_data = response_data.decode("utf-8")
if self.logger:
self.logger.debug('Response: %d, %s, %s' % (response_code, response_type, response_data))
if response_code >= 500 and response_code <= 599:
# 500 Internal Server Error
# 501 Not Implemented
# 502 Bad Gateway
# 503 Service Unavailable
# 504 Gateway Timeout
# 505 HTTP Version Not Supported
# 506 Variant Also Negotiates
# 507 Insufficient Storage
# 508 Loop Detected
# 509 Unassigned
# 510 Not Extended
# 511 Network Authentication Required
# the libary doesn't deal with these errors, just pass upwards!
# there's no value to add and the returned data is questionable or not useful
response.raise_for_status()
# should not be reached
raise CloudFlareInternalError(0, 'internal error in status code processing')
#if response_code >= 400 and response_code <= 499:
# # 400 Bad Request
# # 401 Unauthorized
# # 403 Forbidden
# # 405 Method Not Allowed
# # 415 Unsupported Media Type
# # 429 Too many requests
#
# # don't deal with these errors, just pass upwards!
# response.raise_for_status()
#
#if response_code >= 300 and response_code <= 399:
# # 304 Not Modified
#
# # don't deal with these errors, just pass upwards!
# response.raise_for_status()
#
# should be a 200 response at this point
return [response_type, response_code, response_data]
def _raw(self, method, headers, parts,
identifier1=None, identifier2=None, identifier3=None,
params=None, data=None, files=None):
""" Cloudflare v4 API"""
[response_type, response_code, response_data] = self._network(method,
headers, parts,
identifier1, identifier2, identifier3,
params, data, files)
if response_type == 'application/json':
# API says it's JSON; so it better be parsable as JSON
try:
if hasattr(response_data, 'decode'):
response_data = json.loads(response_data.decode('utf-8'))
else:
response_data = json.loads(response_data)
except ValueError:
if response_data == '':
# This should really be 'null' but it isn't. Even then, it's wrong!
if response_code == requests.codes.ok:
# 200 ok
response_data = {'success': True, 'result': None}
else:
# 3xx & 4xx errors
response_data = {'success': False, 'code': response_code, 'result': None}
else:
# While this should not happen; it's always possible
self.logger.debug('Response data not JSON: %r' % (response_data))
raise CloudFlareAPIError(0, 'JSON parse failed - report to Cloudflare.')
if response_code == requests.codes.ok:
# 200 ok - so nothing needs to be done
pass
else:
# 3xx & 4xx errors - we should report that somehow - but not quite yet
# response_data['code'] = response_code
pass
elif response_type == 'text/plain' or response_type == 'application/octet-stream':
# API says it's text; but maybe it's actually JSON? - should be fixed in API
try:
if hasattr(response_data, 'decode'):
response_data = json.loads(response_data.decode('utf-8'))
else:
response_data = json.loads(response_data)
except ValueError:
# So it wasn't JSON - moving on as if it's text!
# A single value is returned (vs an array or object)
if response_code == requests.codes.ok:
# 200 ok
response_data = {'success': True, 'result': str(response_data)}
else:
# 3xx & 4xx errors
response_data = {'success': False, 'code': response_code, 'result': str(response_data)}
elif response_type == 'text/javascript' or response_type == 'application/javascript':
# used by Cloudflare workers
if response_code == requests.codes.ok:
# 200 ok
response_data = {'success': True, 'result': str(response_data)}
else:
# 3xx & 4xx errors
response_data = {'success': False, 'code': response_code, 'result': str(response_data)}
else:
# Assuming nothing - but continuing anyway
# A single value is returned (vs an array or object)
if response_code == requests.codes.ok:
# 200 ok
response_data = {'success': True, 'result': str(response_data)}
else:
# 3xx & 4xx errors
response_data = {'success': False, 'code': response_code, 'result': str(response_data)}
# it would be nice to return the error code and content type values; but not quite yet
return response_data
def _call(self, method, headers, parts,
identifier1=None, identifier2=None, identifier3=None,
params=None, data=None, files=None):
""" Cloudflare v4 API"""
response_data = self._raw(method, headers, parts,
identifier1, identifier2, identifier3,
params, data, files)
# Sanatize the returned results - just in case API is messed up
if 'success' not in response_data:
if 'errors' in response_data:
if self.logger:
self.logger.debug('Response: assuming success = "False"')
response_data['success'] = False
else:
if 'result' not in response_data:
# Only happens on /certificates call
# should be fixed in /certificates API
if self.logger:
self.logger.debug('Response: assuming success = "False"')
r = response_data
response_data['errors'] = []
response_data['errors'].append(r)
response_data['success'] = False
else:
if self.logger:
self.logger.debug('Response: assuming success = "True"')
response_data['success'] = True
if response_data['success'] is False:
errors = response_data['errors'][0]
code = errors['code']
if 'message' in errors:
message = errors['message']
elif 'error' in errors:
message = errors['error']
else:
message = ''
if 'error_chain' in errors:
error_chain = errors['error_chain']
for error in error_chain:
if self.logger:
self.logger.debug('Response: error %d %s - chain' %
(error['code'], error['message']))
if self.logger:
self.logger.debug('Response: error %d %s' % (code, message))
raise CloudFlareAPIError(code, message, error_chain)
else:
if self.logger:
self.logger.debug('Response: error %d %s' % (code, message))
raise CloudFlareAPIError(code, message)
if self.logger:
self.logger.debug('Response: %s' % (response_data['result']))
if self.raw:
result = {}
# theres always a result value
result['result'] = response_data['result']
# theres may not be a result_info on every call
if 'result_info' in response_data:
result['result_info'] = response_data['result_info']
# no need to return success, errors, or messages as they return via an exception
else:
# theres always a result value
result = response_data['result']
return result
def _call_unwrapped(self, method, headers, parts,
identifier1=None, identifier2=None, identifier3=None,
params=None, data=None, files=None):
""" Cloudflare v4 API"""
response_data = self._raw(method, headers, parts,
identifier1, identifier2, identifier3,
params, data, files)
if self.logger:
self.logger.debug('Response: %s' % (response_data))
result = response_data
return result
class _add_unused(object):
""" Cloudflare v4 API"""
def __init__(self, base, api_call_part1, api_call_part2=None, api_call_part3=None):
""" Cloudflare v4 API"""
self._base = base
self._parts_unused = [api_call_part1, api_call_part2, api_call_part3]
def __call__(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None):
""" Cloudflare v4 API"""
# This is the same as a get()
return self.get(identifier1, identifier2, identifier3, params, data)
def __str__(self):
""" Cloudflare v4 API"""
return '[%s]' % ('/' + '/:id/'.join(filter(None, self._parts_unused)))
def get(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None):
""" Cloudflare v4 API"""
raise CloudFlareAPIError(0, 'get() call not available for this endpoint')
def patch(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None):
""" Cloudflare v4 API"""
raise CloudFlareAPIError(0, 'patch() call not available for this endpoint')
def post(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None):
""" Cloudflare v4 API"""
raise CloudFlareAPIError(0, 'post() call not available for this endpoint')
def put(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None):
""" Cloudflare v4 API"""
raise CloudFlareAPIError(0, 'put() call not available for this endpoint')
def delete(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None):
""" Cloudflare v4 API"""
raise CloudFlareAPIError(0, 'delete() call not available for this endpoint')
class _add_noauth(object):
""" Cloudflare v4 API"""
def __init__(self, base, api_call_part1, api_call_part2=None, api_call_part3=None):
""" Cloudflare v4 API"""
self._base = base
self._parts = [api_call_part1, api_call_part2, api_call_part3]
def __call__(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None):
""" Cloudflare v4 API"""
# This is the same as a get()
return self.get(identifier1, identifier2, identifier3, params, data)
def __str__(self):
""" Cloudflare v4 API"""
return '[%s]' % ('/' + '/:id/'.join(filter(None, self._parts)))
def get(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None):
""" Cloudflare v4 API"""
return self._base.call_with_no_auth('GET', self._parts,
identifier1, identifier2, identifier3,
params, data)
def patch(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None):
""" Cloudflare v4 API"""
raise CloudFlareAPIError(0, 'patch() call not available for this endpoint')
def post(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None):
""" Cloudflare v4 API"""
raise CloudFlareAPIError(0, 'post() call not available for this endpoint')
def put(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None):
""" Cloudflare v4 API"""
raise CloudFlareAPIError(0, 'put() call not available for this endpoint')
def delete(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None):
""" Cloudflare v4 API"""
raise CloudFlareAPIError(0, 'delete() call not available for this endpoint')
class _add_with_auth(object):
""" Cloudflare v4 API"""
def __init__(self, base, api_call_part1, api_call_part2=None, api_call_part3=None):
""" Cloudflare v4 API"""
self._base = base
self._parts = [api_call_part1, api_call_part2, api_call_part3]
def __call__(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None):
""" Cloudflare v4 API"""
# This is the same as a get()
return self.get(identifier1, identifier2, identifier3, params, data)
def __str__(self):
""" Cloudflare v4 API"""
return '[%s]' % ('/' + '/:id/'.join(filter(None, self._parts)))
def get(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None):
""" Cloudflare v4 API"""
return self._base.call_with_auth('GET', self._parts,
identifier1, identifier2, identifier3,
params, data)
def patch(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None):
""" Cloudflare v4 API"""
return self._base.call_with_auth('PATCH', self._parts,
identifier1, identifier2, identifier3,
params, data)
def post(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None, files=None):
""" Cloudflare v4 API"""
return self._base.call_with_auth('POST', self._parts,
identifier1, identifier2, identifier3,
params, data, files)
def put(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None):
""" Cloudflare v4 API"""
return self._base.call_with_auth('PUT', self._parts,
identifier1, identifier2, identifier3,
params, data)
def delete(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None):
""" Cloudflare v4 API"""
return self._base.call_with_auth('DELETE', self._parts,
identifier1, identifier2, identifier3,
params, data)
class _add_with_auth_unwrapped(object):
""" Cloudflare v4 API"""
def __init__(self, base, api_call_part1, api_call_part2=None, api_call_part3=None):
""" Cloudflare v4 API"""
self._base = base
self._parts = [api_call_part1, api_call_part2, api_call_part3]
def __call__(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None):
""" Cloudflare v4 API"""
# This is the same as a get()
return self.get(identifier1, identifier2, identifier3, params, data)
def __str__(self):
""" Cloudflare v4 API"""
return '[%s]' % ('/' + '/:id/'.join(filter(None, self._parts)))
def get(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None):
""" Cloudflare v4 API"""
return self._base.call_with_auth_unwrapped('GET', self._parts,
identifier1, identifier2, identifier3,
params, data)
def patch(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None):
""" Cloudflare v4 API"""
return self._base.call_with_auth_unwrapped('PATCH', self._parts,
identifier1, identifier2, identifier3,
params, data)
def post(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None, files=None):
""" Cloudflare v4 API"""
return self._base.call_with_auth_unwrapped('POST', self._parts,
identifier1, identifier2, identifier3,
params, data, files)
def put(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None):
""" Cloudflare v4 API"""
return self._base.call_with_auth_unwrapped('PUT', self._parts,
identifier1, identifier2, identifier3,
params, data)
def delete(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None):
""" Cloudflare v4 API"""
return self._base.call_with_auth_unwrapped('DELETE', self._parts,
identifier1, identifier2, identifier3,
params, data)
class _add_with_cert_auth(object):
""" Cloudflare v4 API"""
def __init__(self, base, api_call_part1, api_call_part2=None, api_call_part3=None):
""" Cloudflare v4 API"""
self._base = base
self._parts = [api_call_part1, api_call_part2, api_call_part3]
def __call__(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None):
""" Cloudflare v4 API"""
# This is the same as a get()
return self.get(identifier1, identifier2, identifier3, params, data)
def __str__(self):
""" Cloudflare v4 API"""
return '[%s]' % ('/' + '/:id/'.join(filter(None, self._parts)))
def get(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None):
""" Cloudflare v4 API"""
return self._base.call_with_certauth('GET', self._parts,
identifier1, identifier2, identifier3,
params, data)
def patch(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None):
""" Cloudflare v4 API"""
return self._base.call_with_certauth('PATCH', self._parts,
identifier1, identifier2, identifier3,
params, data)
def post(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None, files=None):
""" Cloudflare v4 API"""
return self._base.call_with_certauth('POST', self._parts,
identifier1, identifier2, identifier3,
params, data, files)
def put(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None):
""" Cloudflare v4 API"""
return self._base.call_with_certauth('PUT', self._parts,
identifier1, identifier2, identifier3,
params, data)
def delete(self, identifier1=None, identifier2=None, identifier3=None, params=None, data=None):
""" Cloudflare v4 API"""
return self._base.call_with_certauth('DELETE', self._parts,
identifier1, identifier2, identifier3,
params, data)
def api_list(self, m=None, s=''):
"""recursive walk of the api tree returning a list of api calls"""
if m is None:
m = self
w = []
for n in sorted(dir(m)):
if n[0] == '_':
# internal
continue
if n in ['delete', 'get', 'patch', 'post', 'put']:
# gone too far
continue
a = getattr(m, n)
d = dir(a)
if '_base' in d:
# it's a known api call - lets show the result and continue down the tree
if 'delete' in d or 'get' in d or 'patch' in d or 'post' in d or 'put' in d:
# only show the result if a call exists for this part
if '_parts' in d:
w.append(s + '/' + n)
w = w + self.api_list(a, s + '/' + n)
return w
def __init__(self, email=None, token=None, certtoken=None, debug=False, raw=False, use_sessions=True):
""" Cloudflare v4 API"""
base_url = BASE_URL
# class creation values override configuration values
[conf_email, conf_token, conf_certtoken, extras] = read_configs()
if email is None:
email = conf_email
if token is None:
token = conf_token
if certtoken is None:
certtoken = conf_certtoken
self._base = self._v4base(email, token, certtoken, base_url, debug, raw, use_sessions)
# add the API calls
api_v4(self)
if extras:
api_extras(self, extras)
def __call__(self):
""" Cloudflare v4 API"""
raise TypeError('object is not callable')
def __enter__(self):
""" Cloudflare v4 API"""
return self
def __exit__(self, t, v, tb):
""" Cloudflare v4 API"""
if t is None:
return True
# pretend we didn't deal with raised error - which is true
return False
def __str__(self):
""" Cloudflare v4 API"""
return '["%s","%s"]' % (self._base.email, "REDACTED")
def __repr__(self):
""" Cloudflare v4 API"""
return '%s,%s(%s,"%s","%s","%s",%s,"%s")' % (
self.__module__, type(self).__name__,
self._base.email, 'REDACTED', 'REDACTED',
self._base.base_url, self._base.raw, self._base.user_agent
)
|