Skip to content

Commit 6aa0928

Browse files
committed
Remove HTTP session formating functions
1 parent 8cfce59 commit 6aa0928

File tree

1 file changed

+1
-83
lines changed

1 file changed

+1
-83
lines changed

webexteamssdk/exceptions.py

Lines changed: 1 addition & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -29,101 +29,19 @@
2929
print_function,
3030
unicode_literals,
3131
)
32-
from builtins import *
3332

3433
import logging
35-
import sys
36-
import textwrap
34+
from builtins import *
3735
from json import JSONDecodeError
3836

3937
import requests
40-
from past.builtins import basestring
4138

4239
from .response_codes import RESPONSE_CODES
4340

4441

4542
logger = logging.getLogger(__name__)
4643

4744

48-
def _to_unicode(string):
49-
"""Convert a string (bytes, str or unicode) to unicode."""
50-
assert isinstance(string, basestring)
51-
if sys.version_info[0] >= 3:
52-
if isinstance(string, bytes):
53-
return string.decode('utf-8')
54-
else:
55-
return string
56-
else:
57-
if isinstance(string, str):
58-
return string.decode('utf-8')
59-
else:
60-
return string
61-
62-
63-
def _sanitize(header_tuple):
64-
"""Sanitize request headers.
65-
66-
Remove authentication `Bearer` token.
67-
"""
68-
header, value = header_tuple
69-
70-
if (header.lower().strip() == "Authorization".lower().strip()
71-
and "Bearer".lower().strip() in value.lower().strip()):
72-
return header, "Bearer <redacted>"
73-
74-
else:
75-
return header_tuple
76-
77-
78-
def _response_to_string(response):
79-
"""Render a response object as a human readable string."""
80-
assert isinstance(response, requests.Response)
81-
request = response.request
82-
83-
section_header = "{title:-^79}"
84-
85-
# Prepare request components
86-
req = textwrap.fill("{} {}".format(request.method, request.url),
87-
width=79,
88-
subsequent_indent=' ' * (len(request.method) + 1))
89-
req_headers = [
90-
textwrap.fill("{}: {}".format(*_sanitize(header)),
91-
width=79,
92-
subsequent_indent=' ' * 4)
93-
for header in request.headers.items()
94-
]
95-
req_body = (textwrap.fill(_to_unicode(request.body), width=79)
96-
if request.body else "")
97-
98-
# Prepare response components
99-
resp = textwrap.fill("{} {}".format(response.status_code,
100-
response.reason
101-
if response.reason else ""),
102-
width=79,
103-
subsequent_indent=' ' * (len(request.method) + 1))
104-
resp_headers = [
105-
textwrap.fill("{}: {}".format(*header), width=79,
106-
subsequent_indent=' ' * 4)
107-
for header in response.headers.items()
108-
]
109-
resp_body = textwrap.fill(response.text, width=79) if response.text else ""
110-
111-
# Return the combined string
112-
return "\n".join([
113-
section_header.format(title="Request"),
114-
req,
115-
"\n".join(req_headers),
116-
"",
117-
req_body,
118-
"",
119-
section_header.format(title="Response"),
120-
resp,
121-
"\n".join(resp_headers),
122-
"",
123-
resp_body,
124-
])
125-
126-
12745
class webexteamssdkException(Exception):
12846
"""Base class for all webexteamssdk package exceptions."""
12947
pass

0 commit comments

Comments
 (0)