From 7ecf71994637552d2edab10979b466c19c92c27f Mon Sep 17 00:00:00 2001 From: Alex Davies Date: Mon, 15 Jun 2020 23:09:01 -0300 Subject: [PATCH] Added streaming jsonl format --- documentation/OUTPUT_FORMATS.md | 1 + hug/output_format.py | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/documentation/OUTPUT_FORMATS.md b/documentation/OUTPUT_FORMATS.md index 2d2cf920..a09867b6 100644 --- a/documentation/OUTPUT_FORMATS.md +++ b/documentation/OUTPUT_FORMATS.md @@ -82,6 +82,7 @@ hug provides a large catalog of built-in output formats, which can be used to bu - `hug.output_format.html`: Outputs Hyper Text Markup Language (HTML). - `hug.output_format.json_camelcase`: Outputs in the JSON format, but first converts all keys to camelCase to better conform to Javascript coding standards. - `hug.output_format.pretty_json`: Outputs in the JSON format, with extra whitespace to improve human readability. + - `hug.output_format.json_streaming_lines`: Takes an interable or yield statements and streams it as newline-seperated json objects. - `hug.output_format.image(format)`: Outputs an image (of the specified format). - There are convenience calls in the form `hug.output_format.{FORMAT}_image for the following image types: 'png', 'jpg', 'bmp', 'eps', 'gif', 'im', 'jpeg', 'msp', 'pcx', 'ppm', 'spider', 'tiff', 'webp', 'xbm', 'cur', 'dcx', 'fli', 'flc', 'gbr', 'gd', 'ico', 'icns', 'imt', 'iptc', 'naa', 'mcidas', 'mpo', 'pcd', diff --git a/hug/output_format.py b/hug/output_format.py index 6e60dfb3..2b98b8a2 100644 --- a/hug/output_format.py +++ b/hug/output_format.py @@ -175,6 +175,16 @@ def json(content, request=None, response=None, ensure_ascii=False, **kwargs): content, default=_json_converter, ensure_ascii=ensure_ascii, **kwargs ).encode("utf8") +@content_type("application/jsonl; charset=utf-8") +def json_streaming_lines(data, request=None, response=None,**kwargs): + """Stream a json response using the jsonl format, where each unique json-object is + seperated by a newline + """ + now = str(datetime.utcnow()) + response.append_header('Content-Disposition', + (f'attachment; filename="{now}.jsonl"')) + response.stream = (json(i,indent=None,**kwargs)+b"\n" for i in data) + return def on_valid(valid_content_type, on_invalid=json): """Renders as the specified content type only if no errors are found in the provided data object"""