on 476e87f help sign in
elk/libraries/Http.elm
45 lines 1.4 kB history raw
1-- A library for asynchronous HTTP requests (AJAX). See the
2-- [WebSocket](http://elm-lang.org/docs/WebSocket.elm) library if
3-- you have very strict latency requirements.
5module Http where
7import open Basics
8import open Signal
9import Native.Http
11-- The datatype for responses. Success contains only the returned message.
12-- Failures contain both an error code and an error message.
13data Response a = Success a | Waiting | Failure Int String
15type Request a = {
16 verb : String,
17 url : String,
18 body : a,
19 headers : [(String,String)]
20 }
22-- Create a customized request. Arguments are request type (get, post, put,
23-- delete, etc.), target url, data, and a list of additional headers.
24request : String -> String -> String -> [(String,String)] -> Request String
25request = Request
27-- Create a GET request to the given url.
28get : String -> Request String
29get url = Request "GET" url "" []
31-- Create a POST request to the given url, carrying the given data.
32post : String -> String -> Request String
33post url body = Request "POST" url body []
36-- Performs an HTTP request with the given requests. Produces a signal
37-- that carries the responses.
38send : Signal (Request a) -> Signal (Response String)
39send = Native.Http.send
41-- Performs an HTTP GET request with the given urls. Produces a signal
42-- that carries the responses.
43sendGet : Signal String -> Signal (Response String)
44sendGet reqs = send (lift get reqs)