8000 Fixes issue #754 by moving file read logic inside generator · tableau/server-client-python@e624178 · GitHub
[go: up one dir, main page]

Skip to content

Commit e624178

Browse files
author
Chris Shin
committed
Fixes issue #754 by moving file read logic inside generator
1 parent 62b264c commit e624178

File tree

4 files changed

+86
-6
lines changed

4 files changed

+86
-6
lines changed

tableauserverclient/server/endpoint/fileuploads_endpoint.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,18 @@ def append(self, xml_request, content_type):
4040
return FileuploadItem.from_response(server_response.content, self.parent_srv.namespace)
4141

4242
def read_chunks(self, file):
43+
file_opened = False
44+
try:
45+
file_content = open(file, 'rb')
46+
file_opened = True
47+
except TypeError:
48+
file_content = file
4349

4450
while True:
45-
chunked_content = file.read(CHUNK_SIZE)
51+
chunked_content = file_content.read(CHUNK_SIZE)
4652
if not chunked_content:
53+
if file_opened:
54+
file_content.close()
4755
break
4856
yield chunked_content
4957

@@ -52,11 +60,7 @@ def upload_chunks(cls, parent_srv, file):
5260
file_uploader = cls(parent_srv)
5361
upload_id = file_uploader.initiate()
5462

55-
try:
56-
with open(file, 'rb') as f:
57-
chunks = file_uploader.read_chunks(f)
58-
except TypeError:
59-
chunks = file_uploader.read_chunks(file)
63+
chunks = file_uploader.read_chunks(file)
6064
for chunk in chunks:
6165
xml_request, content_type = RequestFactory.Fileupload.chunk_req(chunk)
6266
fileupload_item = file_uploader.append(xml_request, content_type)

test/assets/fileupload_append.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?xml version='1.0' encoding='UTF-8'?><tsResponse xmlns="http://tableau.com/api" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://tableau.com/api http://tableau.com/api/ts-api-3.11.xsd">
2+
<fileUpload uploadSessionId="3877:63f8579222de403ea574673e22dafdca-1:0" fileSize="5"/>
3+
</tsResponse>

test/assets/fileupload_initialize.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<tsResponse xmlns="http://tableau.com/api" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://tableau.com/api http://tableau.com/api/ts-api-3.11.xsd">
2+
<fileUpload uploadSessionId="7720:170fe6b1c1c7422dadff20f944d58a52-1:0" fileSize="0"/>
3+
</tsResponse>

test/test_fileuploads.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import os
2+
import requests_mock
3+
import unittest
4+
5+
from ._utils import asset
6+
from tableauserverclient.server import Server
7+
from tableauserverclient.server.endpoint.fileuploads_endpoint import Fileuploads
8+
9+
TEST_ASSET_DIR = os.path.join(os.path.dirname(__file__), 'assets')
10+
FILEUPLOAD_INITIALIZE = os.path.join(TEST_ASSET_DIR, 'fileupload_initialize.xml')
11+
FILEUPLOAD_APPEND = os.path.join(TEST_ASSET_DIR, 'fileupload_append.xml')
12+
13+
14+
class FileuploadsTests(unittest.TestCase):
15+
def setUp(self):
16+
self.server = Server('http://test')
17+
18+
# Fake sign in
19+
self.server._site_id = 'dad65087-b08b-4603-af4e-2887b8aafc67'
20+
self.server._auth_token = 'j80k54ll2lfMZ0tv97mlPvvSCRyD0DOM'
21+
22+
self.baseurl = '{}/sites/{}/fileUploads'.format(self.server.baseurl, self.server.site_id)
23+
24+
def test_read_chunks_file_path(self):
25+
fileuploads = Fileuploads(self.server)
26+
27+
file_path = asset('SampleWB.twbx')
28+
chunks = fileuploads.read_chunks(file_path)
29+
for chunk in chunks:
30+
self.assertIsNotNone(chunk)
31+
32+
def test_read_chunks_file_object(self):
33+
fileuploads = Fileuploads(self.server)
34+
35+
with open(asset('SampleWB.twbx'), 'rb') as f:
36+
chunks = fileuploads.read_chunks(f)
37+
for chunk in chunks:
38+
self.assertIsNotNone(chunk)
39+
40+
def test_upload_chunks_file_path(self):
41+
fileuploads = Fileuploads(self.server)
42+
file_path = asset('SampleWB.twbx')
43+
upload_id = '7720:170fe6b1c1c7422dadff20f944d58a52-1:0'
44+
45+
with open(FILEUPLOAD_INITIALIZE, 'rb') as f:
46+
initialize_response_xml = f.read().decode('utf-8')
47+
with open(FILEUPLOAD_APPEND, 'rb') as f:
48+
append_response_xml = f.read().decode('utf-8')
49+
with requests_mock.mock() as m:
50+
m.post(self.baseurl, text=initialize_response_xml)
51+
m.put(self.baseurl + '/' + upload_id, text=append_response_xml)
52+
actual = fileuploads.upload_chunks(self.server, file_path)
53+
54+
self.assertEqual(upload_id, actual)
55+
56+
def test_upload_chunks_file_object(self):
57+
fileuploads = Fileuploads(self.server)
58+
upload_id = '7720:170fe6b1c1c7422dadff20f944d58a52-1:0'
59+
60+
with open(asset('SampleWB.twbx'), 'rb') as file_content:
61+
with open(FILEUPLOAD_INITIALIZE, 'rb') as f:
62+
initialize_response_xml = f.read().decode('utf-8')
63+
with open(FILEUPLOAD_APPEND, 'rb') as f:
64+
append_response_xml = f.read().decode('utf-8')
65+
with requests_mock.mock() as m:
66+
m.post(self.baseurl, text=initialize_response_xml)
67+
m.put(self.baseurl + '/' + upload_id, text=append_response_xml)
68+
actual = fileuploads.upload_chunks(self.server, file_content)
69+
70+
self.assertEqual(upload_id, actual)

0 commit comments

Comments
 (0)
0