elm-requests makes complex HTTP requests simpler:
In Elm, a typical request will be
import Http
Http.get
{ url = "http://deep-thought.com/api/v1/questions/ultimate"
, expect = Http.expectJson ReceivedAnswer D.int
}Requests works similarly
import Request
Request.get
{ url = "http://deep-thought.com/api/v1/questions/ultimate"
, expect = Request.expectJson D.int
}Notice that the message is not part of the request. Request.get also do not
return a Cmd msg, but rather a Request data object. We can execute it
immediately by calling the function
Request.cmd simple ReceivedAnswer request
==> Cmdand obtain the same behavior as in Elm's native Http.get.
This little indirection has a huge payoff. We can now create request objects more easily using a pipeline interface. They can also be easily converted to tasks which allows chaining, batch execution, and more.
For instance, we can chain two http requests so our application only need to handle the final response:
getAnswer : Request Int
getAnswer = Request.get
{ url = "http://deep-thought.com/api/v1/questions/ultimate"
, expect = Request.expectJson D.int
}
getQuestion : Int -> Request String
getQuestion answer = Request.get
{ url = "http://deep-thought.com/api/v1/answers/" ++ String.fromInt
, expect = Request.expectJson D.string
}
Request.task simple getAnswer
|> Task.andThen
(\answer ->
Request.task simple getQuestion
)
|> Task.attempt QuestionReceived