Skip to content

Commit

Permalink
added User-Agent support to help debug calls - should have been done …
Browse files Browse the repository at this point in the history
…on day zero - oh well
  • Loading branch information
mahtin committed Dec 27, 2016
1 parent cc467af commit 9d722f1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
6 changes: 5 additions & 1 deletion CloudFlare/cloudflare.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import requests

from logger import Logger
from utils import sanitize_secrets
from utils import user_agent, sanitize_secrets
from read_configs import read_configs
from api_v4 import api_v4
from api_extras import api_extras
Expand All @@ -27,6 +27,7 @@ def __init__(self, email, token, certtoken, base_url, debug, raw):
self.certtoken = certtoken
self.base_url = base_url
self.raw = raw
self.user_agent = user_agent()

if debug:
self.logger = Logger(debug).getLogger()
Expand All @@ -42,6 +43,7 @@ def call_with_no_auth(self, method,
""" Cloudflare v4 API"""

headers = {
'User-Agent': self.user_agent,
'Content-Type': 'application/json'
}
return self._call(method, headers,
Expand All @@ -60,6 +62,7 @@ def call_with_auth(self, method,
if self.email is '' or self.token is '':
raise CloudFlareAPIError(0, 'no email and/or token defined')
headers = {
'User-Agent': self.user_agent,
'X-Auth-Email': self.email,
'X-Auth-Key': self.token,
'Content-Type': 'application/json'
Expand All @@ -80,6 +83,7 @@ def call_with_certauth(self, method,
if self.certtoken is '' or self.certtoken is None:
raise CloudFlareAPIError(0, 'no cert token defined')
headers = {
'User-Agent': self.user_agent,
'X-Auth-User-Service-Key': self.certtoken,
'Content-Type': 'application/json'
}
Expand Down
13 changes: 13 additions & 0 deletions CloudFlare/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
""" misc utilities for Cloudflare API"""

import sys
import requests
from __init__ import __version__

def user_agent():
""" misc utilities for Cloudflare API"""
# the default User-Agent is something like 'python-requests/2.11.1'
# this additional data helps support @ Cloudflare help customers
return ('python-cloudflare/' + __version__ + '/' +
'python-requests/' + str(requests.__version__) + '/' +
'python/' + '.'.join(map(str, sys.version_info[:3]))
)

def sanitize_secrets(secrets):
""" misc utilities for Cloudflare API"""
redacted_phrase = 'REDACTED'
Expand Down

0 comments on commit 9d722f1

Please sign in to comment.