Last active
November 21, 2018 08:44
-
-
Save girishso/1e6b0cbeb300590bd61b057a0f9b4781 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
type Msg | |
= GotRepos (Result Http.Error RepoResponse) | |
type alias RepoResponse = | |
( List LinkHeader.WebLink, List Repo ) | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment