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