|
1 | 1 | # all the exceptions |
2 | 2 | from exceptions import CloudFlareError, CloudFlareAPIError |
| 3 | +from . import util |
| 4 | +from . import CloudFlareError, CloudFlareAPIError |
| 5 | +from . import logger |
3 | 6 |
|
4 | | -from construct import CloudFlare |
| 7 | +import json |
| 8 | +import requests |
5 | 9 |
|
6 | | -# depends on exceptions |
7 | | -from util import call |
8 | 10 |
|
9 | | -__all__ = [ 'CloudFlareError', 'CloudFlareAPIError', |
10 | | - 'zones', 'user', |
11 | | - 'util' ] |
| 11 | +BASE_URL = 'https://api.cloudflare.com/client/v4' |
| 12 | + |
| 13 | +class CloudFlare(object): |
| 14 | + class BaseClient: |
| 15 | + def __init__(self, email, token, debug): |
| 16 | + self.EMAIL = email |
| 17 | + self.TOKEN = token |
| 18 | + self.logger = logger.Logger(debug).getLogger() |
| 19 | + |
| 20 | + def call(self, method, endpoint, params=None): |
| 21 | + headers = { "X-Auth-Email": self.EMAIL, "X-Auth-Key": self.TOKEN } |
| 22 | + url = BASE_URL + '/' + endpoint |
| 23 | + method = method.upper() |
| 24 | + self.logger.debug("EMAIL is: " + str(self.EMAIL)) |
| 25 | + self.logger.debug("TOKEN is: " + str(self.TOKEN)) |
| 26 | + self.logger.debug("method type is: " + method) |
| 27 | + self.logger.debug("url endpoint is: " + url) |
| 28 | + self.logger.debug("optional params is: " + str(params)) |
| 29 | + if (method is None) or (endpoint is None): |
| 30 | + raise CloudFlareError('You must specify a method and endpoint') |
| 31 | + else: |
| 32 | + if method == 'GET': |
| 33 | + self.logger.debug("headers being sent: " + str(headers)) |
| 34 | + response = requests.get(url, headers=headers, params=params) |
| 35 | + elif method == 'POST': |
| 36 | + headers['Content-Type'] = 'application/json' |
| 37 | + self.logger.debug("headers being sent: " + str(headers)) |
| 38 | + response = requests.post(url, headers=headers, json=params) |
| 39 | + elif method == 'DELETE': |
| 40 | + self.logger.debug("headers being sent: " + str(headers)) |
| 41 | + response = requests.delete(url, headers=headers, json=params) |
| 42 | + data = response.text |
| 43 | + self.logger.debug("data received: " + data) |
| 44 | + try: |
| 45 | + data = json.loads(data) |
| 46 | + return data |
| 47 | + except ValueError: |
| 48 | + raise CloudFlareAPIError('JSON parse failed.') |
| 49 | + if data['result'] == 'error': |
| 50 | + raise CloudFlareAPIError(data['msg']) |
| 51 | + |
| 52 | + class DynamicClient: |
| 53 | + def __init__(self, base_client, url_type): |
| 54 | + self.base_client = base_client |
| 55 | + self.url_type = url_type |
| 56 | + |
| 57 | + def get(self, params=None): |
| 58 | + return self.base_client.call('GET', self.url_type, params) |
| 59 | + |
| 60 | + def post(self, params=None): |
| 61 | + return self.base_client.call('POST', self.url_type, params) |
| 62 | + |
| 63 | + def delete(self, params=None): |
| 64 | + return self.base_client.call('DELETE', self.url_type, params) |
| 65 | + |
| 66 | + def __init__(self, email, token, debug): |
| 67 | + self.base_client = self.BaseClient(email, token, debug) |
| 68 | + setattr(self, "zones", self.DynamicClient(self.base_client, "zones")) |
| 69 | + setattr(self, "user", self.DynamicClient(self.base_client, "user")) |
0 commit comments