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

Commit 0004a42

Browse files
committed
Typos and cleanup
1 parent cedda2b commit 0004a42

1 file changed

Lines changed: 50 additions & 40 deletions

File tree

CloudFlare/cloudflare.py

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def __init__(self, email, token, certtoken, base_url, debug):
2828
self.base_url = base_url
2929

3030
if debug:
31-
self.logger = logger.Logger(debug).getLogger()
31+
self.logger = Logger(debug).getLogger()
3232
else:
3333
self.logger = None
3434

@@ -59,8 +59,8 @@ def call_with_auth(self, method,
5959
if self.email is '' or self.token is '':
6060
raise CloudFlareAPIError(0, 'no email and/or token defined')
6161
headers = {
62-
"X-Auth-Email": self.email,
63-
"X-Auth-Key": self.token,
62+
'X-Auth-Email': self.email,
63+
'X-Auth-Key': self.token,
6464
'Content-Type': 'application/json'
6565
}
6666
return self._call(method, headers,
@@ -79,7 +79,7 @@ def call_with_certauth(self, method,
7979
if self.certtoken is '':
8080
raise CloudFlareAPIError(0, 'no email and/or cert token defined')
8181
headers = {
82-
"X-Auth-User-Service-Key": self.certtoken,
82+
'X-Auth-User-Service-Key': self.certtoken,
8383
'Content-Type': 'application/json'
8484
}
8585
return self._call(method, headers,
@@ -97,65 +97,75 @@ def _call(self, method, headers,
9797

9898
if api_call_part2 is not None or (data is not None and method == 'GET'):
9999
if identifier2 is None:
100-
url = self.base_url + '/' + \
101-
api_call_part1 + '/' + \
102-
identifier1 + '/' + \
103-
api_call_part2
100+
url = (self.base_url + '/'
101+
+ api_call_part1 + '/'
102+
+ identifier1 + '/'
103+
+ api_call_part2)
104104
else:
105-
url = self.base_url + '/' + \
106-
api_call_part1 + '/' + \
107-
identifier1 + '/' + \
108-
api_call_part2 + '/' + \
109-
identifier2
105+
url = (self.base_url + '/'
106+
+ api_call_part1 + '/'
107+
+ identifier1 + '/'
108+
+ api_call_part2 + '/'
109+
+ identifier2)
110110
else:
111111
if identifier1 is None:
112-
url = self.base_url + '/' + \
113-
api_call_part1
112+
url = (self.base_url + '/'
113+
+ api_call_part1)
114114
else:
115-
url = self.base_url + '/' + \
116-
api_call_part1 + '/' + \
117-
identifier1
115+
url = (self.base_url + '/'
116+
+ api_call_part1 + '/'
117+
+ identifier1)
118118
if api_call_part3:
119119
url += '/' + api_call_part3
120120

121121
if self.logger:
122-
self.logger.debug("Call: %s,%s,%s,%s,%s" % (str(api_call_part1),
122+
self.logger.debug('Call: %s,%s,%s,%s,%s' % (str(api_call_part1),
123123
str(identifier1),
124124
str(api_call_part2),
125125
str(identifier2),
126126
str(api_call_part3)))
127-
self.logger.debug("Call: optional params and data %s %s" % (str(params),
127+
self.logger.debug('Call: optional params and data %s %s' % (str(params),
128128
str(data)))
129-
self.logger.debug("Call: method and url %s %s" % (str(method, url)))
130-
self.logger.debug("Call: headers %s" % str(utils.sanitize_secrets(headers)))
129+
self.logger.debug('Call: method and url %s %s' % (str(method), str(url)))
130+
self.logger.debug('Call: headers %s' % str(sanitize_secrets(headers)))
131131

132132
if (method is None) or (api_call_part1 is None):
133133
# should never happen
134134
raise CloudFlareInternalError(0, 'You must specify a method and endpoint')
135135

136136
method = method.upper()
137137

138-
if method == 'GET':
139-
response = requests.get(url, headers=headers, params=params, data=data)
140-
elif method == 'POST':
141-
response = requests.post(url, headers=headers, params=params, json=data)
142-
elif method == 'PUT':
143-
response = requests.put(url, headers=headers, params=params, json=data)
144-
elif method == 'DELETE':
145-
response = requests.delete(url, headers=headers, json=data)
146-
elif method == 'PATCH':
147-
response = requests.request('PATCH', url,
148-
headers=headers, params=params, json=data)
149-
else:
150-
# should never happen
151-
raise CloudFlareAPIError(0, 'method not supported')
138+
if self.logger:
139+
self.logger.debug('Call: doit!')
140+
141+
try:
142+
if method == 'GET':
143+
response = requests.get(url, headers=headers, params=params, data=data)
144+
elif method == 'POST':
145+
response = requests.post(url, headers=headers, params=params, json=data)
146+
elif method == 'PUT':
147+
response = requests.put(url, headers=headers, params=params, json=data)
148+
elif method == 'DELETE':
149+
response = requests.delete(url, headers=headers, json=data)
150+
elif method == 'PATCH':
151+
response = requests.request('PATCH', url,
152+
headers=headers, params=params, json=data)
153+
else:
154+
# should never happen
155+
raise CloudFlareAPIError(0, 'method not supported')
156+
if self.logger:
157+
self.logger.debug('Call: done!')
158+
except Exception as e:
159+
if self.logger:
160+
self.logger.debug('Call: exception!')
161+
raise CloudFlareAPIError(0, 'connection failed.')
152162

153163
if self.logger:
154-
self.logger.debug("Response: url %s", response.url)
164+
self.logger.debug('Response: url %s', response.url)
155165

156166
response_data = response.text
157167
if self.logger:
158-
self.logger.debug("Response: data %s" % response_data)
168+
self.logger.debug('Response: data %s' % response_data)
159169
try:
160170
response_data = json.loads(response_data)
161171
except ValueError:
@@ -165,12 +175,12 @@ def _call(self, method, headers,
165175
code = response_data['errors'][0]['code']
166176
message = response_data['errors'][0]['message']
167177
if self.logger:
168-
self.logger.debug("Response: error %d %s" % (code, message))
178+
self.logger.debug('Response: error %d %s' % (code, message))
169179
raise CloudFlareAPIError(code, message)
170180

171181
result = response_data['result']
172182
if self.logger:
173-
self.logger.debug("Response: %s" % (result))
183+
self.logger.debug('Response: %s' % (result))
174184
return result
175185

176186
class add_unused(object):

0 commit comments

Comments
 (0)