Skip to content

Commit 74ef0a7

Browse files
authored
feat(constants): add CSV and Parquet media types (#2439)
* feat(constants): add csv and parquet media type Signed-off-by: Keming <kemingy94@gmail.com> * update the csv recipe to use the MEDIA_CSV Signed-off-by: Keming <kemingy94@gmail.com>
1 parent 82dc5fc commit 74ef0a7

File tree

8 files changed

+26
-5
lines changed

8 files changed

+26
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add :data:`~falcon.constants.MEDIA_PARQUET` and :data:`~falcon.constants.MEDIA_CSV`
2+
media file types to the constant file.

docs/user/recipes/output-csv.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ and then assign its value to :attr:`resp.text <falcon.Response.text>`:
2323
.. literalinclude:: ../../../examples/recipes/output_csv_text_asgi.py
2424
:language: python
2525

26-
Here we set the response ``Content-Type`` to ``"text/csv"`` as
26+
Here we set the response ``Content-Type`` to :data:`~falcon.constants.MEDIA_CSV` as
2727
recommended by `RFC 4180 <https://tools.ietf.org/html/rfc4180>`_, and assign
2828
the downloadable file name ``report.csv`` via the ``Content-Disposition``
2929
header (see also: :ref:`serve-downloadable-as`).

examples/recipes/output_csv_stream_asgi.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import csv
22

3+
import falcon
4+
35

46
class Report:
57
class PseudoTextStream:
@@ -28,6 +30,6 @@ async def fibonacci_generator(self, n=1000):
2830
stream.clear()
2931

3032
async def on_get(self, req, resp):
31-
resp.content_type = 'text/csv'
33+
resp.content_type = falcon.MEDIA_CSV
3234
resp.downloadable_as = 'report.csv'
3335
resp.stream = self.fibonacci_generator()

examples/recipes/output_csv_stream_wsgi.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import csv
22

3+
import falcon
4+
35

46
class Report:
57
class PseudoTextStream:
@@ -27,6 +29,6 @@ def fibonacci_generator(self, n=1000):
2729
stream.clear()
2830

2931
def on_get(self, req, resp):
30-
resp.content_type = 'text/csv'
32+
resp.content_type = falcon.MEDIA_CSV
3133
resp.downloadable_as = 'report.csv'
3234
resp.stream = self.fibonacci_generator()

examples/recipes/output_csv_text_asgi.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import csv
22
import io
33

4+
import falcon
5+
46

57
class Report:
68
async def on_get(self, req, resp):
@@ -10,6 +12,6 @@ async def on_get(self, req, resp):
1012
writer.writerow(('apples', 13))
1113
writer.writerow(('oranges', 37))
1214

13-
resp.content_type = 'text/csv'
15+
resp.content_type = falcon.MEDIA_CSV
1416
resp.downloadable_as = 'report.csv'
1517
resp.text = output.getvalue()

examples/recipes/output_csv_text_wsgi.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import csv
22
import io
33

4+
import falcon
5+
46

57
class Report:
68
def on_get(self, req, resp):
@@ -10,6 +12,6 @@ def on_get(self, req, resp):
1012
writer.writerow(('apples', 13))
1113
writer.writerow(('oranges', 37))
1214

13-
resp.content_type = 'text/csv'
15+
resp.content_type = falcon.MEDIA_CSV
1416
resp.downloadable_as = 'report.csv'
1517
resp.text = output.getvalue()

falcon/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -336,13 +336,15 @@
336336
from falcon.constants import DEFAULT_MEDIA_TYPE
337337
from falcon.constants import HTTP_METHODS
338338
from falcon.constants import MEDIA_BMP
339+
from falcon.constants import MEDIA_CSV
339340
from falcon.constants import MEDIA_GIF
340341
from falcon.constants import MEDIA_HTML
341342
from falcon.constants import MEDIA_JPEG
342343
from falcon.constants import MEDIA_JS
343344
from falcon.constants import MEDIA_JSON
344345
from falcon.constants import MEDIA_MSGPACK
345346
from falcon.constants import MEDIA_MULTIPART
347+
from falcon.constants import MEDIA_PARQUET
346348
from falcon.constants import MEDIA_PNG
347349
from falcon.constants import MEDIA_TEXT
348350
from falcon.constants import MEDIA_URLENCODED

falcon/constants.py

+9
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@
104104
# but the use of the 'x-' prefix is discouraged by RFC 6838.
105105
MEDIA_MSGPACK = 'application/msgpack'
106106

107+
MEDIA_PARQUET = 'application/vnd.apache.parquet'
108+
107109
MEDIA_MULTIPART = 'multipart/form-data'
108110

109111
MEDIA_URLENCODED = 'application/x-www-form-urlencoded'
@@ -137,6 +139,11 @@
137139
MEDIA_HTML = 'text/html; charset=utf-8'
138140
MEDIA_TEXT = 'text/plain; charset=utf-8'
139141

142+
# NOTE(kemingy): According to RFC 4180, common usage of CSV is US-ASCII,
143+
# but other charsets can also be used. We use UTF-8 to make it compatible
144+
# with most modern systems.
145+
MEDIA_CSV = 'text/csv; charset=utf-8'
146+
140147
MEDIA_JPEG = 'image/jpeg'
141148
MEDIA_PNG = 'image/png'
142149
MEDIA_GIF = 'image/gif'
@@ -167,6 +174,7 @@
167174
(ext, media_type.split(';', 1)[0])
168175
for ext, media_type in (
169176
('.bmp', MEDIA_BMP),
177+
('.csv', MEDIA_CSV),
170178
('.gif', MEDIA_GIF),
171179
('.htm', MEDIA_HTML),
172180
('.html', MEDIA_HTML),
@@ -175,6 +183,7 @@
175183
('.js', MEDIA_JS),
176184
('.json', MEDIA_JSON),
177185
('.mjs', MEDIA_JS),
186+
('.parquet', MEDIA_PARQUET),
178187
('.png', MEDIA_PNG),
179188
('.txt', MEDIA_TEXT),
180189
('.xml', MEDIA_XML),

0 commit comments

Comments
 (0)