From 8e79a53d3cbc1f486fe027083712b9a9a0546552 Mon Sep 17 00:00:00 2001 From: Felix Wong Date: Sat, 14 Feb 2015 11:28:24 +0000 Subject: [PATCH] posting dns_records to zone --- cloudflare_v4/util.py | 28 ++++++++++++++++------------ cloudflare_v4/zones/dns_records.py | 7 +++++-- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/cloudflare_v4/util.py b/cloudflare_v4/util.py index 45041bd..063860a 100644 --- a/cloudflare_v4/util.py +++ b/cloudflare_v4/util.py @@ -4,24 +4,28 @@ import json import requests -logger = logger.Logger(logger.DEBUG).getLogger() +logger = logger.Logger(logger.INFO).getLogger() +BASE_URL = 'https://api.cloudflare.com/client/v4' def call(auth, method, endpoint, params=None): - logger.debug(auth) - logger.debug(method) - logger.debug(endpoint) - logger.debug(params) + headers = { "X-Auth-Email": auth['EMAIL'], "X-Auth-Key": auth['TOKEN'] } + url = BASE_URL + '/' + endpoint + 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: - response = requests.request(method, - 'https://api.cloudflare.com/client/v4/' + endpoint, - headers={ "X-Auth-Email": auth['EMAIL'], - "X-Auth-Key": auth['TOKEN'] }, - params=params - ) + if method.upper() == 'GET': + logger.debug("headers being sent: " + str(headers)) + response = requests.get(url, headers=headers, params=params) + elif method.upper() == 'POST': + headers['Content-Type'] = 'application/json' + logger.debug("headers being sent: " + str(headers)) + response = requests.post(url, headers=headers, json=params) data = response.text - logger.debug(data) + logger.debug("data received: " + data) try: data = json.loads(data) return data diff --git a/cloudflare_v4/zones/dns_records.py b/cloudflare_v4/zones/dns_records.py index 517a2e5..741917d 100644 --- a/cloudflare_v4/zones/dns_records.py +++ b/cloudflare_v4/zones/dns_records.py @@ -2,5 +2,8 @@ ENDPOINT = 'dns_records' -def get(auth, params=None): - util.call(auth, 'GET', 'zones/' + params + '/' + ENDPOINT) +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)