on c19ccfe help sign in
elk/libraries/Http.elm
43 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 Native.Http (send)
8import Signal (lift)
10-- The datatype for responses. Success contains only the returned message.
11-- Failures contain both an error code and an error message.
12data Response a = Success a | Waiting | Failure Int String
14type Request a = {
15 verb : String,
16 url : String,
17 body : a,
18 headers : [(String,String)]
19 }
21-- Create a customized request. Arguments are request type (get, post, put,
22-- delete, etc.), target url, data, and a list of additional headers.
23request : String -> String -> String -> [(String,String)] -> Request String
24request = Request
26-- Create a GET request to the given url.
27get : String -> Request String
28get url = Request "GET" url "" []
30-- Create a POST request to the given url, carrying the given data.
31post : String -> String -> Request String
32post url body = Request "POST" url body []
35-- Performs an HTTP request with the given requests. Produces a signal
36-- that carries the responses.
37send : Signal (Request a) -> Signal (Response String)
39-- Performs an HTTP GET request with the given urls. Produces a signal
40-- that carries the responses.
41sendGet : Signal String -> Signal (Response String)
42sendGet reqs = send (lift get reqs)