Abstract away a handler

This commit is contained in:
ronreg-ribdev 2020-06-01 01:07:07 -07:00
parent 9f2550edc7
commit 8ce9df1ec8

View File

@ -131,6 +131,19 @@
:: get .root | .stations | .station for list of stations
++ bart-api-key "Q5RQ-PUEB-999T-DWEI"
++ bart-api-url-base "http://api.bart.gov/api"
++ with-json-handler
|= [response=client-response:iris jsonhandler=$-(json json)]
^- json
=, format
?. ?=(%finished -.response)
%- pairs:enjs [fulltext+s+'bart response error' ~]
=/ data=(unit mime-data:iris) full-file.response
?~ data %- pairs:enjs ~
=/ ujon=(unit json) (de-json:html q.data.u.data)
?~ ujon %- pairs:enjs ~
?> ?=(%o -.u.ujon)
=/ parsed-json=json u.ujon
(jsonhandler parsed-json)
++ bart-api-request-stations
^- request:http
=/ url (crip "{bart-api-url-base}/stn.aspx?cmd=stns&key={bart-api-key}&json=y")
@ -141,14 +154,8 @@
|= response=client-response:iris
^- json
=, format
?. ?=(%finished -.response)
%- pairs:enjs [fulltext+s+'bart response error' ~]
=/ data=(unit mime-data:iris) full-file.response
?~ data %- pairs:enjs ~
=/ ujon=(unit json) (de-json:html q.data.u.data)
?~ ujon %- pairs:enjs ~
?> ?=(%o -.u.ujon)
=/ parsed-json u.ujon
=/ handler |= parsed-json=json
?> ?=(%o -.parsed-json)
=/ root=json (~(got by p.parsed-json) 'root')
?> ?=(%o -.root)
=/ stations (~(got by p.root) 'stations')
@ -165,6 +172,7 @@
?> ?=(%s -.abbr)
(pairs:enjs [name+s+p.name abbr+s+p.abbr ~])
(pairs:enjs [[%stations %a abbr-and-name] ~])
(with-json-handler response handler)
::
++ bart-api-elevator-status
^- request:http