Skip to content

Instantly share code, notes, and snippets.

@grimrose
Created February 24, 2018 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grimrose/6325622985de0c3ceabb8d2678ce08e6 to your computer and use it in GitHub Desktop.
Save grimrose/6325622985de0c3ceabb8d2678ce08e6 to your computer and use it in GitHub Desktop.
Elmなんか作ろう会 #6 https://elm-jp.connpass.com/event/79821/ #moku2_elm
module Main exposing (..)
import Html exposing (Html, div, h1, h2, img, p, text)
--import Html.Attributes exposing (src)
import Http exposing (Request)
import Json.Decode as Decode
--import Json.Encode as Encode
---- MODEL ----
type alias UserId = String
type alias QiitaUser =
{
id: UserId
}
type alias Model =
{
id: UserId
}
init : ( Model, Cmd Msg )
init =
({ id = "ここにidが入るよ" }, getQiitaUser)
---- UPDATE ----
type Msg
= GetQiitaUserResponse (Result Http.Error QiitaUser)
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
GetQiitaUserResponse res ->
let
usr = case res of
Ok u -> u
Err _ -> Debug.crash "http error"
id = usr.id
in
( { model | id = id }, Cmd.none )
decodeQiitaUser : Decode.Decoder QiitaUser
decodeQiitaUser =
Decode.map QiitaUser
(Decode.at ["id"] Decode.string)
qiitaApiV2Endpoint: String
qiitaApiV2Endpoint = "https://qiita.com/api/v2"
getQiitaUser: Cmd Msg
getQiitaUser =
let
url = qiitaApiV2Endpoint ++ "/users/grimrose@github"
in
Http.send GetQiitaUserResponse <| (Http.get url decodeQiitaUser)
---- VIEW ----
view : Model -> Html Msg
view model =
div []
[
h1 [] [ text (toString model) ]
]
---- PROGRAM ----
main : Program Never Model Msg
main =
Html.program
{ view = view
, init = init
, update = update
, subscriptions = always Sub.none
}
{--
-- HTTP Reqeust (認証なし)
-- http://package.elm-lang.org/packages/elm-lang/core/5.1.1/Json-Decode
decodeHoge : Decode.Decoder Hoge
decodeHoge =
Decode.map3 Hoge
(Decode.at [ "id" ] Decode.int)
(Decode.at [ "name" ] Decode.string)
(Decode.at [ "job" ] Decode.string)
decodeHogeList : Decode.Decoder (List Hoge)
decodeHogeList =
Decode.list decodeHoge
getHoge =
let
url = qiitaDomain ++ "/users/ababup1192"
in
Http.send msg <| (Http.get url decodeHogeList)
qiitaDomain =
"https://qiita.com/api/v2/"
authHeader =
Http.header "Authorization"
"Bearer YOUR_TOKEN"
-- HTTP Request (認証あり)
getAuthHoge : Cmd Msg
getAuthHoge =
let
url =
qiitaDomain ++ "/users/ababup1192"
-- POSTする必要がある場合には、jsonを生成してください。
-- jsonBody =
-- Http.jsonBody <| json
request =
Http.request
{ method = "GET"
, headers = [ authHeader ]
, url = url
-- , body = jsonBody
, expect = Http.expectStringResponse (\_ -> Ok ())
, timeout = Nothing
, withCredentials = False
}
in
-- http://package.elm-lang.org/packages/elm-lang/http/1.0.0/Http#send
-- http://package.elm-lang.org/packages/elm-lang/core/latest/Result
-- Cmd Msg (Result Http.Error String)
Http.send msg request
--}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment