csv/response.ss
#lang scheme/base

(require scheme/contract
         web-server/servlet
         "../plain/util.ss"
         "render.ss"
         "struct.ss")

;;  [#:code      integer]
;;  [#:message   string]
;;  [#:seconds   integer]
;;  [#:mime-type (U bytes string)]
;;  [#:headers   (listof header)]
;;  csv
;; ->
;;  response
(define (make-csv-response
         #:code      [code      200]
         #:message   [message   "OK"]
         #:seconds   [seconds   (current-seconds)]
         #:mime-type [mime-type #"text/csv; charset=utf-8"]
         #:headers   [headers   no-cache-http-headers]
         content)
  (let ([mime-type (if (bytes? mime-type)
                       mime-type
                       (string->bytes/utf-8 mime-type))])
    (make-response/full code message seconds mime-type headers (list (csv->string content)))))

; Provide statements -----------------------------

(provide/contract
 [make-csv-response (->* (csv?)
                          (#:code integer? 
                                  #:message   string?
                                  #:seconds   integer?
                                  #:mime-type (or/c bytes? string?)
                                  #:headers   (listof header?))
                          response?)])