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

Commit 589c0de

Browse files
committed
Moved core logic into its own file - CloudFlare/cloudflare.py
1 parent 78fff44 commit 589c0de

2 files changed

Lines changed: 265 additions & 260 deletions

File tree

CloudFlare/__init__.py

Lines changed: 2 additions & 260 deletions
Original file line numberDiff line numberDiff line change
@@ -1,264 +1,6 @@
1-
""" CloudFlare v4 API"""
2-
3-
import json
4-
import urllib
5-
import requests
61

7-
from logger import Logger
8-
from utils import sanitize_secrets
9-
from read_configs import read_configs
10-
from api_v4 import api_v4
11-
from api_extras import api_extras
12-
from exceptions import CloudFlareError, CloudFlareAPIError, CloudFlareInternalError
2+
""" CloudFlare v4 API"""
133

144
__version__ = '1.0.7'
155

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

0 commit comments

Comments
 (0)