Skip to content

Commit

Permalink
Added support for extra API calls via the configuration file. See README
Browse files Browse the repository at this point in the history
  • Loading branch information
mahtin committed May 4, 2016
1 parent 7a42be0 commit 9f61cbc
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 2 deletions.
4 changes: 3 additions & 1 deletion CloudFlare/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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)

43 changes: 43 additions & 0 deletions CloudFlare/api_extras.py
Original file line number Diff line number Diff line change
@@ -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)

9 changes: 8 additions & 1 deletion CloudFlare/read_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]

17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

0 comments on commit 9f61cbc

Please sign in to comment.