on 459c430 help sign in
213 lines 7.2 kB history raw
1---
2navhome: /docs
3next: false
4sort: 9
5title: HTTP Requests and Timers
6---
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`:
17```
18:: Up-ness monitor. Accepts atom url, 'on', or 'off'
19::
20:::: /hoon/up/examples/app
21 ::
22/? 314
23|%
24++ move {bone card}
25++ card
26 $% {$hiss wire $~ $httr {$purl p/purl}}
27 {$wait wire @da}
28 ==
29++ action
30 $% {$on $~} :: enable polling('on')
31 {$off $~} :: disable polling('off)
32 {$target p/cord} :: set poll target('http://...')
33 ==
34--
35|_ {hid/bowl on/_| in-progress/_| target/@t}
36++ poke-cord
37 |= url-or-command/@t ^- (quip move +>)
38 =+ ^- act/action
39 ?: ?=($on url-or-command) [%on ~]
40 ?: ?=($off url-or-command) [%off ~]
41 [%target url-or-command]
42 ?- -.act
43 $target [~ +>.$(target p.act)]
44 $off [~ +>.$(on |)]
45 $on
46 :- ?: |(on in-progress) ~
47 [ost.hid %hiss /request ~ %httr %purl (need (epur target))]~
48 +>.$(on &, in-progress &)
49 ==
50++ sigh-httr
51 |= {wir/wire code/@ud headers/mess body/(unit octs)}
52 ~& 'arrive here'
53 ^- {(list move) _+>.$}
54 ?: &((gte code 200) (lth code 300))
55 ~& [%all-is-well code]
56 :_ +>.$
57 [ost.hid %wait /timer (add ~s10 now.hid)]~
58 ~& [%we-have-a-problem code]
59 ~& [%headers headers]
60 ~& [%body body]
61 :_ +>.$
62 [ost.hid %wait /timer (add ~s10 now.hid)]~
63++ wake-timer
64 |= {wir/wire $~} ^- (quip move +>)
65 ?: on
66 :_ +>.$
67 [ost.hid %hiss /request ~ %httr %purl (need (epur target))]~
68 [~ +>.$(in-progress |)]
69::
70++ prep ~& target _`. ::
71--
72```
74There's some fancy stuff going on here. We'll go through it line by
75line, though.
77Firstly, there's two kinds of cards we're sending to arvo. A
78`%them` card makes an HTTP requrest out of a unit `hiss`.
80> Recall that `++unit` means "maybe". Formally, "`(unit a)` is
81> either null or a pair of null and a value in `a`". Also recall
82> that to pull a value out of a unit `(u.unit)`, you must first
83> verify that the unit is not null (for example with `?~`).
85A `hiss` (all of the following terms are defined in `zuse`) is a
86pair of a `purl` and a `moth`. A `purl` is a parsed url
87structure, which can be created with `++epur`, which is a
88function that takes a url as text and parses it into a `(unit
89purl)`. Thus, the result is null if and only if the url is malformed.
91A `moth` is a treble of a `meth`, a `math`, and `(unit octs)`.
92`meth` is the HTTP method, in this case `%get`. `math` is a map
93of HTTP headers. The `(unit octs)` is a possible octet stream
94representing the body. If it's null, then no body is sent (as in
95this case).
97> An octect stream is a pair of the length in bytes of the data
98> plus the data itself. You can use `++taco` takes text and
99> turns it into an octet strem.
101When you send this request, you can expect a `%thou` with the
102response, which we handle later on in `++thou-request`.
104For `%wait`, you just pass a [`@da`]() (absolute date), and arvo will
105produce a `%wake` when the time comes.
107> A timer is guaranteed to not be triggered before the given
108> time, but it's currently impossible to guarantee the timer will be
109> triggered at exactly the requested time.
111Let's take a look at our state:
113```
114|_ {hid/bowl on/_| in-progress=_| target/@t}
115```
117We have three pieces of app-specific state. `target` is the url
118we're monitoring. `on` and `in-progress` are booleans
119representing, respectively, whether or not we're supposed to keep
120monitoring and whether or not we're in the middle of a request.
122The type of booleans is usually written as `?`, which is a union
123of `&` (true) and `|` false. This type defaults to `&` (true). In our
124case, we want both of these to default to `|` (false). Because
125of that, we use `_|` as the type. `_value` means "take the type
126of `value`, but make the default value be `value`". Thus, our
127type is still a boolean, just like `?`, but the default value is
128`|` (false).
130> This is the same `_` used in `_+>.$`, which means "the same
131> type as the value "+>.$". In other words, the same type as our
132> current context and state.
134Let's take a look at `++poke-cord`. When we're poked with a
135cord, we first check whether it's `'off'`. If so, we set `on` to
136false.
138If not, we check whether it's `on`. If so, we set `on` and
139`in-progress` to true. If it was already either on or in
140progress, then we don't take any other immediate action. If it
141was both off and not in progress, then we send an HTTP request.
142This request follows the pattern in `++hiss` well. `(need (epur
143target))` is the parsed url, `%get` is the HTTP method, `~`
144means no extra headers, and another `~` means no body.
146> Note the `~` at the end of the move. This is a convenient
147> shortcut for creating a list of a single element. It's part of
148> a small family of such shortcuts. `~[a b c]` is `[a b c ~]`,
149> `[a b c]~` is `[[a b c] ~]` and `\`[a b c]` is `[~ a b c]`.
150> These may be mixed and matched to create various convoluted
151> structures and emojis.
153If the argument is neither 'off' nor 'on', then we assume it's an
154actual url, so we save it in `target`.
156When the HTTP response comes back, we handle it with
157`++thou-request`, which, along with the wire the request was sent
158on, takes the status code, the response headers, and the response
159body.
161We check whether the status code is between 200 and 300. If so,
162all is well, and we print a message saying so. We start the
163timer for ten seconds after the present time.
165If we got a bad status code, then we print out the entire
166response and start the timer again.
168After ten seconds, arvo will give us a `%wake` event, which will
169be handled in `++wake-timer`. If we're still supposed to keep
170monitoring, we send the same HTTP request as before. Otherwise,
171we set `in-progress` to false.
173Let's try it out:
175```
176~fintud-macrep:dojo> |start %up
177>=
178~fintud-macrep:dojo> :examples-up &cord 'http://www.google.com'
179>=
180[%all-is-well 200]
181[%all-is-well 200]
182~fintud-macrep:dojo> :examples-up &cord 'http://example.com'
183>=
184[%all-is-well 200]
185~fintud-macrep:dojo> :examples-up &cord 'http://google.com'
186>=
187[%we-have-a-problem 301]
188[ %headers
189 ~[
190 [p='X-Frame-Options' q='SAMEORIGIN']
191 [p='X-XSS-Protection' q='1; mode=block']
192 [p='Content-Length' q='219']
193 [p='Server' q='gws']
194 [p='Cache-Control' q='public, max-age=2592000']
195 [p='Expires' q='Sun, 10 Jan 2016 23:45:05 GMT']
196 [p='Date' q='Fri, 11 Dec 2015 23:45:05 GMT']
197 [p='Content-Type' q='text/html; charset=UTF-8']
198 [p='Location' q='http://www.google.com/']
199 ]
201[ %body
202 [ ~
203 [ p=219
204 q
205 \/'<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">\0a<TITLE>301 Moved</TI\/
206 TLE></HEAD><BODY>\0a<H1>301 Moved</H1>\0aThe document has moved\0a<A HREF="http://www.google.com/">her
207 e</A>.\0d\0a</BODY></HTML>\0d\0a'
208 \/ \/
209 ]
210 ]
212~fintud-macrep:dojo> :up &cord 'off'
213```