diff --git a/CloudFlare/__init__.py b/CloudFlare/__init__.py index 535d4ab..a87a4c4 100644 --- a/CloudFlare/__init__.py +++ b/CloudFlare/__init__.py @@ -3,6 +3,7 @@ from utils import 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 @@ -171,7 +172,7 @@ def __init__(self, email=None, token=None, certtoken=None, debug=False): base_url = BASE_URL # class creation values override configuration values - [ conf_email, conf_token, conf_certtoken ] = read_configs() + [ conf_email, conf_token, conf_certtoken, extras ] = read_configs() if email is None: email = conf_email @@ -188,4 +189,5 @@ def __init__(self, email=None, token=None, certtoken=None, debug=False): # add the API calls api_v4(self) + api_extras(self, extras) diff --git a/CloudFlare/api_extras.py b/CloudFlare/api_extras.py new file mode 100644 index 0000000..9ea1054 --- /dev/null +++ b/CloudFlare/api_extras.py @@ -0,0 +1,43 @@ + +import re + +def api_extras(self, extras=None): + for extra in extras: + if extra == '': + continue + extra = re.sub(r"^.*/client/v4/", '/', extra) + extra = re.sub(r"^.*/v4/", '/', extra) + extra = re.sub(r"^/", '', extra) + current = self + + # build parts of the extra command + parts = [] + nn = 0 + for element in extra.split('/'): + if element[0] == ':': + nn += 1 + continue + try: + parts[nn] + except: + parts.append([]) + parts[nn].append(element) + + # insert extra command into class + element_path = [] + for element in parts[0]: + element_path.append(element) + try: + current = getattr(current, element) + # exists + continue + except: + pass + # does not exist + if element == parts[0][-1] and len(parts) > 1: + # last element + setattr(current, element, self._client_with_auth(self.base, '/'.join(element_path), '/'.join(parts[1]))) + else: + setattr(current, element, self._client_with_auth(self.base, '/'.join(element_path))) + current = getattr(current, element) + diff --git a/CloudFlare/read_configs.py b/CloudFlare/read_configs.py index eff7ae6..9fde05b 100644 --- a/CloudFlare/read_configs.py +++ b/CloudFlare/read_configs.py @@ -32,5 +32,12 @@ def read_configs(): except: certtoken = None - return [ email, token, certtoken ] + try: + extras = re.sub(r"\s+", ' ', config.get('CloudFlare', 'extras')) + except: + extras = None + + extras = extras.split(' ') + + return [ email, token, certtoken, extras ] diff --git a/README.md b/README.md index 26107f9..9e59d91 100644 --- a/README.md +++ b/README.md @@ -440,3 +440,20 @@ $ GET /ips ``` + +## Adding extra API calls manually + +Extra API calls can be added via the configuration file +``` +$ cat ~/.cloudflare/cloudflare.cfg +[CloudFlare] +extras= + /client/v4/command + /client/v4/command/:command_identifier + /client/v4/command/:command_identifier/settings +$ +``` + +While it's easy to call anything within CloudFlare's API, it's not very useful to add items in here as they will simply return API URL errors. +Technically, this is only useful for internal testing within CloudFlare. +