forked from cloudflare/python-cloudflare
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
644 additions
and
456 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,54 @@ | ||
""" API extras for CloudFlare API""" | ||
|
||
import re | ||
|
||
def api_extras(self, extras=None): | ||
for extra in extras: | ||
if extra == '': | ||
continue | ||
extra = re.sub(r"^.*/client/v4/", '/', extra) | ||
extra = re.sub(r"^.*/v4/", '/', extra) | ||
extra = re.sub(r"^/", '', extra) | ||
""" API extras for CloudFlare API""" | ||
|
||
# build parts of the extra command | ||
parts = [] | ||
nn = 0 | ||
for element in extra.split('/'): | ||
if element[0] == ':': | ||
nn += 1 | ||
continue | ||
try: | ||
parts[nn] | ||
except: | ||
parts.append([]) | ||
parts[nn].append(element) | ||
for extra in extras: | ||
if extra == '': | ||
continue | ||
extra = re.sub(r"^.*/client/v4/", '/', extra) | ||
extra = re.sub(r"^.*/v4/", '/', extra) | ||
extra = re.sub(r"^/", '', extra) | ||
|
||
# insert extra command into class | ||
element_path = [] | ||
current = self | ||
for element in parts[0]: | ||
element_path.append(element) | ||
try: | ||
m = getattr(current, element) | ||
# exists - but still add it there's a second part | ||
if element == parts[0][-1] and len(parts) > 1: | ||
setattr(m, parts[1][0], self._client_with_auth(self.base, '/'.join(element_path), '/'.join(parts[1]))) | ||
current = m | ||
continue | ||
except: | ||
pass | ||
# does not exist | ||
if element == parts[0][-1] and len(parts) > 1: | ||
# last element | ||
setattr(current, element, self._client_with_auth(self.base, '/'.join(element_path), '/'.join(parts[1]))) | ||
else: | ||
setattr(current, element, self._client_with_auth(self.base, '/'.join(element_path))) | ||
current = getattr(current, element) | ||
# build parts of the extra command | ||
parts = [] | ||
nn = 0 | ||
for element in extra.split('/'): | ||
if element[0] == ':': | ||
nn += 1 | ||
continue | ||
try: | ||
parts[nn] | ||
except IndexError: | ||
parts.append([]) | ||
parts[nn].append(element) | ||
|
||
# insert extra command into class | ||
element_path = [] | ||
current = self | ||
for element in parts[0]: | ||
element_path.append(element) | ||
try: | ||
m = getattr(current, element) | ||
# exists - but still add it there's a second part | ||
if element == parts[0][-1] and len(parts) > 1: | ||
api_call_part1 = '/'.join(element_path) | ||
api_call_part2 = '/'.join(parts[1]) | ||
setattr(m, parts[1][0], self._client_with_auth(self.base, api_call_part1, api_call_part2)) | ||
current = m | ||
continue | ||
except: | ||
pass | ||
# does not exist | ||
if element == parts[0][-1] and len(parts) > 1: | ||
# last element | ||
api_call_part1 = '/'.join(element_path) | ||
api_call_part2 = '/'.join(parts[1]) | ||
setattr(current, element, self._client_with_auth(self.base, api_call_part1, api_call_part2)) | ||
else: | ||
api_call_part1 = '/'.join(element_path) | ||
setattr(current, element, self._client_with_auth(self.base, api_call_part1)) | ||
current = getattr(current, element) | ||
|
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,31 @@ | ||
""" errors for CloudFlare API""" | ||
|
||
class CloudFlareError(Exception): | ||
def __init__(self, code, message): | ||
self.code = code | ||
self.message = message | ||
""" errors for CloudFlare API""" | ||
|
||
def __int__(self): | ||
return self.code | ||
def __init__(self, code, message): | ||
""" errors for CloudFlare API""" | ||
|
||
def __str__(self): | ||
return self.message | ||
self.code = code | ||
self.message = message | ||
|
||
def __int__(self): | ||
""" errors for CloudFlare API""" | ||
|
||
return self.code | ||
|
||
def __str__(self): | ||
""" errors for CloudFlare API""" | ||
|
||
return self.message | ||
|
||
class CloudFlareAPIError(CloudFlareError): | ||
pass | ||
""" errors for CloudFlare API""" | ||
|
||
pass | ||
|
||
class CloudFlareInternalError(CloudFlareError): | ||
pass | ||
""" errors for CloudFlare API""" | ||
|
||
pass | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,43 @@ | ||
|
||
""" Logging for CloudFlare API""" | ||
import logging | ||
|
||
DEBUG = 0 | ||
INFO = 1 | ||
|
||
class Logger: | ||
def __init__(self, level): | ||
self.logger_level = self._get_logging_level(level) | ||
#logging.basicConfig(level=self.logger_level) | ||
request_logger = logging.getLogger("requests.packages.urllib3") | ||
request_logger.setLevel(self.logger_level) | ||
request_logger.propagate = level | ||
class Logger(object): | ||
""" Logging for CloudFlare API""" | ||
|
||
def __init__(self, level): | ||
""" Logging for CloudFlare API""" | ||
self.logger_level = self._get_logging_level(level) | ||
#logging.basicConfig(level=self.logger_level) | ||
request_logger = logging.getLogger("requests.packages.urllib3") | ||
request_logger.setLevel(self.logger_level) | ||
request_logger.propagate = level | ||
|
||
def getLogger(self): | ||
# create logger | ||
logger = logging.getLogger('Python CloudFlare API v4') | ||
logger.setLevel(self.logger_level) | ||
def getLogger(self): | ||
""" Logging for CloudFlare API""" | ||
# create logger | ||
logger = logging.getLogger('Python CloudFlare API v4') | ||
logger.setLevel(self.logger_level) | ||
|
||
ch = logging.StreamHandler() | ||
ch.setLevel(self.logger_level) | ||
ch = logging.StreamHandler() | ||
ch.setLevel(self.logger_level) | ||
|
||
# create formatter | ||
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') | ||
# create formatter | ||
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') | ||
|
||
# add formatter to ch | ||
ch.setFormatter(formatter) | ||
# add formatter to ch | ||
ch.setFormatter(formatter) | ||
|
||
# add ch to logger | ||
logger.addHandler(ch) | ||
# add ch to logger | ||
logger.addHandler(ch) | ||
|
||
return logger | ||
return logger | ||
|
||
def _get_logging_level(self, level): | ||
if level == True: | ||
return logging.DEBUG | ||
else: | ||
return logging.INFO | ||
def _get_logging_level(self, level): | ||
""" Logging for CloudFlare API""" | ||
if level is True: | ||
return logging.DEBUG | ||
else: | ||
return logging.INFO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,50 @@ | ||
""" reading the config file for CloudFlare API""" | ||
|
||
import os | ||
import re | ||
import ConfigParser | ||
|
||
def read_configs(): | ||
|
||
# envioronment variables override config files | ||
email = os.getenv('CF_API_EMAIL') | ||
token = os.getenv('CF_API_KEY') | ||
certtoken = os.getenv('CF_API_CERTKEY') | ||
|
||
# grab values from config files | ||
config = ConfigParser.RawConfigParser() | ||
config.read(['.cloudflare.cfg', os.path.expanduser('~/.cloudflare.cfg'), os.path.expanduser('~/.cloudflare/cloudflare.cfg')]) | ||
|
||
if email is None: | ||
try: | ||
email = re.sub(r"\s+", '', config.get('CloudFlare', 'email')) | ||
except: | ||
email = None | ||
|
||
if token is None: | ||
try: | ||
token = re.sub(r"\s+", '', config.get('CloudFlare', 'token')) | ||
except: | ||
token = None | ||
|
||
if certtoken is None: | ||
try: | ||
certtoken = re.sub(r"\s+", '', config.get('CloudFlare', 'certtoken')) | ||
except: | ||
certtoken = None | ||
|
||
try: | ||
extras = re.sub(r"\s+", ' ', config.get('CloudFlare', 'extras')) | ||
except: | ||
extras = None | ||
|
||
if extras: | ||
extras = extras.split(' ') | ||
|
||
return [ email, token, certtoken, extras ] | ||
""" reading the config file for CloudFlare API""" | ||
|
||
# envioronment variables override config files | ||
email = os.getenv('CF_API_EMAIL') | ||
token = os.getenv('CF_API_KEY') | ||
certtoken = os.getenv('CF_API_CERTKEY') | ||
|
||
# grab values from config files | ||
config = ConfigParser.RawConfigParser() | ||
config.read([ | ||
'.cloudflare.cfg', | ||
os.path.expanduser('~/.cloudflare.cfg'), | ||
os.path.expanduser('~/.cloudflare/cloudflare.cfg') | ||
]) | ||
|
||
if email is None: | ||
try: | ||
email = re.sub(r"\s+", '', config.get('CloudFlare', 'email')) | ||
except: | ||
email = None | ||
|
||
if token is None: | ||
try: | ||
token = re.sub(r"\s+", '', config.get('CloudFlare', 'token')) | ||
except: | ||
token = None | ||
|
||
if certtoken is None: | ||
try: | ||
certtoken = re.sub(r"\s+", '', config.get('CloudFlare', 'certtoken')) | ||
except: | ||
certtoken = None | ||
|
||
try: | ||
extras = re.sub(r"\s+", ' ', config.get('CloudFlare', 'extras')) | ||
except: | ||
extras = None | ||
|
||
if extras: | ||
extras = extras.split(' ') | ||
|
||
return [email, token, certtoken, extras] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,18 @@ | ||
""" misc utilities for CloudFlare API""" | ||
|
||
def sanitize_secrets(secrets): | ||
redacted_phrase = 'REDACTED' | ||
""" misc utilities for CloudFlare API""" | ||
redacted_phrase = 'REDACTED' | ||
|
||
if secrets is None: | ||
return None | ||
if secrets is None: | ||
return None | ||
|
||
secrets_copy = secrets.copy() | ||
if 'password' in secrets_copy: | ||
secrets_copy['password'] = redacted_phrase | ||
elif 'X-Auth-Key' in secrets_copy: | ||
secrets_copy['X-Auth-Key'] = redacted_phrase | ||
elif 'X-Auth-User-Service-Key' in secrets_copy: | ||
secrets_copy['X-Auth-User-Service-Key'] = redacted_phrase | ||
secrets_copy = secrets.copy() | ||
if 'password' in secrets_copy: | ||
secrets_copy['password'] = redacted_phrase | ||
elif 'X-Auth-Key' in secrets_copy: | ||
secrets_copy['X-Auth-Key'] = redacted_phrase | ||
elif 'X-Auth-User-Service-Key' in secrets_copy: | ||
secrets_copy['X-Auth-User-Service-Key'] = redacted_phrase | ||
|
||
return secrets_copy | ||
return secrets_copy |
Oops, something went wrong.