Use Accept header to determine output format
This commit is contained in:
parent
842468cf53
commit
4516614a8b
1 changed files with 19 additions and 5 deletions
20
server.ss
20
server.ss
|
@ -36,6 +36,9 @@
|
||||||
(display "No SNCF API authentication key found. Set the SNCF_AUTH_KEY environment variable.\n"
|
(display "No SNCF API authentication key found. Set the SNCF_AUTH_KEY environment variable.\n"
|
||||||
(current-error-port))
|
(current-error-port))
|
||||||
(exit 1))
|
(exit 1))
|
||||||
|
(def headers (http-request-headers req))
|
||||||
|
(def accept-header (assoc "Accept" headers))
|
||||||
|
(when accept-header (set! accept-header (cdr accept-header)))
|
||||||
(def params (parse-request-params (http-request-params req)))
|
(def params (parse-request-params (http-request-params req)))
|
||||||
(def station (assoc "station" params))
|
(def station (assoc "station" params))
|
||||||
(when station (set! station (cdr station)))
|
(when station (set! station (cdr station)))
|
||||||
|
@ -43,16 +46,27 @@
|
||||||
(def datetime (if datetime-str
|
(def datetime (if datetime-str
|
||||||
(string->date (cdr datetime-str) "~Y~m~dT~H~M~S")
|
(string->date (cdr datetime-str) "~Y~m~dT~H~M~S")
|
||||||
#f))
|
#f))
|
||||||
(def style (if (assoc "markdown" params) 'markdown 'unicode))
|
|
||||||
(define-values (station-name station-id)
|
(define-values (station-name station-id)
|
||||||
(if station
|
(if station
|
||||||
(get-station-id sncf-key station)
|
(get-station-id sncf-key station)
|
||||||
(values "Vernon - Giverny (Vernon)" "stop_area:SNCF:87415604")))
|
(values "Vernon - Giverny (Vernon)" "stop_area:SNCF:87415604")))
|
||||||
(define-values (departures disruptions) (get-departures sncf-key station-id datetime))
|
(define-values (departures disruptions) (get-departures sncf-key station-id datetime))
|
||||||
(http-response-write res 200 '(("Content-Type" . "text/plain"))
|
(def content
|
||||||
|
(cond
|
||||||
|
((or (assoc "markdown" params) (string-prefix? "text/markdown" accept-header))
|
||||||
(with-output-to-string
|
(with-output-to-string
|
||||||
(lambda () (display-all departures disruptions station-name datetime
|
(lambda () (display-all departures disruptions station-name datetime
|
||||||
style: style)))))
|
style: 'markdown))))
|
||||||
|
((string-prefix? "text/html" accept-header) ; TODO
|
||||||
|
(with-output-to-string
|
||||||
|
(lambda () (display-all departures disruptions station-name datetime
|
||||||
|
style: 'markdown))))
|
||||||
|
(#t
|
||||||
|
(with-output-to-string
|
||||||
|
(lambda () (display-all departures disruptions station-name datetime
|
||||||
|
style: 'unicode))))))
|
||||||
|
(http-response-write res 200 '(("Content-Type" . "text/plain; charset=utf-8"))
|
||||||
|
content))
|
||||||
|
|
||||||
(def (parse-request-params params)
|
(def (parse-request-params params)
|
||||||
(if params
|
(if params
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue