This post is in response to my previously posted question (which is now closed):
The solution to this post is great and taught me a deeper understanding of how commands work. I ran into an issue when I needed to chain multiple HTTP calls (login
and then getUser
with the token from login) and I did not see how to do that with the constructor pattern. I found the following article helpful:
https://korban.net/posts/elm/2019-02-15-combining-http-requests-with-task-in-elm/
I ended up with the following:
login : Config -> String -> String -> (Task Error User)
login config email password =
maybeLogin config email password
|> Task.mapError HttpError
|> Task.andThen
(\token -> createUser token config
|> Task.mapError HttpError
)
maybeLogin: Config -> String -> String -> (Task Http.Error Token)
maybeLogin config email password =
let
url = config.apiUrl ++ "/token"
body = "grant_type=password" ++
"&username=" ++ percentEncode email ++
"&password=" ++ percentEncode password
rememberHeaders = getRememberHeaders config.setCookie
in
Http.task { url = url
, method = "POST"
, body = Http.stringBody "application/x-www-form-urlencoded" body
, headers = config.defaultHeaders ++ rememberHeaders
, resolver= (Http.stringResolver <| handleJsonResponse <| tokenDecoder)
, timeout = Nothing
}