Skip to content

Commit 547fca0

Browse files
committed
Move sanitize back to .exceptions
1 parent e40e9af commit 547fca0

2 files changed

Lines changed: 22 additions & 28 deletions

File tree

webexteamsdk/exceptions.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,20 @@
3030
unicode_literals,
3131
)
3232

33-
from builtins import *
3433
import sys
3534
import textwrap
35+
from builtins import *
3636

37-
from past.builtins import basestring
3837
import requests
38+
from past.builtins import basestring
3939

4040
from .response_codes import SPARK_RESPONSE_CODES
4141

4242

4343
# Helper functions
4444

4545

46-
def to_unicode(string):
46+
def _to_unicode(string):
4747
"""Convert a string (bytes, str or unicode) to unicode."""
4848
assert isinstance(string, basestring)
4949
if sys.version_info[0] >= 3:
@@ -58,7 +58,22 @@ def to_unicode(string):
5858
return string
5959

6060

61-
def response_to_string(response):
61+
def _sanitize(header_tuple):
62+
"""Sanitize request headers.
63+
64+
Remove authentication `Bearer` token.
65+
"""
66+
header, value = header_tuple
67+
68+
if (header.lower().strip() == "Authorization".lower().strip()
69+
and "Bearer".lower().strip() in value.lower().strip()):
70+
return header, "Bearer <redacted>"
71+
72+
else:
73+
return header_tuple
74+
75+
76+
def _response_to_string(response):
6277
"""Render a response object as a human readable string."""
6378
assert isinstance(response, requests.Response)
6479
request = response.request
@@ -70,12 +85,12 @@ def response_to_string(response):
7085
width=79,
7186
subsequent_indent=' ' * (len(request.method) + 1))
7287
req_headers = [
73-
textwrap.fill("{}: {}".format(*sanitize(header)),
88+
textwrap.fill("{}: {}".format(*_sanitize(header)),
7489
width=79,
7590
subsequent_indent=' ' * 4)
7691
for header in request.headers.items()
7792
]
78-
req_body = (textwrap.fill(to_unicode(request.body), width=79)
93+
req_body = (textwrap.fill(_to_unicode(request.body), width=79)
7994
if request.body else "")
8095

8196
# Prepare response components
@@ -130,7 +145,7 @@ def __init__(self, response):
130145
response_reason = " " + response.reason if response.reason else ""
131146
description = SPARK_RESPONSE_CODES.get(response_code,
132147
"Unknown Response Code")
133-
detail = response_to_string(response)
148+
detail = _response_to_string(response)
134149

135150
super(SparkApiError, self).__init__("Response Code [{}]{} - {}\n{}"
136151
"".format(response_code,

webexteamsdk/utils.py

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,6 @@
4646
from .response_codes import RATE_LIMIT_RESPONSE_CODE
4747

4848

49-
__author__ = "Chris Lunsford"
50-
__author_email__ = "chrlunsf@cisco.com"
51-
__copyright__ = "Copyright (c) 2016-2018 Cisco and/or its affiliates."
52-
__license__ = "MIT"
53-
54-
5549
EncodableFile = namedtuple('EncodableFile',
5650
['file_name', 'file_object', 'content_type'])
5751

@@ -249,18 +243,3 @@ def json_dict(json_data):
249243
"'json_data' must be a dictionary or valid JSON string; "
250244
"received: {!r}".format(json_data)
251245
)
252-
253-
254-
def sanitize(header_tuple):
255-
"""Sanitize request headers.
256-
257-
Remove authentication `Bearer` token.
258-
"""
259-
header, value = header_tuple
260-
261-
if (header.lower().strip() == "Authorization".lower().strip()
262-
and "Bearer".lower().strip() in value.lower().strip()):
263-
return header, "Bearer <redacted>"
264-
265-
else:
266-
return header_tuple

0 commit comments

Comments
 (0)