From 9b895b133398423855357e2774d8ff3336320297 Mon Sep 17 00:00:00 2001 From: Felix Wong Date: Sun, 2 Aug 2015 02:56:35 -0700 Subject: [PATCH] actually make methods slightly more dynamic --- cloudflare_v4/__init__.py | 70 +++++++++++++++++++++++++++--- cloudflare_v4/construct.py | 7 --- cloudflare_v4/logger.py | 15 ++++--- cloudflare_v4/user/__init__.py | 6 --- cloudflare_v4/util.py | 46 -------------------- cloudflare_v4/zones/__init__.py | 13 ------ cloudflare_v4/zones/dns_records.py | 12 ----- 7 files changed, 74 insertions(+), 95 deletions(-) delete mode 100644 cloudflare_v4/construct.py delete mode 100644 cloudflare_v4/user/__init__.py delete mode 100644 cloudflare_v4/util.py delete mode 100644 cloudflare_v4/zones/__init__.py delete mode 100644 cloudflare_v4/zones/dns_records.py diff --git a/cloudflare_v4/__init__.py b/cloudflare_v4/__init__.py index 75f76b3..c9cc7ff 100644 --- a/cloudflare_v4/__init__.py +++ b/cloudflare_v4/__init__.py @@ -1,11 +1,69 @@ # all the exceptions from exceptions import CloudFlareError, CloudFlareAPIError +from . import util +from . import CloudFlareError, CloudFlareAPIError +from . import logger -from construct import CloudFlare +import json +import requests -# depends on exceptions -from util import call -__all__ = [ 'CloudFlareError', 'CloudFlareAPIError', - 'zones', 'user', - 'util' ] +BASE_URL = 'https://api.cloudflare.com/client/v4' + +class CloudFlare(object): + class BaseClient: + def __init__(self, email, token, debug): + self.EMAIL = email + self.TOKEN = token + self.logger = logger.Logger(debug).getLogger() + + def call(self, method, endpoint, params=None): + headers = { "X-Auth-Email": self.EMAIL, "X-Auth-Key": self.TOKEN } + url = BASE_URL + '/' + endpoint + method = method.upper() + self.logger.debug("EMAIL is: " + str(self.EMAIL)) + self.logger.debug("TOKEN is: " + str(self.TOKEN)) + self.logger.debug("method type is: " + method) + self.logger.debug("url endpoint is: " + url) + self.logger.debug("optional params is: " + str(params)) + if (method is None) or (endpoint is None): + raise CloudFlareError('You must specify a method and endpoint') + else: + if method == 'GET': + self.logger.debug("headers being sent: " + str(headers)) + response = requests.get(url, headers=headers, params=params) + elif method == 'POST': + headers['Content-Type'] = 'application/json' + self.logger.debug("headers being sent: " + str(headers)) + response = requests.post(url, headers=headers, json=params) + elif method == 'DELETE': + self.logger.debug("headers being sent: " + str(headers)) + response = requests.delete(url, headers=headers, json=params) + data = response.text + self.logger.debug("data received: " + data) + try: + data = json.loads(data) + return data + except ValueError: + raise CloudFlareAPIError('JSON parse failed.') + if data['result'] == 'error': + raise CloudFlareAPIError(data['msg']) + + class DynamicClient: + def __init__(self, base_client, url_type): + self.base_client = base_client + self.url_type = url_type + + def get(self, params=None): + return self.base_client.call('GET', self.url_type, params) + + def post(self, params=None): + return self.base_client.call('POST', self.url_type, params) + + def delete(self, params=None): + return self.base_client.call('DELETE', self.url_type, params) + + def __init__(self, email, token, debug): + self.base_client = self.BaseClient(email, token, debug) + setattr(self, "zones", self.DynamicClient(self.base_client, "zones")) + setattr(self, "user", self.DynamicClient(self.base_client, "user")) diff --git a/cloudflare_v4/construct.py b/cloudflare_v4/construct.py deleted file mode 100644 index e2ee184..0000000 --- a/cloudflare_v4/construct.py +++ /dev/null @@ -1,7 +0,0 @@ -import zones -import user - -class CloudFlare(object): - def __init__(self, email, token): - self.EMAIL = email - self.TOKEN = token diff --git a/cloudflare_v4/logger.py b/cloudflare_v4/logger.py index db45e4b..ee35f66 100644 --- a/cloudflare_v4/logger.py +++ b/cloudflare_v4/logger.py @@ -5,16 +5,21 @@ class Logger: def __init__(self, level): - self.logger_level = self.get_level(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 def getLogger(self): # create logger logger = logging.getLogger('Python CloudFlare API v4') logger.setLevel(self.logger_level) - # create console handler and set level to debug ch = logging.StreamHandler() - ch.setLevel(logging.DEBUG) + ch.setLevel(self.logger_level) # create formatter formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') @@ -27,8 +32,8 @@ def getLogger(self): return logger - def get_level(self, level): - if level == 0: + def _get_logging_level(self, level): + if level == True: return logging.DEBUG else: return logging.INFO diff --git a/cloudflare_v4/user/__init__.py b/cloudflare_v4/user/__init__.py deleted file mode 100644 index c65d094..0000000 --- a/cloudflare_v4/user/__init__.py +++ /dev/null @@ -1,6 +0,0 @@ -from .. import util - -ENDPOINT = 'user' - -def get(auth, params=None): - return util.call(auth, 'GET', ENDPOINT, params) diff --git a/cloudflare_v4/util.py b/cloudflare_v4/util.py deleted file mode 100644 index 422933f..0000000 --- a/cloudflare_v4/util.py +++ /dev/null @@ -1,46 +0,0 @@ -from . import CloudFlareError, CloudFlareAPIError -from . import logger - -import json -import requests -import logging - -logging.basicConfig() -logging.getLogger().setLevel(logging.DEBUG) -request_logger = logging.getLogger("requests.packages.urllib3") -request_logger.setLevel(logging.DEBUG) -request_logger.propagate = True - -logger = logger.Logger(logger.DEBUG).getLogger() -BASE_URL = 'https://api.cloudflare.com/client/v4' - -def call(auth, method, endpoint, params=None): - headers = { "X-Auth-Email": auth['EMAIL'], "X-Auth-Key": auth['TOKEN'] } - url = BASE_URL + '/' + endpoint - method = method.upper() - logger.debug("auth is: " + str(auth)) - logger.debug("method type is: " + method) - logger.debug("url endpoint is: " + url) - logger.debug("optional params is: " + str(params)) - if (auth is None) or (method is None) or (endpoint is None): - raise CloudFlareError('You must specify auth, method, and endpoint') - else: - if method == 'GET': - logger.debug("headers being sent: " + str(headers)) - response = requests.get(url, headers=headers, params=params) - elif method == 'POST': - headers['Content-Type'] = 'application/json' - logger.debug("headers being sent: " + str(headers)) - response = requests.post(url, headers=headers, json=params) - elif method == 'DELETE': - logger.debug("headers being sent: " + str(headers)) - response = requests.delete(url, headers=headers, json=params) - data = response.text - logger.debug("data received: " + data) - try: - data = json.loads(data) - return data - except ValueError: - raise CloudFlareAPIError('JSON parse failed.') - if data['result'] == 'error': - raise CloudFlareAPIError(data['msg']) diff --git a/cloudflare_v4/zones/__init__.py b/cloudflare_v4/zones/__init__.py deleted file mode 100644 index d6f5205..0000000 --- a/cloudflare_v4/zones/__init__.py +++ /dev/null @@ -1,13 +0,0 @@ -from .. import util -from . import dns_records - -ENDPOINT = 'zones' - -def get(auth, params=None): - if type(params) is dict: - return util.call(auth, 'GET', ENDPOINT, params) - elif type(params) is str: - return util.call(auth, 'GET', ENDPOINT + '/' + params) - -def purge(auth, params): - return util.call(auth, 'DELETE', ENDPOINT + '/' + params + '/purge_cache', { 'purge_everything': True }) diff --git a/cloudflare_v4/zones/dns_records.py b/cloudflare_v4/zones/dns_records.py deleted file mode 100644 index ac0a901..0000000 --- a/cloudflare_v4/zones/dns_records.py +++ /dev/null @@ -1,12 +0,0 @@ -from .. import util - -ENDPOINT = 'dns_records' - -def get(auth, zone_id, params=None): - return util.call(auth, 'GET', 'zones/' + zone_id + '/' + ENDPOINT, params) - -def post(auth, zone_id, params=None): - return util.call(auth, 'POST', 'zones/' + zone_id + '/' + ENDPOINT, params) - -def put(auth, zone_id, params=None): - return util.call(auth, 'PUT', 'zones/' + zone_id + '/' + ENDPOINT, params)