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,30 +154,25 @@
|= 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 =/ root=json (~(got by p.parsed-json) 'root')
?~ data %- pairs:enjs ~ ?> ?=(%o -.root)
=/ ujon=(unit json) (de-json:html q.data.u.data) =/ stations (~(got by p.root) 'stations')
?~ ujon %- pairs:enjs ~ ?> ?=(%o -.stations)
?> ?=(%o -.u.ujon) =/ station=json (~(got by p.stations) 'station')
=/ parsed-json u.ujon ?> ?=(%a -.station)
=/ root=json (~(got by p.parsed-json) 'root') =/ inner p.station
?> ?=(%o -.root) =/ abbr-and-name %- turn :- inner |= item=json
=/ stations (~(got by p.root) 'stations') ^- json
?> ?=(%o -.stations) ?> ?=(%o -.item)
=/ station=json (~(got by p.stations) 'station') =/ name (~(got by p.item) 'name')
?> ?=(%a -.station) ?> ?=(%s -.name)
=/ inner p.station =/ abbr (~(got by p.item) 'abbr')
=/ abbr-and-name %- turn :- inner |= item=json ?> ?=(%s -.abbr)
^- json (pairs:enjs [name+s+p.name abbr+s+p.abbr ~])
?> ?=(%o -.item) (pairs:enjs [[%stations %a abbr-and-name] ~])
=/ name (~(got by p.item) 'name') (with-json-handler response handler)
?> ?=(%s -.name)
=/ abbr (~(got by p.item) 'abbr')
?> ?=(%s -.abbr)
(pairs:enjs [name+s+p.name abbr+s+p.abbr ~])
(pairs:enjs [[%stations %a abbr-and-name] ~])
:: ::
++ bart-api-elevator-status ++ bart-api-elevator-status
^- request:http ^- request:http