Created
September 4, 2016 15:32
-
-
Save magopian/66ad532bda717379b79fd54826a10e0d to your computer and use it in GitHub Desktop.
experimenting elm-http issue: https://github.com/evancz/elm-http/issues/40
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 Html | |
import Html.App | |
import Html.Events | |
import Http | |
import Json.Decode as Json exposing ((:=)) | |
import Task | |
url : String | |
url = "http://swapi.co/api/people/1/?format=json" | |
type alias Model = | |
{ content : String | |
} | |
init : (Model, Cmd Msg) | |
init = (Model "", Cmd.none) | |
type Msg | |
= Retrieve | |
| RetrieveSuccess String | |
| RetrieveFail Http.Error | |
update : Msg -> Model -> (Model, Cmd Msg) | |
update message model = | |
case message of | |
Retrieve -> | |
(model, getData url) | |
RetrieveSuccess content -> | |
({ model | content = content}, Cmd.none) | |
RetrieveFail _ -> | |
({ model | content = "error"}, Cmd.none) | |
sendRequest : String -> Platform.Task Http.RawError Http.Response | |
sendRequest url = | |
Http.send | |
Http.defaultSettings | |
{ verb = "GET" | |
, headers = [] | |
, url = url | |
, body = Http.empty | |
} | |
getData : String -> Cmd Msg | |
getData url = | |
Task.perform | |
RetrieveFail | |
RetrieveSuccess | |
(Http.fromJson ("name" := Json.string) (sendRequest url)) | |
view : Model -> Html.Html Msg | |
view model = | |
Html.div | |
[] | |
[ Html.text ("result: " ++ model.content) | |
, Html.br [] [] | |
, Html.button [ Html.Events.onClick Retrieve ] [ Html.text "Request data" ] | |
] | |
main = Html.App.program | |
{ init = init | |
, view = view | |
, update = update | |
, subscriptions = \_ -> Sub.none | |
} |
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 Html | |
import Html.App | |
import Html.Events | |
import Http | |
import Json.Decode as Json exposing ((:=)) | |
import Task | |
url : String | |
url = "http://swapi.co/api/people/1/?format=json" | |
type alias Model = | |
{ content : String | |
} | |
init : (Model, Cmd Msg) | |
init = (Model "", Cmd.none) | |
type Msg | |
= Retrieve | |
| RetrieveSuccess String | |
| RetrieveFail Http.Error | |
update : Msg -> Model -> (Model, Cmd Msg) | |
update message model = | |
case message of | |
Retrieve -> | |
(model, getData url) | |
RetrieveSuccess content -> | |
({ model | content = content}, Cmd.none) | |
RetrieveFail error -> | |
({ model | content = "error"}, Cmd.none) | |
getData : String -> Cmd Msg | |
getData url = | |
Task.perform RetrieveFail RetrieveSuccess (Http.getString url) | |
view : Model -> Html.Html Msg | |
view model = | |
Html.div | |
[] | |
[ Html.text ("result: " ++ model.content) | |
, Html.br [] [] | |
, Html.button [ Html.Events.onClick Retrieve ] [ Html.text "Request data" ] | |
] | |
main = Html.App.program | |
{ init = init | |
, view = view | |
, update = update | |
, subscriptions = \_ -> Sub.none | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment