Last active
December 10, 2015 20:57
-
-
Save fredcy/c1ea4266469b2380ed32 to your computer and use it in GitHub Desktop.
Example using Elm Http.get
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Effects | |
import Html exposing (..) | |
import Http | |
import StartApp | |
import Task | |
app : StartApp.App String | |
app = | |
StartApp.start | |
{ init = ("default", Effects.task get) | |
, inputs = [] | |
, update = \action model -> (action, Effects.none) | |
, view = \addr t -> div [] [ text t ] | |
} | |
{-| Task that gets a fixed URL via HTTP and returns the result as a String. | |
-} | |
get : Task.Task Effects.Never String | |
get = | |
let | |
getTask = Http.getString "http://httpbin.org/get" | |
handleError e = Task.succeed (stringFromHttpError e) | |
in | |
Task.onError getTask handleError | |
stringFromHttpError : Http.Error -> String | |
stringFromHttpError e = | |
case e of | |
Http.Timeout -> "Timeout" | |
Http.NetworkError -> "Network Error" | |
Http.UnexpectedPayload msg -> "Unexpected Payload: " ++ msg | |
Http.BadResponse code msg -> "Bad Reponse: " ++ (toString code) ++ " " ++ msg | |
main : Signal Html | |
main = app.html | |
port tasks : Signal (Task.Task Effects.Never ()) | |
port tasks = | |
app.tasks |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment