on 63243ca help sign in
elk/libraries/Http.elm
56 lines 1.5 kB history raw
1module Http where
3{-| A library for asynchronous HTTP requests. See the
4[WebSocket](http://elm-lang.org/docs/WebSocket.elm) library if
5you have very strict latency requirements.
7# Sending Requests
8@docs send, sendGet
10# Creating Requests
11@docs get, post, request
13# Responses
14@docs Response
15-}
17import open Signal
18import Native.Http
20{-| The datatype for responses. Success contains only the returned message.
21Failures contain both an error code and an error message.
22-}
23data Response a = Success a | Waiting | Failure Int String
25type Request a = {
26 verb : String,
27 url : String,
28 body : a,
29 headers : [(String,String)]
30 }
32{-| Create a customized request. Arguments are request type (get, post, put,
33delete, etc.), target url, data, and a list of additional headers.
34-}
35request : String -> String -> String -> [(String,String)] -> Request String
36request = Request
38{-| Create a GET request to the given url. -}
39get : String -> Request String
40get url = Request "GET" url "" []
42{-| Create a POST request to the given url, carrying the given data. -}
43post : String -> String -> Request String
44post url body = Request "POST" url body []
46{-| Performs an HTTP request with the given requests. Produces a signal
47that carries the responses.
48-}
49send : Signal (Request a) -> Signal (Response String)
50send = Native.Http.send
52{-| Performs an HTTP GET request with the given urls. Produces a signal
53that carries the responses.
54-}
55sendGet : Signal String -> Signal (Response String)
56sendGet reqs = send (lift get reqs)