|
29 | 29 | print_function, |
30 | 30 | unicode_literals, |
31 | 31 | ) |
32 | | -from builtins import * |
33 | 32 |
|
34 | 33 | import logging |
35 | | -import sys |
36 | | -import textwrap |
| 34 | +from builtins import * |
37 | 35 | from json import JSONDecodeError |
38 | 36 |
|
39 | 37 | import requests |
40 | | -from past.builtins import basestring |
41 | 38 |
|
42 | 39 | from .response_codes import RESPONSE_CODES |
43 | 40 |
|
44 | 41 |
|
45 | 42 | logger = logging.getLogger(__name__) |
46 | 43 |
|
47 | 44 |
|
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 | | - |
127 | 45 | class webexteamssdkException(Exception): |
128 | 46 | """Base class for all webexteamssdk package exceptions.""" |
129 | 47 | pass |
|
0 commit comments