Skip to content

Instantly share code, notes, and snippets.

@girishso
Last active November 21, 2018 08:44
Show Gist options
  • Save girishso/1e6b0cbeb300590bd61b057a0f9b4781 to your computer and use it in GitHub Desktop.
Save girishso/1e6b0cbeb300590bd61b057a0f9b4781 to your computer and use it in GitHub Desktop.
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