Skip to content

Instantly share code, notes, and snippets.

@fredcy
Last active December 10, 2015 20:57
Show Gist options
  • Save fredcy/c1ea4266469b2380ed32 to your computer and use it in GitHub Desktop.
Save fredcy/c1ea4266469b2380ed32 to your computer and use it in GitHub Desktop.
Example using Elm Http.get
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