2013-09-10 06:07:49 +00:00
|
|
|
{-| A library for asynchronous HTTP requests. See the
|
2013-09-10 03:14:58 +00:00
|
|
|
[WebSocket](http://elm-lang.org/docs/WebSocket.elm) library if
|
|
|
|
you have very strict latency requirements.
|
|
|
|
|
2013-09-10 06:07:49 +00:00
|
|
|
# Sending Requests
|
|
|
|
@docs send, sendGet
|
2013-09-10 03:14:58 +00:00
|
|
|
|
2013-09-10 06:07:49 +00:00
|
|
|
# Creating Requests
|
|
|
|
@docs get, post, request
|
|
|
|
|
|
|
|
# Responses
|
|
|
|
@docs Response
|
2013-09-10 03:14:58 +00:00
|
|
|
|
|
|
|
-}
|
2013-02-20 22:36:16 +00:00
|
|
|
|
2013-03-10 08:54:37 +00:00
|
|
|
module Http where
|
2013-02-20 22:36:16 +00:00
|
|
|
|
2013-07-29 13:48:35 +00:00
|
|
|
import open Basics
|
|
|
|
import open Signal
|
2013-07-29 21:23:04 +00:00
|
|
|
import Native.Http
|
2013-02-22 23:19:40 +00:00
|
|
|
|
2013-09-10 03:14:58 +00:00
|
|
|
{-| The datatype for responses. Success contains only the returned message.
|
|
|
|
Failures contain both an error code and an error message.
|
|
|
|
-}
|
2013-02-20 22:36:16 +00:00
|
|
|
data Response a = Success a | Waiting | Failure Int String
|
|
|
|
|
|
|
|
type Request a = {
|
|
|
|
verb : String,
|
|
|
|
url : String,
|
|
|
|
body : a,
|
|
|
|
headers : [(String,String)]
|
2013-03-10 08:54:37 +00:00
|
|
|
}
|
2013-02-20 22:36:16 +00:00
|
|
|
|
2013-09-10 03:14:58 +00:00
|
|
|
{-| Create a customized request. Arguments are request type (get, post, put,
|
|
|
|
delete, etc.), target url, data, and a list of additional headers.
|
|
|
|
-}
|
2013-02-20 22:36:16 +00:00
|
|
|
request : String -> String -> String -> [(String,String)] -> Request String
|
|
|
|
request = Request
|
|
|
|
|
2013-09-10 03:14:58 +00:00
|
|
|
{-| Create a GET request to the given url. -}
|
2013-02-20 22:36:16 +00:00
|
|
|
get : String -> Request String
|
|
|
|
get url = Request "GET" url "" []
|
|
|
|
|
2013-09-10 03:14:58 +00:00
|
|
|
{-| Create a POST request to the given url, carrying the given data. -}
|
2013-02-20 22:36:16 +00:00
|
|
|
post : String -> String -> Request String
|
|
|
|
post url body = Request "POST" url body []
|
|
|
|
|
2013-09-10 03:14:58 +00:00
|
|
|
{-| Performs an HTTP request with the given requests. Produces a signal
|
|
|
|
that carries the responses.
|
|
|
|
-}
|
2013-02-20 22:36:16 +00:00
|
|
|
send : Signal (Request a) -> Signal (Response String)
|
2013-07-29 21:23:04 +00:00
|
|
|
send = Native.Http.send
|
2013-02-20 22:36:16 +00:00
|
|
|
|
2013-09-10 03:14:58 +00:00
|
|
|
{-| Performs an HTTP GET request with the given urls. Produces a signal
|
|
|
|
that carries the responses.
|
|
|
|
-}
|
2013-02-20 22:36:16 +00:00
|
|
|
sendGet : Signal String -> Signal (Response String)
|
2013-03-10 08:54:37 +00:00
|
|
|
sendGet reqs = send (lift get reqs)
|