diff options
author | Joris | 2017-03-24 09:21:06 +0000 |
---|---|---|
committer | Joris | 2017-03-24 09:21:06 +0000 |
commit | c0ac16a713c4e53cf6af8e72a6d5f6b8ac5d6456 (patch) | |
tree | 8a438430cee7411259fc395d8f3898488e85d750 /src/client/elm/Utils/Http.elm | |
parent | 293eb8295162bf0a038f488237db9c9d1316c04d (diff) | |
parent | cfca18262c1ff48dcb683ddab7d03cf8e55573ff (diff) | |
download | budget-c0ac16a713c4e53cf6af8e72a6d5f6b8ac5d6456.tar.gz budget-c0ac16a713c4e53cf6af8e72a6d5f6b8ac5d6456.tar.bz2 budget-c0ac16a713c4e53cf6af8e72a6d5f6b8ac5d6456.zip |
Merge branch 'features/categories' into 'master'
Features/categories
See merge request !1
Diffstat (limited to 'src/client/elm/Utils/Http.elm')
-rw-r--r-- | src/client/elm/Utils/Http.elm | 80 |
1 files changed, 25 insertions, 55 deletions
diff --git a/src/client/elm/Utils/Http.elm b/src/client/elm/Utils/Http.elm index 4edc233..dd3870a 100644 --- a/src/client/elm/Utils/Http.elm +++ b/src/client/elm/Utils/Http.elm @@ -1,69 +1,39 @@ module Utils.Http exposing ( jsonRequest , request - , requestWithBody - , decodeHttpValue , errorKey ) import Http exposing (..) import Task exposing (..) -import Json.Decode as JsonDecode exposing (Decoder) -import Json.Encode as JsonEncode - -jsonRequest : String -> String -> JsonEncode.Value -> Task Error Value -jsonRequest method url json = - json - |> JsonEncode.encode 0 - |> Http.string - |> requestWithBody method url - -request : String -> String -> Task Error Value -request method url = requestWithBody method url empty - -requestWithBody : String -> String -> Body -> Task Error Value -requestWithBody method url body = - { verb = method - , headers = [] - , url = url - , body = body - } - |> Http.send defaultSettings - |> mapError promoteError - |> flip Task.andThen handleResponse - -promoteError : RawError -> Error -promoteError rawError = - case rawError of - RawTimeout -> Timeout - RawNetworkError -> NetworkError - -handleResponse : Response -> Task Error Value -handleResponse response = - if 200 <= response.status && response.status < 300 - then Task.succeed response.value - else fail (BadResponse response.status (responseString response.value)) - -responseString : Value -> String -responseString value = - case value of - Text str -> str - _ -> "" - -decodeHttpValue : Decoder a -> Value -> Task Error a -decodeHttpValue decoder value = - case value of - Text str -> - case JsonDecode.decodeString decoder str of - Ok v -> succeed v - Err msg -> fail (UnexpectedPayload msg) - _ -> - fail (UnexpectedPayload "Response body is a blob, expecting a string.") +import Json.Decode as Decode exposing (Decoder, Value) +import Json.Encode as Encode + +jsonRequest : String -> String -> Expect a -> (Result Error a -> msg) -> Encode.Value -> Cmd msg +jsonRequest method url expect handleResult value = + requestWithBody method url (jsonBody value) expect handleResult + +request : String -> String -> Expect a -> (Result Error a -> msg) -> Cmd msg +request method url = requestWithBody method url emptyBody + +requestWithBody : String -> String -> Body -> Expect a -> (Result Error a -> msg) -> Cmd msg +requestWithBody method url body expect handleResult = + let req = Http.request + { method = method + , headers = [] + , url = url + , body = body + , expect = expect + , timeout = Nothing + , withCredentials = False + } + in send handleResult req errorKey : Error -> String errorKey error = case error of + BadUrl _ -> "BadUrl" Timeout -> "Timeout" NetworkError -> "NetworkError" - UnexpectedPayload _ -> "UnexpectedPayload" - BadResponse _ key -> key + BadPayload _ _ -> "BadPayload" + BadStatus response -> response.body |