Created
November 21, 2018 08:49
-
-
Save girishso/6343a18af49a142d37b59bc92d61e09c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
module Main exposing (Model, Msg(..), extractRepoResponse, fetchRepos, getRepos, init, main, update, view) | |
import Browser | |
import Dict | |
import Html exposing (..) | |
import Html.Attributes exposing (..) | |
import Html.Events exposing (..) | |
import Http | |
import Json.Decode as Decode | |
import Json.Decode.Pipeline as Pipeline | |
import LinkHeader | |
main = | |
Browser.element | |
{ init = \() -> init | |
, view = view | |
, update = update | |
, subscriptions = \_ -> Sub.none | |
} | |
type alias Model = | |
{ value : String | |
} | |
init : ( Model, Cmd Msg ) | |
init = | |
( Model "waiting..." | |
, fetchRepos | |
) | |
type Msg | |
= GotRepos (Result Http.Error RepoResponse) | |
type alias RepoResponse = | |
( List LinkHeader.WebLink, List Repo ) | |
update : Msg -> Model -> ( Model, Cmd Msg ) | |
update msg model = | |
case msg of | |
GotRepos (Ok ( header, repos )) -> | |
( Model ("Link headers: " ++ Debug.toString header ++ " ---- " ++ Debug.toString repos), Cmd.none ) | |
GotRepos (Err err) -> | |
( { model | value = Debug.toString err }, Cmd.none ) | |
view : Model -> Html Msg | |
view model = | |
div [] | |
[ text model.value | |
] | |
fetchRepos : Cmd Msg | |
fetchRepos = | |
Http.send GotRepos (getRepos "https://api.github.com/users/girishso/repos") | |
getRepos : String -> Http.Request RepoResponse | |
getRepos url = | |
Http.request | |
{ method = "GET" | |
, headers = [] | |
, url = url | |
, body = Http.emptyBody | |
, expect = Http.expectStringResponse extractRepoResponse | |
, timeout = Nothing | |
, withCredentials = False | |
} | |
extractRepoResponse : Http.Response String -> Result String RepoResponse | |
extractRepoResponse resp = | |
let | |
headers = | |
Dict.get "link" resp.headers | |
|> Maybe.map LinkHeader.parse | |
|> Maybe.withDefault [] | |
repos = | |
Decode.decodeString decodeRepos resp.body | |
|> Result.mapError Decode.errorToString | |
in | |
Result.map (\x -> ( headers, x )) repos | |
type alias Repo = | |
{ id : Int | |
, full_name : String | |
, description : String | |
, html_url : String | |
, stargazers_count : Int | |
, language : String | |
} | |
decodeRepos : Decode.Decoder (List Repo) | |
decodeRepos = | |
Decode.list decodeRepo | |
decodeRepo : Decode.Decoder Repo | |
decodeRepo = | |
Decode.succeed Repo | |
|> Pipeline.required "id" Decode.int | |
|> Pipeline.required "full_name" Decode.string | |
|> Pipeline.optional "description" Decode.string "" | |
|> Pipeline.required "html_url" Decode.string | |
|> Pipeline.required "stargazers_count" Decode.int | |
|> Pipeline.optional "language" Decode.string "<unknown>" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment