Skip to content

Add option to return bytes stream to workbooks.download() method #811

Description

@YevhenKv

Hi,

It would be nice if workbooks.download() method could also return bytes stream for downloaded object. For example, add argument stream=True/False to download function that would define whether object should be downloaded into specified path or returned as a byte stream.
https://github.com/tableau/server-client-python/blob/master/tableauserverclient/server/endpoint/workbooks_endpoint.py#L134
In this case byte stream can be handled outside of library, for example it can be used to write data in AWS S3 bucket, GCS bucket or Azure Blob storage instead of local storage.

Example:
def download(self, workbook_id, filepath=None, include_extract=True, no_extract=None, stream=False):
if not workbook_id:
error = "Workbook ID undefined."
raise ValueError(error)
url = "{0}/{1}/content".format(self.baseurl, workbook_id)

if no_extract is False or no_extract is True:
    import warnings
    warnings.warn('no_extract is deprecated, use include_extract instead.', DeprecationWarning)
    include_extract = not no_extract

if not include_extract:
    url += "?includeExtract=False"

with closing(self.get_request(url, parameters={"stream": True})) as server_response:
    _, params = cgi.parse_header(server_response.headers['Content-Disposition'])
    filename = to_filename(os.path.basename(params['filename']))
    if filepath:
        download_path = make_download_path(filepath, filename)

        with open(download_path, 'wb') as f:
            for chunk in server_response.iter_content(1024):  # 1KB
                f.write(chunk)
        logger.info('Downloaded workbook to {0} (ID: {1})'.format(download_path, workbook_id))
        return os.path.abspath(download_path)
    if stream:
        return server_response.content

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions