Skip to content

Commit

Permalink
actually make methods slightly more dynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
fawaf committed Aug 2, 2015
1 parent 4a6acab commit 9b895b1
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 95 deletions.
70 changes: 64 additions & 6 deletions cloudflare_v4/__init__.py
Original file line number Diff line number Diff line change
@@ -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"))
7 changes: 0 additions & 7 deletions cloudflare_v4/construct.py

This file was deleted.

15 changes: 10 additions & 5 deletions cloudflare_v4/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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
6 changes: 0 additions & 6 deletions cloudflare_v4/user/__init__.py

This file was deleted.

46 changes: 0 additions & 46 deletions cloudflare_v4/util.py

This file was deleted.

13 changes: 0 additions & 13 deletions cloudflare_v4/zones/__init__.py

This file was deleted.

12 changes: 0 additions & 12 deletions cloudflare_v4/zones/dns_records.py

This file was deleted.

0 comments on commit 9b895b1

Please sign in to comment.