Merge: lib/popcorn: add response handler to return csv documents
authorJean Privat <jean@pryen.org>
Mon, 20 Feb 2017 14:20:34 +0000 (09:20 -0500)
committerJean Privat <jean@pryen.org>
Mon, 20 Feb 2017 14:20:34 +0000 (09:20 -0500)
Pull-Request: #2370
Reviewed-by: Jean Privat <jean@pryen.org>

lib/nitcorn/media_types.nit
lib/popcorn/pop_handlers.nit

index 305f314..49b6daa 100644 (file)
@@ -37,6 +37,7 @@ class MediaTypes
                types["htm"]        = "text/html"
                types["shtml"]      = "text/html"
                types["css"]        = "text/css"
+               types["csv"]        = "text/csv"
                types["xml"]        = "text/xml"
                types["rss"]        = "text/xml"
                types["gif"]        = "image/gif"
@@ -50,6 +51,7 @@ class MediaTypes
                types["ico"]        = "image/x-icon"
                types["jng"]        = "image/x-jng"
                types["wbmp"]       = "image/vnd.wap.wbmp"
+               types["gz"]         = "application/gzip"
                types["jar"]        = "application/java-archive"
                types["war"]        = "application/java-archive"
                types["ear"]        = "application/java-archive"
index 8d66cc9..f55126f 100644 (file)
@@ -20,6 +20,7 @@ module pop_handlers
 import pop_routes
 import json::static
 import json
+import csv
 
 # Class handler for a route.
 #
@@ -459,6 +460,16 @@ redef class HttpResponse
                end
        end
 
+       # Write data as CSV and set the right content type header.
+       fun csv(csv: nullable CsvDocument, status: nullable Int) do
+               header["Content-Type"] = media_types["csv"].as(not null)
+               if csv == null then
+                       send(null, status)
+               else
+                       send(csv.write_to_string, status)
+               end
+       end
+
        # Write error as JSON and set the right content type header.
        fun json_error(error: nullable Jsonable, status: nullable Int) do
                json(error, status)