|
| 1 | +""" API extras for CloudFlare API""" |
1 | 2 |
|
2 | 3 | import re |
3 | 4 |
|
4 | 5 | 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) |
| 6 | + """ API extras for CloudFlare API""" |
11 | 7 |
|
12 | | - # build parts of the extra command |
13 | | - parts = [] |
14 | | - nn = 0 |
15 | | - for element in extra.split('/'): |
16 | | - if element[0] == ':': |
17 | | - nn += 1 |
18 | | - continue |
19 | | - try: |
20 | | - parts[nn] |
21 | | - except: |
22 | | - parts.append([]) |
23 | | - parts[nn].append(element) |
| 8 | + for extra in extras: |
| 9 | + if extra == '': |
| 10 | + continue |
| 11 | + extra = re.sub(r"^.*/client/v4/", '/', extra) |
| 12 | + extra = re.sub(r"^.*/v4/", '/', extra) |
| 13 | + extra = re.sub(r"^/", '', extra) |
24 | 14 |
|
25 | | - # insert extra command into class |
26 | | - element_path = [] |
27 | | - current = self |
28 | | - for element in parts[0]: |
29 | | - element_path.append(element) |
30 | | - try: |
31 | | - m = getattr(current, element) |
32 | | - # exists - but still add it there's a second part |
33 | | - if element == parts[0][-1] and len(parts) > 1: |
34 | | - setattr(m, parts[1][0], self._client_with_auth(self.base, '/'.join(element_path), '/'.join(parts[1]))) |
35 | | - current = m |
36 | | - continue |
37 | | - except: |
38 | | - pass |
39 | | - # does not exist |
40 | | - if element == parts[0][-1] and len(parts) > 1: |
41 | | - # last element |
42 | | - setattr(current, element, self._client_with_auth(self.base, '/'.join(element_path), '/'.join(parts[1]))) |
43 | | - else: |
44 | | - setattr(current, element, self._client_with_auth(self.base, '/'.join(element_path))) |
45 | | - current = getattr(current, element) |
| 15 | + # build parts of the extra command |
| 16 | + parts = [] |
| 17 | + nn = 0 |
| 18 | + for element in extra.split('/'): |
| 19 | + 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], self._client_with_auth(self.base, api_call_part1, api_call_part2)) |
| 40 | + current = m |
| 41 | + continue |
| 42 | + except: |
| 43 | + pass |
| 44 | + # does not exist |
| 45 | + if element == parts[0][-1] and len(parts) > 1: |
| 46 | + # last element |
| 47 | + api_call_part1 = '/'.join(element_path) |
| 48 | + api_call_part2 = '/'.join(parts[1]) |
| 49 | + setattr(current, element, self._client_with_auth(self.base, api_call_part1, api_call_part2)) |
| 50 | + else: |
| 51 | + api_call_part1 = '/'.join(element_path) |
| 52 | + setattr(current, element, self._client_with_auth(self.base, api_call_part1)) |
| 53 | + current = getattr(current, element) |
46 | 54 |
|
0 commit comments