on db72bdb help sign in
elk/libraries/Http.elm
58 lines 1.5 kB history raw
1{-| A library for asynchronous HTTP requests. See the
2[WebSocket](http://elm-lang.org/docs/WebSocket.elm) library if
3you 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
14-}
16module Http where
18import open Basics
19import open Signal
20import Native.Http
22{-| The datatype for responses. Success contains only the returned message.
23Failures contain both an error code and an error message.
24-}
25data Response a = Success a | Waiting | Failure Int String
27type Request a = {
28 verb : String,
29 url : String,
30 body : a,
31 headers : [(String,String)]
32 }
34{-| Create a customized request. Arguments are request type (get, post, put,
35delete, etc.), target url, data, and a list of additional headers.
36-}
37request : String -> String -> String -> [(String,String)] -> Request String
38request = Request
40{-| Create a GET request to the given url. -}
41get : String -> Request String
42get url = Request "GET" url "" []
44{-| Create a POST request to the given url, carrying the given data. -}
45post : String -> String -> Request String
46post url body = Request "POST" url body []
48{-| Performs an HTTP request with the given requests. Produces a signal
49that carries the responses.
50-}
51send : Signal (Request a) -> Signal (Response String)
52send = Native.Http.send
54{-| Performs an HTTP GET request with the given urls. Produces a signal
55that carries the responses.
56-}
57sendGet : Signal String -> Signal (Response String)
58sendGet reqs = send (lift get reqs)