Commit 8dbc046e authored by André Espaze's avatar André Espaze
Browse files

Refactoring to decodeResponse for Http.task

parent 737436220845
module Common exposing module Common exposing
( classes ( classes
, decodeJsonMessage
, decodeResponse
, expectJsonMessage , expectJsonMessage
, expectStringResponse , expectStringResponse
, maybe , maybe
...@@ -9,7 +11,7 @@ module Common exposing ...@@ -9,7 +11,7 @@ module Common exposing
import Dict import Dict
import Html.Styled exposing (Attribute) import Html.Styled exposing (Attribute)
import Html.Styled.Attributes exposing (class) import Html.Styled.Attributes exposing (class)
import Http import Http exposing (Response)
import Json.Decode as D import Json.Decode as D
...@@ -33,12 +35,16 @@ resultEither mapErr mapOk result = ...@@ -33,12 +35,16 @@ resultEither mapErr mapOk result =
mapOk b mapOk b
expectStringResponse : type alias ToMsg a msg =
(Result String a -> msg) Result String a -> msg
-> (String -> String)
-> D.Decoder a
-> Http.Expect msg type alias ReadError =
expectStringResponse toMsg readErr decoder = String -> String
decodeResponse : ReadError -> D.Decoder a -> Response String -> Result String a
decodeResponse readErr decoder resp =
let let
badStatus code body = badStatus code body =
"BadStatus " "BadStatus "
...@@ -46,36 +52,51 @@ expectStringResponse toMsg readErr decoder = ...@@ -46,36 +52,51 @@ expectStringResponse toMsg readErr decoder =
++ " : " ++ " : "
++ readErr body ++ readErr body
in in
Http.expectStringResponse toMsg <| case resp of
\resp -> Http.BadUrl_ x ->
case resp of Err <| "BadUrl : " ++ x
Http.BadUrl_ x ->
Err <| "BadUrl : " ++ x
Http.Timeout_ -> Http.Timeout_ ->
Err "Timeout" Err "Timeout"
Http.NetworkError_ -> Http.NetworkError_ ->
Err "NetworkError" Err "NetworkError"
Http.BadStatus_ metadata body -> Http.BadStatus_ metadata body ->
Err <| badStatus metadata.statusCode body Err <| badStatus metadata.statusCode body
Http.GoodStatus_ _ body -> Http.GoodStatus_ _ body ->
D.decodeString decoder body D.decodeString decoder body
|> Result.mapError D.errorToString |> Result.mapError D.errorToString
expectJsonMessage : (Result String a -> msg) -> D.Decoder a -> Http.Expect msg readErrorMessage : ReadError
expectJsonMessage toMsg = readErrorMessage =
let let
dictToStr =
Dict.toList
>> List.map (\( k, v ) -> "(" ++ k ++ ", " ++ v ++ ")")
>> String.join " "
getMessage x = getMessage x =
Dict.get "message" x Dict.get "message" x
|> Maybe.withDefault (Debug.toString x) |> Maybe.withDefault (dictToStr x)
readErr body =
D.decodeString (D.dict D.string) body
|> Result.map getMessage
|> resultEither D.errorToString identity
in in
expectStringResponse toMsg readErr D.decodeString (D.dict D.string)
>> Result.map getMessage
>> resultEither D.errorToString identity
decodeJsonMessage : D.Decoder a -> Response String -> Result String a
decodeJsonMessage =
decodeResponse readErrorMessage
expectStringResponse : ToMsg a msg -> ReadError -> D.Decoder a -> Http.Expect msg
expectStringResponse toMsg readErr decoder =
Http.expectStringResponse toMsg (decodeResponse readErr decoder)
expectJsonMessage : ToMsg a msg -> D.Decoder a -> Http.Expect msg
expectJsonMessage toMsg =
expectStringResponse toMsg readErrorMessage
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment