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

Commit 9d722f1

Browse files
committed
added User-Agent support to help debug calls - should have been done on day zero - oh well
1 parent cc467af commit 9d722f1

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

CloudFlare/cloudflare.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import requests
66

77
from logger import Logger
8-
from utils import sanitize_secrets
8+
from utils import user_agent, sanitize_secrets
99
from read_configs import read_configs
1010
from api_v4 import api_v4
1111
from api_extras import api_extras
@@ -27,6 +27,7 @@ def __init__(self, email, token, certtoken, base_url, debug, raw):
2727
self.certtoken = certtoken
2828
self.base_url = base_url
2929
self.raw = raw
30+
self.user_agent = user_agent()
3031

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

4445
headers = {
46+
'User-Agent': self.user_agent,
4547
'Content-Type': 'application/json'
4648
}
4749
return self._call(method, headers,
@@ -60,6 +62,7 @@ def call_with_auth(self, method,
6062
if self.email is '' or self.token is '':
6163
raise CloudFlareAPIError(0, 'no email and/or token defined')
6264
headers = {
65+
'User-Agent': self.user_agent,
6366
'X-Auth-Email': self.email,
6467
'X-Auth-Key': self.token,
6568
'Content-Type': 'application/json'
@@ -80,6 +83,7 @@ def call_with_certauth(self, method,
8083
if self.certtoken is '' or self.certtoken is None:
8184
raise CloudFlareAPIError(0, 'no cert token defined')
8285
headers = {
86+
'User-Agent': self.user_agent,
8387
'X-Auth-User-Service-Key': self.certtoken,
8488
'Content-Type': 'application/json'
8589
}

CloudFlare/utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
""" misc utilities for Cloudflare API"""
22

3+
import sys
4+
import requests
5+
from __init__ import __version__
6+
7+
def user_agent():
8+
""" misc utilities for Cloudflare API"""
9+
# the default User-Agent is something like 'python-requests/2.11.1'
10+
# this additional data helps support @ Cloudflare help customers
11+
return ('python-cloudflare/' + __version__ + '/' +
12+
'python-requests/' + str(requests.__version__) + '/' +
13+
'python/' + '.'.join(map(str, sys.version_info[:3]))
14+
)
15+
316
def sanitize_secrets(secrets):
417
""" misc utilities for Cloudflare API"""
518
redacted_phrase = 'REDACTED'

0 commit comments

Comments
 (0)