Skip to content

Instantly share code, notes, and snippets.

@fswalker
Last active February 17, 2018 10:57
Show Gist options
  • Save fswalker/e9a87b544ef8fbbbb646ab165ff95d22 to your computer and use it in GitHub Desktop.
Save fswalker/e9a87b544ef8fbbbb646ab165ff95d22 to your computer and use it in GitHub Desktop.
Simple example which shows usage of Elm's Http module to get some string response from GitHub api. Ellie link: https://ellie-app.com/bPZKsNPCa1/0
module Main exposing (main)
import Html as Html exposing (..)
import Http as Http
type alias Model = String
type Msg =
UpdateLicenses (Result Http.Error String)
main : Program Never Model Msg
main =
Html.program
{ init = ("loading licenses...", getGithubLicenses ())
, update = update
, view = view
, subscriptions = \_ -> Sub.none
}
update : Msg -> Model -> (Model, Cmd msg)
update msg model =
case msg of
UpdateLicenses (Ok json) ->
( json, Cmd.none)
UpdateLicenses (Err e) ->
( "error ocurred", Cmd.none )
view : Model -> Html msg
view model =
text model
getGithubLicenses : () -> Cmd Msg
getGithubLicenses () =
let
request =
Http.getString "https://api.github.com/licenses"
in
Http.send UpdateLicenses request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment