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

Commit 9f61cbc

Browse files
committed
Added support for extra API calls via the configuration file. See README
1 parent 7a42be0 commit 9f61cbc

File tree

4 files changed

+71
-2
lines changed

4 files changed

+71
-2
lines changed

CloudFlare/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from utils import sanitize_secrets
44
from read_configs import read_configs
55
from api_v4 import api_v4
6+
from api_extras import api_extras
67

78
from exceptions import CloudFlareError, CloudFlareAPIError, CloudFlareInternalError
89

@@ -171,7 +172,7 @@ def __init__(self, email=None, token=None, certtoken=None, debug=False):
171172
base_url = BASE_URL
172173

173174
# class creation values override configuration values
174-
[ conf_email, conf_token, conf_certtoken ] = read_configs()
175+
[ conf_email, conf_token, conf_certtoken, extras ] = read_configs()
175176

176177
if email is None:
177178
email = conf_email
@@ -188,4 +189,5 @@ def __init__(self, email=None, token=None, certtoken=None, debug=False):
188189

189190
# add the API calls
190191
api_v4(self)
192+
api_extras(self, extras)
191193

CloudFlare/api_extras.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
import re
3+
4+
def api_extras(self, extras=None):
5+
for extra in extras:
6+
if extra == '':
7+
continue
8+
extra = re.sub(r"^.*/client/v4/", '/', extra)
9+
extra = re.sub(r"^.*/v4/", '/', extra)
10+
extra = re.sub(r"^/", '', extra)
11+
current = self
12+
13+
# build parts of the extra command
14+
parts = []
15+
nn = 0
16+
for element in extra.split('/'):
17+
if element[0] == ':':
18+
nn += 1
19+
continue
20+
try:
21+
parts[nn]
22+
except:
23+
parts.append([])
24+
parts[nn].append(element)
25+
26+
# insert extra command into class
27+
element_path = []
28+
for element in parts[0]:
29+
element_path.append(element)
30+
try:
31+
current = getattr(current, element)
32+
# exists
33+
continue
34+
except:
35+
pass
36+
# does not exist
37+
if element == parts[0][-1] and len(parts) > 1:
38+
# last element
39+
setattr(current, element, self._client_with_auth(self.base, '/'.join(element_path), '/'.join(parts[1])))
40+
else:
41+
setattr(current, element, self._client_with_auth(self.base, '/'.join(element_path)))
42+
current = getattr(current, element)
43+

CloudFlare/read_configs.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,12 @@ def read_configs():
3232
except:
3333
certtoken = None
3434

35-
return [ email, token, certtoken ]
35+
try:
36+
extras = re.sub(r"\s+", ' ', config.get('CloudFlare', 'extras'))
37+
except:
38+
extras = None
39+
40+
extras = extras.split(' ')
41+
42+
return [ email, token, certtoken, extras ]
3643

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,3 +440,20 @@ $
440440
441441
GET /ips
442442
```
443+
444+
## Adding extra API calls manually
445+
446+
Extra API calls can be added via the configuration file
447+
```
448+
$ cat ~/.cloudflare/cloudflare.cfg
449+
[CloudFlare]
450+
extras=
451+
/client/v4/command
452+
/client/v4/command/:command_identifier
453+
/client/v4/command/:command_identifier/settings
454+
$
455+
```
456+
457+
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.
458+
Technically, this is only useful for internal testing within CloudFlare.
459+

0 commit comments

Comments
 (0)