|
2 | 2 |
|
3 | 3 | import re |
4 | 4 |
|
| 5 | +from .exceptions import CloudFlareAPIError |
| 6 | + |
5 | 7 | def api_extras(self, extras=None): |
6 | 8 | """ API extras for Cloudflare API""" |
7 | 9 |
|
| 10 | + count = 0; |
8 | 11 | for extra in extras: |
9 | | - if extra == '': |
10 | | - continue |
11 | 12 | extra = re.sub(r"^.*/client/v4/", '/', extra) |
12 | 13 | extra = re.sub(r"^.*/v4/", '/', extra) |
13 | 14 | extra = re.sub(r"^/", '', extra) |
| 15 | + if extra == '': |
| 16 | + continue |
14 | 17 |
|
15 | 18 | # build parts of the extra command |
16 | 19 | parts = [] |
17 | | - nn = 0 |
| 20 | + part = None |
18 | 21 | for element in extra.split('/'): |
19 | 22 | if element[0] == ':': |
20 | | - nn += 1 |
21 | | - continue |
22 | | - try: |
23 | | - parts[nn] |
24 | | - except IndexError: |
25 | | - parts.append([]) |
26 | | - parts[nn].append(element) |
27 | | - |
28 | | - # insert extra command into class |
29 | | - element_path = [] |
30 | | - current = self |
31 | | - for element in parts[0]: |
32 | | - element_path.append(element) |
33 | | - try: |
34 | | - m = getattr(current, element) |
35 | | - # exists - but still add it there's a second part |
36 | | - if element == parts[0][-1] and len(parts) > 1: |
37 | | - api_call_part1 = '/'.join(element_path) |
38 | | - api_call_part2 = '/'.join(parts[1]) |
39 | | - setattr(m, parts[1][0], |
40 | | - self._AddWithAuth(self._base, api_call_part1, api_call_part2)) |
41 | | - current = m |
| 23 | + parts.append(part) |
| 24 | + part = None |
42 | 25 | continue |
43 | | - except: |
44 | | - pass |
45 | | - # does not exist |
46 | | - if element == parts[0][-1] and len(parts) > 1: |
47 | | - # last element |
48 | | - api_call_part1 = '/'.join(element_path) |
49 | | - api_call_part2 = '/'.join(parts[1]) |
50 | | - setattr(current, element, |
51 | | - self._AddWithAuth(self._base, api_call_part1, api_call_part2)) |
| 26 | + if part: |
| 27 | + part += '/' + element |
52 | 28 | else: |
53 | | - api_call_part1 = '/'.join(element_path) |
54 | | - setattr(current, element, |
55 | | - self._AddWithAuth(self._base, api_call_part1)) |
56 | | - current = getattr(current, element) |
| 29 | + part = element |
| 30 | + if part: |
| 31 | + parts.append(part) |
| 32 | + |
| 33 | + if len(parts) > 1: |
| 34 | + p = parts[1].split('/') |
| 35 | + for nn in range(0, len(p)): |
| 36 | + try: |
| 37 | + self.add('VOID', parts[0], '/'.join(p[0:nn])) |
| 38 | + except CloudFlareAPIError: |
| 39 | + # already exists - this is ok |
| 40 | + pass |
| 41 | + |
| 42 | + if len(parts) > 2: |
| 43 | + p = parts[2].split('/') |
| 44 | + for nn in range(0, len(p)): |
| 45 | + try: |
| 46 | + self.add('VOID', parts[0], parts[1], '/'.join(p[0:nn])) |
| 47 | + except CloudFlareAPIError: |
| 48 | + # already exists - this is ok |
| 49 | + pass |
| 50 | + |
| 51 | + while len(parts) < 3: |
| 52 | + parts.append(None) |
| 53 | + |
| 54 | + # we can only add AUTH elements presently |
| 55 | + try: |
| 56 | + self.add('AUTH', parts[0], parts[1], parts[2]) |
| 57 | + count += 1 |
| 58 | + except CloudFlareAPIError: |
| 59 | + # this is silently dropped - however, that could change |
| 60 | + pass |
| 61 | + |
| 62 | + return count |
0 commit comments