Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

Commit 3c48798

Browse files
committed
add exceptions + escaping urls
1 parent 28daae8 commit 3c48798

2 files changed

Lines changed: 13 additions & 4 deletions

File tree

cloudflare_v4/__init__.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# all the exceptions
2-
from .exceptions import CloudFlareError, CloudFlareAPIError
2+
from .exceptions import CloudFlareError, CloudFlareAPIError, CloudFlareInternalError
33
from . import logger
44

55
import json
66
import requests
7-
7+
import urllib
88

99
BASE_URL = 'https://api.cloudflare.com/client/v4'
1010

@@ -17,6 +17,7 @@ def __init__(self, email, token, debug):
1717

1818
def call(self, method, main_endpoint, endpoint=None, params=None, data=None):
1919
headers = { "X-Auth-Email": self.EMAIL, "X-Auth-Key": self.TOKEN }
20+
headers['Content-Type'] = 'application/json'
2021
if endpoint is not None or (data is not None and method == 'GET'):
2122
url = BASE_URL + '/' + main_endpoint + '/' + params + '/' + endpoint
2223
else:
@@ -31,22 +32,27 @@ def call(self, method, main_endpoint, endpoint=None, params=None, data=None):
3132
self.logger.debug("optional params is: %s" % str(params))
3233
self.logger.debug("optional data is: %s" % str(data))
3334
if (method is None) or (main_endpoint is None):
34-
raise CloudFlareError('You must specify a method and endpoint') # should never happen
35+
raise CloudFlareInternalError('You must specify a method and endpoint') # should never happen
3536
else:
3637
self.logger.debug("headers being sent: %s" % str(headers))
3738
if method == 'GET':
3839
if data:
3940
params_to_send = data
4041
else:
4142
params_to_send = params
43+
if params_to_send.has_key('content'):
44+
params_to_send['content'] = urllib.quote(params_to_send['content'])
4245
self.logger.debug("params being sent: %s", params_to_send)
4346
response = requests.get(url, headers=headers,
4447
params=params_to_send)
4548
elif method == 'POST':
46-
headers['Content-Type'] = 'application/json'
4749
response = requests.post(url, headers=headers, json=data)
4850
elif method == 'DELETE':
4951
response = requests.delete("%s/%s" % (url, data), headers=headers)
52+
elif method == 'PATCH':
53+
pass
54+
else:
55+
raise CloudFlareAPIInternalError('method not supported') # should never happen
5056
data = response.text
5157
self.logger.debug("data received: %s" % data)
5258
try:

cloudflare_v4/exceptions.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ def __str__(self):
66

77
class CloudFlareAPIError(CloudFlareError):
88
pass
9+
10+
class CloudFlareInternalError(CloudFlareError):
11+
pass

0 commit comments

Comments
 (0)