on d0df030 help sign in
elk/libraries/Http.elm
57 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 Basics
18import open Signal
19import Native.Http
21{-| The datatype for responses. Success contains only the returned message.
22Failures contain both an error code and an error message.
23-}
24data Response a = Success a | Waiting | Failure Int String
26type Request a = {
27 verb : String,
28 url : String,
29 body : a,
30 headers : [(String,String)]
31 }
33{-| Create a customized request. Arguments are request type (get, post, put,
34delete, etc.), target url, data, and a list of additional headers.
35-}
36request : String -> String -> String -> [(String,String)] -> Request String
37request = Request
39{-| Create a GET request to the given url. -}
40get : String -> Request String
41get url = Request "GET" url "" []
43{-| Create a POST request to the given url, carrying the given data. -}
44post : String -> String -> Request String
45post url body = Request "POST" url body []
47{-| Performs an HTTP request with the given requests. Produces a signal
48that carries the responses.
49-}
50send : Signal (Request a) -> Signal (Response String)
51send = Native.Http.send
53{-| Performs an HTTP GET request with the given urls. Produces a signal
54that carries the responses.
55-}
56sendGet : Signal String -> Signal (Response String)
57sendGet reqs = send (lift get reqs)