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