shrub/lib/connector.hoon

169 lines
5.4 KiB
Plaintext
Raw Normal View History

2016-04-09 02:47:34 +03:00
:: This is a library for writing API connectors.
::
:: The basic flow is as follows:
:: -- define a list of `++place`s, which specify the exported
:: interface.
2016-04-09 02:47:34 +03:00
:: -- in `++peer-scry` in the connector app, call `++read` in
:: this library to match to the appropriate place and
:: produce a move (usually either an immediate response or
:: an http request to the api).
:: -- in `++sigh-httr` in the connector app, call `++sigh` in
:: this library to handle the response according to the
:: place.
2016-04-08 22:47:11 +03:00
|* {move/mold sub-result/mold}
=> |%
2016-04-09 02:47:34 +03:00
:: A place consists of:
2016-04-20 19:47:30 +03:00
:: -- `guard`, the type of the paths we should match. For
2016-04-09 02:47:34 +03:00
:: example, to match `/issues/<user>/<repo>` use
2018-03-19 07:18:20 +03:00
:: `{$issues @t @t ~}`.
2016-04-09 02:47:34 +03:00
:: -- `read-x`, called when someone tries to read the
:: place with care `%x`. Should produce a single move,
:: usually either a `%diff` response if we can
:: immediately answer or a `%hiss` http request if we
:: need to make a request to the api. See the
:: `++read-*` functions in `++helpers` for some common
:: handlers.
:: -- `read-y`, same as `read-x` except with care `%y`.
:: -- `sigh-x`, called when an http response comes back on
:: this place. You're given the json of the result, and
:: you should produce either a result or null. Null
:: represents an error. If you didn't create an http
:: request in `read-x`, then this should never be
:: called. Use `++sigh-strange` from `++helpers` to
:: unconditionally signal an error.
:: -- `sigh-y`, same as `sigh-x` except with care `%y`.
:: Note that a `%y` request must produce an arch, unlike
:: a `%x` request, which may produce data of any mark.
::
2016-04-08 22:47:11 +03:00
++ place
$: guard/mold
read-x/$-(path move)
read-y/$-(path move)
sigh-x/$-(jon/json (unit sub-result))
sigh-y/$-(jon/json (unit arch))
==
--
|%
:: Generic helpers for place definitions
::
++ helpers
2016-04-09 02:47:34 +03:00
|= {ost/bone wir/wire api-url/tape}
2016-04-08 22:47:11 +03:00
|%
2016-04-09 02:47:34 +03:00
:: Produce null. Used as `++read-x` in places which are pure
:: directories. `++sigh-x` should be `++sigh-strange`.
::
2016-04-08 22:47:11 +03:00
++ read-null |=(pax/path [ost %diff %null ~])
2016-04-09 02:47:34 +03:00
::
:: Produce an arch with the given list of children. Used as
:: `++read-y` in places which have a static list of (known)
:: children rather than having to ask the api. `++sigh-y`
:: should be `++sigh-strange`.
::
2016-04-08 22:47:11 +03:00
++ read-static
|= children/(list @t)
|= pax/path
[ost %diff %arch ~ (malt (turn children |=(@t [+< ~])))]
::
2016-04-09 02:47:34 +03:00
:: Produce an api request to the given path. Use this if the
:: endpoint is static. If the endpoint depends on parameters
:: in the path, use `++get`. For example:
:: `|=(pax/path (get /users/[+<.pax]/repos))`.
::
2016-04-08 22:47:11 +03:00
++ read-get
|= endpoint/path
|= pax/path
(get endpoint)
::
2016-04-09 02:47:34 +03:00
:: Make an api request to the specified endpoint.
2016-04-08 22:47:11 +03:00
::
++ get
|= endpoint/path
^- move
:* ost %hiss wir `~ %httr %hiss
(endpoint-to-purl endpoint) %get ~ ~
==
::
2016-04-09 02:47:34 +03:00
:: Convert an endpoint path to a purl.
::
2016-04-08 22:47:11 +03:00
++ endpoint-to-purl
|= endpoint/path
(scan (weld api-url <`path`endpoint>) auri:de-purl:html)
2016-04-09 02:47:34 +03:00
::
:: Return error. Used when no http response is expected.
::
++ sigh-strange |=(jon/json ~)
2016-04-08 22:47:11 +03:00
--
::
2016-04-09 02:47:34 +03:00
:: Handles one-time requests by mapping them to their handling,
:: either `read-x` or `read-y`, in `places`.
2016-04-08 22:47:11 +03:00
::
++ read
|= {ost/bone places/(list place) ren/care:clay pax/path}
2016-04-08 22:47:11 +03:00
^- move
?~ places
~& [%strange-path pax]
(move [ost %diff ?+(ren !! $x null+~, $y arch+*arch)])
=+ match=((soft guard.i.places) pax)
?~ match
$(places t.places)
(?+(ren !! $x read-x.i.places, $y read-y.i.places) pax)
::
2017-10-20 03:46:34 +03:00
:: Handles http responses sent in `++read` by mapping them to
2016-04-09 02:47:34 +03:00
:: their handling, either `sigh-x` or `sigh-y`, in `places`.
::
2016-04-08 22:47:11 +03:00
++ sigh
=, html
2017-10-20 02:43:37 +03:00
=, eyre
|= {places/(list place) ren/care:clay pax/path res/httr:eyre}
2016-04-08 22:47:11 +03:00
^- sub-result
=< ?+(ren ~|([%invalid-care ren] !!) $x sigh-x, $y sigh-y)
|%
++ sigh-x
?~ r.res
2016-04-09 02:47:34 +03:00
~& [err+%empty-response code+p.res]
null+~
=+ jon=(rush q.u.r.res apex:de-json)
2016-04-08 22:47:11 +03:00
?~ jon
2016-04-09 02:47:34 +03:00
~& [err+%bad-json code+p.res body+q.u.r.res]
null+~
2016-04-08 22:47:11 +03:00
?. =(2 (div p.res 100))
2016-04-09 02:47:34 +03:00
~& [err+%request-rejected code+p.res msg+u.jon]
null+~
2016-04-08 22:47:11 +03:00
|- ^- sub-result
?~ places
~&([%sigh-strange-path pax] (sub-result null+~))
=+ match=((soft guard.i.places) pax)
?~ match
$(places t.places)
=+ (sigh-x.i.places u.jon)
?~ -
~& [err+s+%response-not-valid pax+pax code+(numb:enjs:format p.res) msg+u.jon]
2016-04-08 22:47:11 +03:00
(sub-result null+~)
u.-
::
++ sigh-y
?~ r.res
~& [err+s+%empty-response code+(numb:enjs:format p.res)]
2016-04-08 22:47:11 +03:00
arch+*arch
=+ jon=(rush q.u.r.res apex:de-json)
2016-04-08 22:47:11 +03:00
?~ jon
~& [err+s+%bad-json code+(numb:enjs:format p.res) body+s+q.u.r.res]
2016-04-08 22:47:11 +03:00
arch+*arch
?. =(2 (div p.res 100))
~& [err+s+%request-rejected code+(numb:enjs:format p.res) msg+u.jon]
2016-04-08 22:47:11 +03:00
arch+*arch
%- sub-result
|- ^- {$arch arch}
?~ places
~&([%sigh-strange-path pax] arch+*arch)
=+ match=((soft guard.i.places) pax)
?~ match
$(places t.places)
=+ (sigh-y.i.places u.jon)
?~ -
~& [err+s+%response-not-valid pax+pax code+(numb:enjs:format p.res) msg+u.jon]
2016-04-08 22:47:11 +03:00
arch+*arch
arch+u.-
--
--