|
14 | 14 | BASE_URL = 'https://api.cloudflare.com/client/v4' |
15 | 15 |
|
16 | 16 | class CloudFlare(object): |
17 | | - class _base: |
18 | | - def __init__(self, email, token, certtoken, base_url, debug): |
19 | | - self.EMAIL = email |
20 | | - self.TOKEN = token |
21 | | - self.CERTTOKEN = certtoken |
22 | | - self.BASE_URL = base_url |
23 | | - |
24 | | - if debug: |
25 | | - self.logger = logger.Logger(debug).getLogger() |
26 | | - else: |
27 | | - self.logger = None |
28 | | - |
29 | | - def _call_with_no_auth(self, method, api_call_part1, api_call_part2=None, api_call_part3=None, identifier1=None, identifier2=None, params=None, data=None): |
30 | | - headers = {} |
31 | | - return self._call(method, headers, api_call_part1, api_call_part2, api_call_part3, identifier1, identifier2, params, data) |
32 | | - |
33 | | - def _call_with_auth(self, method, api_call_part1, api_call_part2=None, api_call_part3=None, identifier1=None, identifier2=None, params=None, data=None): |
34 | | - if self.EMAIL is '' or self.TOKEN is '': |
35 | | - raise CloudFlareAPIError(0, 'no email and/or token defined') |
36 | | - headers = { "X-Auth-Email": self.EMAIL, "X-Auth-Key": self.TOKEN, 'Content-Type': 'application/json' } |
37 | | - return self._call(method, headers, api_call_part1, api_call_part2, api_call_part3, identifier1, identifier2, params, data) |
38 | | - |
39 | | - def _call_with_certauth(self, method, api_call_part1, api_call_part2=None, api_call_part3=None, identifier1=None, identifier2=None, params=None, data=None): |
40 | | - if self.CERTTOKEN is '': |
41 | | - raise CloudFlareAPIError(0, 'no email and/or cert token defined') |
42 | | - headers = { "X-Auth-User-Service-Key": self.CERTTOKEN, 'Content-Type': 'application/json' } |
43 | | - return self._call(method, headers, api_call_part1, api_call_part2, api_call_part3, identifier1, identifier2, params, data) |
44 | | - |
45 | | - def _call(self, method, headers, api_call_part1, api_call_part2=None, api_call_part3=None, identifier1=None, identifier2=None, params=None, data=None): |
46 | | - if api_call_part2 is not None or (data is not None and method == 'GET'): |
47 | | - if identifier2 is None: |
48 | | - url = self.BASE_URL + '/' + api_call_part1 + '/' + identifier1 + '/' + api_call_part2 |
49 | | - else: |
50 | | - url = self.BASE_URL + '/' + api_call_part1 + '/' + identifier1 + '/' + api_call_part2 + '/' + identifier2 |
51 | | - else: |
52 | | - if identifier1 is None: |
53 | | - url = self.BASE_URL + '/' + api_call_part1 |
54 | | - else: |
55 | | - url = self.BASE_URL + '/' + api_call_part1 + '/' + identifier1 |
56 | | - if api_call_part3: |
57 | | - url += '/' + api_call_part3 |
58 | | - |
59 | | - if self.logger: |
60 | | - self.logger.debug("Call: %s,%s,%s,%s,%s" % (str(api_call_part1), str(identifier1), str(api_call_part2), str(identifier2), str(api_call_part3))) |
61 | | - self.logger.debug("Call: optional params and data: %s %s" % (str(params), str(data))) |
62 | | - self.logger.debug("Call: url is: %s" % (str(url))) |
63 | | - self.logger.debug("Call: method is: %s" % (str(method))) |
64 | | - self.logger.debug("Call: headers %s" % str(utils.sanitize_secrets(headers))) |
65 | | - |
66 | | - if (method is None) or (api_call_part1 is None): |
67 | | - raise CloudFlareInternalError('You must specify a method and endpoint') # should never happen |
68 | | - |
69 | | - method = method.upper() |
70 | | - |
71 | | - if method == 'GET': |
72 | | - response = requests.get(url, headers=headers, params=params, data=data) |
73 | | - elif method == 'POST': |
74 | | - response = requests.post(url, headers=headers, params=params, json=data) |
75 | | - elif method == 'PUT': |
76 | | - response = requests.put(url, headers=headers, params=params, json=data) |
77 | | - elif method == 'DELETE': |
78 | | - if data: |
79 | | - response = requests.delete(url, headers=headers, json=data) |
80 | | - else: |
81 | | - response = requests.delete(url, headers=headers) |
82 | | - elif method == 'PATCH': |
83 | | - if data: |
84 | | - response = requests.request('PATCH', url, headers=headers, params=params, json=data) |
85 | | - else: |
86 | | - response = requests.request('PATCH', url, headers=headers, params=params) |
87 | | - else: |
88 | | - raise CloudFlareAPIError(0, 'method not supported') # should never happen |
89 | | - |
90 | | - if self.logger: |
91 | | - self.logger.debug("Response url: %s", response.url) |
92 | | - |
93 | | - response_data = response.text |
94 | | - if self.logger: |
95 | | - self.logger.debug("Response_data: %s" % response_data) |
96 | | - try: |
97 | | - response_data = json.loads(response_data) |
98 | | - except ValueError: |
99 | | - raise CloudFlareAPIError(0, 'JSON parse failed.') |
100 | | - |
101 | | - if response_data['success'] is False: |
102 | | - if self.logger: |
103 | | - self.logger.debug("response_data error: %d %s" % (response_data['errors'][0]['code'], response_data['errors'][0]['message'])) |
104 | | - raise CloudFlareAPIError(response_data['errors'][0]['code'], response_data['errors'][0]['message']) |
105 | | - |
106 | | - return response_data['result'] |
107 | | - |
108 | | - class _unused: |
109 | | - def __init__(self, base, api_call_part1, api_call_part2=None, api_call_part3=None): |
110 | | - #if self.logger: |
111 | | - # self.logger.debug("_unused %s,%s,%s" % (str(base), str(api_call_part1), str(api_call_part2))) |
112 | | - self.base = base |
113 | | - self.api_call_part1 = api_call_part1 |
114 | | - self.api_call_part2 = api_call_part2 |
115 | | - self.api_call_part3 = api_call_part3 |
116 | | - |
117 | | - class _client_noauth: |
118 | | - def __init__(self, base, api_call_part1, api_call_part2=None, api_call_part3=None): |
119 | | - #if self.logger: |
120 | | - # self.logger.debug("_client_noauth %s,%s,%s" % (str(base), str(api_call_part1), str(api_call_part2))) |
121 | | - self.base = base |
122 | | - self.api_call_part1 = api_call_part1 |
123 | | - self.api_call_part2 = api_call_part2 |
124 | | - self.api_call_part3 = api_call_part3 |
125 | | - |
126 | | - def get(self, identifier1=None, identifier2=None, params=None, data=None): |
127 | | - return self.base._call_with_no_auth('GET', self.api_call_part1, self.api_call_part2, self.api_call_part3, identifier1, identifier2, params, data) |
128 | | - |
129 | | - class _client_with_auth: |
130 | | - def __init__(self, base, api_call_part1, api_call_part2=None, api_call_part3=None): |
131 | | - #if self.logger: |
132 | | - # self.logger.debug("_client_with_auth %s,%s,%s" % (str(base), str(api_call_part1), str(api_call_part2))) |
133 | | - self.base = base |
134 | | - self.api_call_part1 = api_call_part1 |
135 | | - self.api_call_part2 = api_call_part2 |
136 | | - self.api_call_part3 = api_call_part3 |
137 | | - |
138 | | - def get(self, identifier1=None, identifier2=None, params=None, data=None): |
139 | | - return self.base._call_with_auth('GET', self.api_call_part1, self.api_call_part2, self.api_call_part3, identifier1, identifier2, params, data) |
140 | | - |
141 | | - def patch(self, identifier1=None, identifier2=None, params=None, data=None): |
142 | | - return self.base._call_with_auth('PATCH', self.api_call_part1, self.api_call_part2, self.api_call_part3, identifier1, identifier2, params, data) |
143 | | - |
144 | | - def post(self, identifier1=None, identifier2=None, params=None, data=None): |
145 | | - return self.base._call_with_auth('POST', self.api_call_part1, self.api_call_part2, self.api_call_part3, identifier1, identifier2, params, data) |
146 | | - |
147 | | - def put(self, identifier1=None, identifier2=None, params=None, data=None): |
148 | | - return self.base._call_with_auth('PUT', self.api_call_part1, self.api_call_part2, self.api_call_part3, identifier1, identifier2, params, data) |
149 | | - |
150 | | - def delete(self, identifier1=None, identifier2=None, params=None, data=None): |
151 | | - return self.base._call_with_auth('DELETE', self.api_call_part1, self.api_call_part2, self.api_call_part3, identifier1, identifier2, params, data) |
152 | | - |
153 | | - class _client_with_cert_auth: |
154 | | - def __init__(self, base, api_call_part1, api_call_part2=None, api_call_part3=None): |
155 | | - #if self.logger: |
156 | | - # self.logger.debug("_client_with_cert_auth %s,%s,%s" % (str(base), str(api_call_part1), str(api_call_part2))) |
157 | | - self.base = base |
158 | | - self.api_call_part1 = api_call_part1 |
159 | | - self.api_call_part2 = api_call_part2 |
160 | | - self.api_call_part3 = api_call_part3 |
161 | | - |
162 | | - def get(self, identifier1=None, identifier2=None, params=None, data=None): |
163 | | - return self.base._call_with_certauth('GET', self.api_call_part1, self.api_call_part2, self.api_call_part3, identifier1, identifier2, params, data) |
164 | | - |
165 | | - def patch(self, identifier1=None, identifier2=None, params=None, data=None): |
166 | | - return self.base._call_with_certauth('PATCH', self.api_call_part1, self.api_call_part2, self.api_call_part3, identifier1, identifier2, params, data) |
167 | | - |
168 | | - def post(self, identifier1=None, identifier2=None, params=None, data=None): |
169 | | - return self.base._call_with_certauth('POST', self.api_call_part1, self.api_call_part2, self.api_call_part3, identifier1, identifier2, params, data) |
170 | | - |
171 | | - def put(self, identifier1=None, identifier2=None, params=None, data=None): |
172 | | - return self.base._call_with_certauth('PUT', self.api_call_part1, self.api_call_part2, self.api_call_part3, identifier1, identifier2, params, data) |
173 | | - |
174 | | - def delete(self, identifier1=None, identifier2=None, params=None, data=None): |
175 | | - return self.base._call_with_certauth('DELETE', self.api_call_part1, self.api_call_part2, self.api_call_part3, identifier1, identifier2, params, data) |
176 | | - |
177 | | - def __init__(self, email=None, token=None, certtoken=None, debug=False): |
178 | | - base_url = BASE_URL |
179 | | - |
180 | | - # class creation values override configuration values |
181 | | - [ conf_email, conf_token, conf_certtoken, extras ] = read_configs() |
182 | | - |
183 | | - if email is None: |
184 | | - email = conf_email |
185 | | - if token is None: |
186 | | - token = conf_token |
187 | | - if certtoken is None: |
188 | | - certtoken = conf_certtoken |
189 | | - |
190 | | - # Removed: There are cases where you don't need an email and token |
191 | | - #if email is None or token is None: |
192 | | - # raise CloudFlareInternalError('You must at least specify an email and token string') |
193 | | - |
194 | | - self.base = self._base(email, token, certtoken, base_url, debug) |
195 | | - |
196 | | - # add the API calls |
197 | | - api_v4(self) |
198 | | - if extras: |
199 | | - api_extras(self, extras) |
| 17 | + class _base: |
| 18 | + def __init__(self, email, token, certtoken, base_url, debug): |
| 19 | + self.EMAIL = email |
| 20 | + self.TOKEN = token |
| 21 | + self.CERTTOKEN = certtoken |
| 22 | + self.BASE_URL = base_url |
| 23 | + |
| 24 | + if debug: |
| 25 | + self.logger = logger.Logger(debug).getLogger() |
| 26 | + else: |
| 27 | + self.logger = None |
| 28 | + |
| 29 | + def _call_with_no_auth(self, method, api_call_part1, api_call_part2=None, api_call_part3=None, identifier1=None, identifier2=None, params=None, data=None): |
| 30 | + headers = {} |
| 31 | + return self._call(method, headers, api_call_part1, api_call_part2, api_call_part3, identifier1, identifier2, params, data) |
| 32 | + |
| 33 | + def _call_with_auth(self, method, api_call_part1, api_call_part2=None, api_call_part3=None, identifier1=None, identifier2=None, params=None, data=None): |
| 34 | + if self.EMAIL is '' or self.TOKEN is '': |
| 35 | + raise CloudFlareAPIError(0, 'no email and/or token defined') |
| 36 | + headers = { "X-Auth-Email": self.EMAIL, "X-Auth-Key": self.TOKEN, 'Content-Type': 'application/json' } |
| 37 | + return self._call(method, headers, api_call_part1, api_call_part2, api_call_part3, identifier1, identifier2, params, data) |
| 38 | + |
| 39 | + def _call_with_certauth(self, method, api_call_part1, api_call_part2=None, api_call_part3=None, identifier1=None, identifier2=None, params=None, data=None): |
| 40 | + if self.CERTTOKEN is '': |
| 41 | + raise CloudFlareAPIError(0, 'no email and/or cert token defined') |
| 42 | + headers = { "X-Auth-User-Service-Key": self.CERTTOKEN, 'Content-Type': 'application/json' } |
| 43 | + return self._call(method, headers, api_call_part1, api_call_part2, api_call_part3, identifier1, identifier2, params, data) |
| 44 | + |
| 45 | + def _call(self, method, headers, api_call_part1, api_call_part2=None, api_call_part3=None, identifier1=None, identifier2=None, params=None, data=None): |
| 46 | + if api_call_part2 is not None or (data is not None and method == 'GET'): |
| 47 | + if identifier2 is None: |
| 48 | + url = self.BASE_URL + '/' + api_call_part1 + '/' + identifier1 + '/' + api_call_part2 |
| 49 | + else: |
| 50 | + url = self.BASE_URL + '/' + api_call_part1 + '/' + identifier1 + '/' + api_call_part2 + '/' + identifier2 |
| 51 | + else: |
| 52 | + if identifier1 is None: |
| 53 | + url = self.BASE_URL + '/' + api_call_part1 |
| 54 | + else: |
| 55 | + url = self.BASE_URL + '/' + api_call_part1 + '/' + identifier1 |
| 56 | + if api_call_part3: |
| 57 | + url += '/' + api_call_part3 |
| 58 | + |
| 59 | + if self.logger: |
| 60 | + self.logger.debug("Call: %s,%s,%s,%s,%s" % (str(api_call_part1), str(identifier1), str(api_call_part2), str(identifier2), str(api_call_part3))) |
| 61 | + self.logger.debug("Call: optional params and data: %s %s" % (str(params), str(data))) |
| 62 | + self.logger.debug("Call: url is: %s" % (str(url))) |
| 63 | + self.logger.debug("Call: method is: %s" % (str(method))) |
| 64 | + self.logger.debug("Call: headers %s" % str(utils.sanitize_secrets(headers))) |
| 65 | + |
| 66 | + if (method is None) or (api_call_part1 is None): |
| 67 | + raise CloudFlareInternalError('You must specify a method and endpoint') # should never happen |
| 68 | + |
| 69 | + method = method.upper() |
| 70 | + |
| 71 | + if method == 'GET': |
| 72 | + response = requests.get(url, headers=headers, params=params, data=data) |
| 73 | + elif method == 'POST': |
| 74 | + response = requests.post(url, headers=headers, params=params, json=data) |
| 75 | + elif method == 'PUT': |
| 76 | + response = requests.put(url, headers=headers, params=params, json=data) |
| 77 | + elif method == 'DELETE': |
| 78 | + if data: |
| 79 | + response = requests.delete(url, headers=headers, json=data) |
| 80 | + else: |
| 81 | + response = requests.delete(url, headers=headers) |
| 82 | + elif method == 'PATCH': |
| 83 | + if data: |
| 84 | + response = requests.request('PATCH', url, headers=headers, params=params, json=data) |
| 85 | + else: |
| 86 | + response = requests.request('PATCH', url, headers=headers, params=params) |
| 87 | + else: |
| 88 | + raise CloudFlareAPIError(0, 'method not supported') # should never happen |
| 89 | + |
| 90 | + if self.logger: |
| 91 | + self.logger.debug("Response url: %s", response.url) |
| 92 | + |
| 93 | + response_data = response.text |
| 94 | + if self.logger: |
| 95 | + self.logger.debug("Response_data: %s" % response_data) |
| 96 | + try: |
| 97 | + response_data = json.loads(response_data) |
| 98 | + except ValueError: |
| 99 | + raise CloudFlareAPIError(0, 'JSON parse failed.') |
| 100 | + |
| 101 | + if response_data['success'] is False: |
| 102 | + if self.logger: |
| 103 | + self.logger.debug("response_data error: %d %s" % (response_data['errors'][0]['code'], response_data['errors'][0]['message'])) |
| 104 | + raise CloudFlareAPIError(response_data['errors'][0]['code'], response_data['errors'][0]['message']) |
| 105 | + |
| 106 | + return response_data['result'] |
| 107 | + |
| 108 | + class _unused: |
| 109 | + def __init__(self, base, api_call_part1, api_call_part2=None, api_call_part3=None): |
| 110 | + #if self.logger: |
| 111 | + # self.logger.debug("_unused %s,%s,%s" % (str(base), str(api_call_part1), str(api_call_part2))) |
| 112 | + self.base = base |
| 113 | + self.api_call_part1 = api_call_part1 |
| 114 | + self.api_call_part2 = api_call_part2 |
| 115 | + self.api_call_part3 = api_call_part3 |
| 116 | + |
| 117 | + class _client_noauth: |
| 118 | + def __init__(self, base, api_call_part1, api_call_part2=None, api_call_part3=None): |
| 119 | + #if self.logger: |
| 120 | + # self.logger.debug("_client_noauth %s,%s,%s" % (str(base), str(api_call_part1), str(api_call_part2))) |
| 121 | + self.base = base |
| 122 | + self.api_call_part1 = api_call_part1 |
| 123 | + self.api_call_part2 = api_call_part2 |
| 124 | + self.api_call_part3 = api_call_part3 |
| 125 | + |
| 126 | + def get(self, identifier1=None, identifier2=None, params=None, data=None): |
| 127 | + return self.base._call_with_no_auth('GET', self.api_call_part1, self.api_call_part2, self.api_call_part3, identifier1, identifier2, params, data) |
| 128 | + |
| 129 | + class _client_with_auth: |
| 130 | + def __init__(self, base, api_call_part1, api_call_part2=None, api_call_part3=None): |
| 131 | + #if self.logger: |
| 132 | + # self.logger.debug("_client_with_auth %s,%s,%s" % (str(base), str(api_call_part1), str(api_call_part2))) |
| 133 | + self.base = base |
| 134 | + self.api_call_part1 = api_call_part1 |
| 135 | + self.api_call_part2 = api_call_part2 |
| 136 | + self.api_call_part3 = api_call_part3 |
| 137 | + |
| 138 | + def get(self, identifier1=None, identifier2=None, params=None, data=None): |
| 139 | + return self.base._call_with_auth('GET', self.api_call_part1, self.api_call_part2, self.api_call_part3, identifier1, identifier2, params, data) |
| 140 | + |
| 141 | + def patch(self, identifier1=None, identifier2=None, params=None, data=None): |
| 142 | + return self.base._call_with_auth('PATCH', self.api_call_part1, self.api_call_part2, self.api_call_part3, identifier1, identifier2, params, data) |
| 143 | + |
| 144 | + def post(self, identifier1=None, identifier2=None, params=None, data=None): |
| 145 | + return self.base._call_with_auth('POST', self.api_call_part1, self.api_call_part2, self.api_call_part3, identifier1, identifier2, params, data) |
| 146 | + |
| 147 | + def put(self, identifier1=None, identifier2=None, params=None, data=None): |
| 148 | + return self.base._call_with_auth('PUT', self.api_call_part1, self.api_call_part2, self.api_call_part3, identifier1, identifier2, params, data) |
| 149 | + |
| 150 | + def delete(self, identifier1=None, identifier2=None, params=None, data=None): |
| 151 | + return self.base._call_with_auth('DELETE', self.api_call_part1, self.api_call_part2, self.api_call_part3, identifier1, identifier2, params, data) |
| 152 | + |
| 153 | + class _client_with_cert_auth: |
| 154 | + def __init__(self, base, api_call_part1, api_call_part2=None, api_call_part3=None): |
| 155 | + #if self.logger: |
| 156 | + # self.logger.debug("_client_with_cert_auth %s,%s,%s" % (str(base), str(api_call_part1), str(api_call_part2))) |
| 157 | + self.base = base |
| 158 | + self.api_call_part1 = api_call_part1 |
| 159 | + self.api_call_part2 = api_call_part2 |
| 160 | + self.api_call_part3 = api_call_part3 |
| 161 | + |
| 162 | + def get(self, identifier1=None, identifier2=None, params=None, data=None): |
| 163 | + return self.base._call_with_certauth('GET', self.api_call_part1, self.api_call_part2, self.api_call_part3, identifier1, identifier2, params, data) |
| 164 | + |
| 165 | + def patch(self, identifier1=None, identifier2=None, params=None, data=None): |
| 166 | + return self.base._call_with_certauth('PATCH', self.api_call_part1, self.api_call_part2, self.api_call_part3, identifier1, identifier2, params, data) |
| 167 | + |
| 168 | + def post(self, identifier1=None, identifier2=None, params=None, data=None): |
| 169 | + return self.base._call_with_certauth('POST', self.api_call_part1, self.api_call_part2, self.api_call_part3, identifier1, identifier2, params, data) |
| 170 | + |
| 171 | + def put(self, identifier1=None, identifier2=None, params=None, data=None): |
| 172 | + return self.base._call_with_certauth('PUT', self.api_call_part1, self.api_call_part2, self.api_call_part3, identifier1, identifier2, params, data) |
| 173 | + |
| 174 | + def delete(self, identifier1=None, identifier2=None, params=None, data=None): |
| 175 | + return self.base._call_with_certauth('DELETE', self.api_call_part1, self.api_call_part2, self.api_call_part3, identifier1, identifier2, params, data) |
| 176 | + |
| 177 | + def __init__(self, email=None, token=None, certtoken=None, debug=False): |
| 178 | + base_url = BASE_URL |
| 179 | + |
| 180 | + # class creation values override configuration values |
| 181 | + [ conf_email, conf_token, conf_certtoken, extras ] = read_configs() |
| 182 | + |
| 183 | + if email is None: |
| 184 | + email = conf_email |
| 185 | + if token is None: |
| 186 | + token = conf_token |
| 187 | + if certtoken is None: |
| 188 | + certtoken = conf_certtoken |
| 189 | + |
| 190 | + # Removed: There are cases where you don't need an email and token |
| 191 | + # if email is None or token is None: |
| 192 | + # raise CloudFlareInternalError('You must at least specify an email and token string') |
| 193 | + |
| 194 | + self.base = self._base(email, token, certtoken, base_url, debug) |
| 195 | + |
| 196 | + # add the API calls |
| 197 | + api_v4(self) |
| 198 | + if extras: |
| 199 | + api_extras(self, extras) |
200 | 200 |
|
0 commit comments