5title: HTTP Requests and Timers
8# Writing an HTTP request
10There's a variety of arvo services we haven't touched on yet.
11Let's figure out how to make HTTP requests and set timers.
13Let's build an uptime-monitoring system. It'll ping a web
14service periodically and print out an error when the response
15code isn't 2xx. Here's `app/up.hoon`:
18:: Up-ness monitor. Accepts atom url, 'on', or 'off'
20:::: /hoon/up/examples/app
26 $% {$hiss wire unit-iden/$~ mark/$httr cage/{mark/$purl vase/purl}}
30 $% {$on $~} :: enable polling('on')
31 {$off $~} :: disable polling('off)
32 {$target p/cord} :: set poll target('http://...')
35|_ {hid/bowl on/_| in-progress/_| target/@t}
37 |= url-or-command/@t ^- (quip move +>)
39 ?: ?=($on url-or-command) [%on ~]
40 ?: ?=($off url-or-command) [%off ~]
41 [%target url-or-command]
43 $target [~ +>.$(target p.act)]
46 :- ?: |(on in-progress) ~
47 [ost.hid %hiss /request ~ %httr %purl (need (epur target))]~
48 +>.$(on &, in-progress &)
51 |= {wir/wire code/@ud headers/mess body/(unit octs)}
53 ^- {(list move) _+>.$}
54 ?: &((gte code 200) (lth code 300))
55 ~& [%all-is-well code]
57 [ost.hid %wait /timer (add ~s10 now.hid)]~
58 ~& [%we-have-a-problem code]
62 [ost.hid %wait /timer (add ~s10 now.hid)]~
64 |= {wir/wire $~} ^- (quip move +>)
67 [ost.hid %hiss /request ~ %httr %purl (need (epur target))]~
68 [~ +>.$(in-progress |)]
70++ prep ~& target _`. :: computed when the source file changes;
71-- :: here it prints target then resets our state
74There's some fancy stuff going on here. We'll go through it line by
77There's two kinds of cards we're sending to arvo. A `%hiss` move tells
78`%eyre` to make an HTTP request. It expects a `(unit iden)`, which will
79be null here because we aren't doing any authentication; a mark, in this
80case the http request mark `&httr`; and some data in the form of a
81`cage`, which is a `{mark vase}`. This is confusing, so the mold in the
82code quoted above includes a bunch of otherwise unnecessary faces for
83the purpose of illustration. Compare: `{$hiss p/(unit iden) q/mark
86> You can grep zuse.hoon and arvo.hoon for most of these definitions.
87> However, beware the ambiguity surrounding "hiss": there's ++hiss in
88> section 3bI of `zuse` ("Arvo structures"), and there's `%hiss` the
89> move that is sent to %eyre to make our HTTP request happen. Here we're
90> talking about the latter. See `++kiss-eyre` in `zuse`.
92> Recall that `++unit` means "maybe". Formally, "`(unit a)` is
93> either null or a pair of null and a value in `a`". Also recall
94> that to pull a value out of a unit (`u.unit`), you must first
95> verify that the unit is not null (for example with `?~`). See
98A `purl` is a parsed url structure, which can be created with `++epur`,
99which is a function that takes a url as text and parses it into a `(unit
100purl)`. Thus, the result is null if and only if the url is malformed.
101'Purl' is also the mark which will be applied to this result.
103When you send this request, you can expect a `%sigh` with the
104response, which we handle later on in `++sigh-httr`.
106For `%wait`, you just pass a [`@da`]() (absolute date), and arvo will
107produce a `%wake` when the time comes.
109> A timer is guaranteed to not be triggered before the given
110> time, but it's currently impossible to guarantee the timer will be
111> triggered at exactly the requested time.
113Let's take a look at our state:
116|_ {hid/bowl on/_| in-progress=_| target/@t}
119We have three pieces of app-specific state. `target` is the url
120we're monitoring. `on` and `in-progress` are booleans
121representing, respectively, whether or not we're supposed to keep
122monitoring and whether or not we're in the middle of a request.
124The type of booleans is usually written as `?`, which is a union
125of `&` (true) and `|` false. This type defaults to `&` (true). In our
126case, we want both of these to default to `|` (false). Because
127of that, we use `_|` as the type. `_value` means "take the type
128of `value`, but make the default value be `value`". Thus, our
129type is still a boolean, just like `?`, but the default value is
132> This is the same `_` used in `_+>.$`, which means "the same
133> type as the value "+>.$". In other words, the same type as our
134> current context and state.
136Let's take a look at `++poke-atom`. When we're poked with an atom, we
137first check whether the atom is `'off'`. If so, we set our state
138variable `on` to false.
140If not, we check whether the atom is `'on'`. If so, we set `on` and
141`in-progress` to true. If it was already either on or in progress, then
142we don't take any other immediate action. If it was both off and not in
143progress, then we send an HTTP request.
145If the argument is neither 'off' nor 'on', then we assume it's an
146actual url, so we save it in `target`.
148Here's the move that sends the HTTP request:
150[ost.hid %hiss /request ~ %httr %purl (need (epur target))]
153> Remember, we are expected to produce a *list* of moves. Note the `~`
154> after the move in the full example. This is a convenient shortcut for
155> creating a list of a single element. It's part of a small family of
156> such shortcuts. `~[a b c]` is `[a b c ~]`, `[a b c]~` is `[[a b c]
157> ~]` and `\`[a b c]` is `[~ a b c]`. These may be mixed and matched to
158> create various convoluted structures and emojis.
160The correspondence between this move and `{bone card}` can be hard to
161visualize on one line. Here it is more pedantically:
164:* bone=ost.hid :: the move
169 cage=[%purl (need (epur target))]
173When the HTTP response comes back, we handle it with
174`++sigh-httr`, which, along with the wire the request was sent
175on, takes the status code, the response headers, and the response
178We check whether the status code is between 200 and 300. If so,
179all is well, and we print a message saying so. We start the
180timer for ten seconds after the present time.
182If we got a bad status code, then we print out the entire
183response and start the timer again.
185After ten seconds, arvo will give us a `%wake` event, which will
186be handled in `++wake-timer`. If we're still supposed to keep
187monitoring, we send the same HTTP request as before. Otherwise,
188we set `in-progress` to false.
193~fintud-macrep:dojo> |start %up
195~fintud-macrep:dojo> :examples-up &atom 'http://www.google.com'
199~fintud-macrep:dojo> :examples-up &atom 'http://example.com'
202~fintud-macrep:dojo> :examples-up &atom 'http://google.com'
204[%we-have-a-problem 301]
207 [p='X-Frame-Options' q='SAMEORIGIN']
208 [p='X-XSS-Protection' q='1; mode=block']
209 [p='Content-Length' q='219']
211 [p='Cache-Control' q='public, max-age=2592000']
212 [p='Expires' q='Sun, 10 Jan 2016 23:45:05 GMT']
213 [p='Date' q='Fri, 11 Dec 2015 23:45:05 GMT']
214 [p='Content-Type' q='text/html; charset=UTF-8']
215 [p='Location' q='http://www.google.com/']
222 \/'<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">\0a<TITLE>301 Moved</TI\/
223 TLE></HEAD><BODY>\0a<H1>301 Moved</H1>\0aThe document has moved\0a<A HREF="http://www.google.com/">her
224 e</A>.\0d\0a</BODY></HTML>\0d\0a'
229~fintud-macrep:dojo> :up &atom 'off'
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list