8000 Add a simple static method to strip non-XML responses from debug log … · jsonhuff/server-client-python@6586931 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6586931

Browse files
authored
Add a simple static method to strip non-XML responses from debug log entries.
Tested manually, and a simple regression test added to the suite.
1 parent 86e4638 commit 6586931

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

samples/download_view_image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def main():
4343
tableau_auth = TSC.TableauAuth(args.username, password, site_id=site_id)
4444
server = TSC.Server(args.server)
4545
# The new endpoint was introduced in Version 2.5
46-
server.version = 2.5
46+
server.version = "2.5"
4747

4848
with server.auth.sign_in(tableau_auth):
4949
# Step 2: Query for the view that we want an image of

tableauserverclient/server/endpoint/endpoint.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ def _make_common_headers(auth_token, content_type):
2727

2828
return headers
2929

30+
@staticmethod
31+
def _safe_to_log(server_response):
32+
'''Checks if the server_response content is not xml (eg binary image or zip)
33+
and and replaces it with a constant
34+
'''
35+
ALLOWED_CONTENT_TYPES = ('application/xml',)
36+
if server_response.headers.get('Content-Type', None) not in ALLOWED_CONTENT_TYPES:
37+
return '[Truncated File Contents]'
38+
else:
39+
return server_response.content
40+
3041
def _make_request(self, method, url, content=None, request_object=None,
3142
auth_token=None, content_type=None, parameters=None):
3243
if request_object is not None:
@@ -50,7 +61,7 @@ def _make_request(self, method, url, content=None, request_object=None,
5061
return server_response
5162

5263
def _check_status(self, server_response):
53-
logger.debug(server_response.content)
64+
logger.debug(self._safe_to_log(server_response))
5465
if server_response.status_code not in Success_codes:
5566
raise ServerResponseError.from_response(server_response.content, self.parent_srv.namespace)
5667

test/test_regression_tests.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
11
import unittest
22
import tableauserverclient.server.request_factory as factory
3+
from tableauserverclient.server.endpoint import Endpoint
34

45

56
class BugFix257(unittest.TestCase):
67
def test_empty_request_works(self):
78
result = factory.EmptyRequest().empty_req()
89
self.assertEqual(b'<tsRequest />', result)
10+
11+
12+
class BugFix273(unittest.TestCase):
13+
def test_binary_log_truncated(self):
14+
15+
class FakeResponse(object):
16+
17+
headers = {'Content-Type': 'application/octet-stream'}
18+
content = b'\x1337' * 1000
19+
status_code = 200
20+
21+
server_response = FakeResponse()
22+
23+
self.assertEqual(Endpoint._safe_to_log(server_response), '[Truncated File Contents]')

0 commit comments

Comments
 (0)
0