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

Commit 7a0d715

Browse files
committed
sanitize passed variables, handle errors from network better, first pass at keywork conflict issues
1 parent c6b7423 commit 7a0d715

1 file changed

Lines changed: 26 additions & 1 deletion

File tree

CloudFlare/cloudflare.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import json
55
import requests
6+
import keyword
67

78
from .network import CFnetwork
89
from .logging_helper import CFlogger
@@ -48,6 +49,15 @@ def __init__(self, config):
4849
self.network = CFnetwork(use_sessions=self.use_sessions)
4950
self.user_agent = user_agent()
5051

52+
if not isinstance(self.email, str):
53+
raise ValueError('email argument not string')
54+
if not isinstance(self.token, str):
55+
raise ValueError('token argument not string')
56+
if not isinstance(self.certtoken, str):
57+
raise ValueError('certtoken argument not string')
58+
if not isinstance(self.base_url, str):
59+
raise ValueError('base url argument not string')
60+
5161
if 'debug' in config and config['debug']:
5262
self.logger = CFlogger(config['debug']).getLogger()
5363
else:
@@ -201,10 +211,22 @@ def _call_network(self, method, headers, parts,
201211

202212
try:
203213
response = self.network(method, url, headers, params, data, files)
214+
except requests.RequestException as e:
215+
if self.logger:
216+
self.logger.debug('Call: requests exception! "%s"' % (e))
217+
raise CloudFlareAPIError(0, e)
218+
except requests.ConnectionError as e:
219+
if self.logger:
220+
self.logger.debug('Call: requests connection exception! "%s"' % (e))
221+
raise CloudFlareAPIError(0, 'connection error')
222+
except requests.exceptions.Timeout as e:
223+
if self.logger:
224+
self.logger.debug('Call: requests timeout exception! "%s"' % (e))
225+
raise CloudFlareAPIError(0, 'connection timeout')
204226
except Exception as e:
205227
if self.logger:
206228
self.logger.debug('Call: exception! "%s"' % (e))
207-
raise CloudFlareAPIError(0, 'connection failed.')
229+
raise
208230

209231
# Create response_{type|code|data}
210232
try:
@@ -859,6 +881,9 @@ def add(self, t, p1, p2=None, p3=None):
859881
# should never happen
860882
raise CloudFlareAPIError(0, 'api load type mismatch')
861883

884+
if keyword.iskeyword(name):
885+
## add an extra keywork prefix'ed with underscore so it can used with Python code
886+
setattr(branch, name + '_', f)
862887
if '-' in name:
863888
# dashes (vs underscores) cause issues in Python and other languages
864889
setattr(branch, name.replace('-','_'), f)

0 commit comments

Comments
 (0)