3030 unicode_literals ,
3131)
3232
33- from builtins import *
3433import sys
3534import textwrap
35+ from builtins import *
3636
37- from past .builtins import basestring
3837import requests
38+ from past .builtins import basestring
3939
4040from .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 ,
0 commit comments