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