This file contains 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 alias Metadata = | |
{ url : String | |
, statusCode : Int | |
, statusText : String | |
, headers : Dict String String | |
} | |
This file contains 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
convertResponseString : Response String -> Result Error ( Metadata, String ) | |
convertResponseString httpResponse = | |
case httpResponse of | |
Http.BadUrl_ url -> | |
Err (Http.BadUrl url) | |
Http.Timeout_ -> | |
Err Http.Timeout | |
Http.NetworkError_ -> |
This file contains 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
expectStringDetailed : (Result Error ( Metadata, String ) -> msg) -> Expect msg | |
expectStringDetailed msg = | |
Http.expectStringResponse msg convertResponseString | |
This file contains 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 Error | |
= BadUrl String | |
| Timeout | |
| NetworkError | |
| BadStatus Int | |
| BadBody String | |
This file contains 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
-- Change this to return Result ErrorDetailed ( Http.Metadata, String ) instead of Result Http.Error ( Http.Metadata, String ) | |
convertResponseString : Response String -> Result ErrorDetailed ( Metadata, String ) | |
convertResponseString httpResponse = | |
case httpResponse of | |
Http.BadUrl_ url -> | |
Err (BadUrl url) | |
Http.Timeout_ -> | |
Err Timeout |
This file contains 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
module Main exposing (main) | |
{- | |
This example demonstrates how to send an HTTP request and get back | |
a detailed response using expectStringDetailed. The response will be | |
more detailed for both successful and unsuccessful requests. | |
From this post: https://medium.com/@jzxhuang/handling-detailed-http-responses-in-elm-6ddd02322e | |
-} |
This file contains 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
-- The only difference between this and Http.Error is the BadStatus constructor | |
type ErrorDetailed | |
= BadUrl String | |
| Timeout | |
| NetworkError | |
| BadStatus Http.Metadata String | |
| BadBody String | |
This file contains 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
expectStringResponse : (Result x a -> msg) -> (Response String -> Result x a) -> Expect msg | |
type Response body | |
= BadUrl_ String | |
| Timeout_ | |
| NetworkError_ | |
| BadStatus_ Metadata body | |
| GoodStatus_ Metadata body | |
This file contains 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
expectStringDetailed : (Result DetailedError ( Metadata, String ) -> msg) -> Expect msg | |
This file contains 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
getPost : Model -> (Result Http.Error String -> msg) -> Int -> Cmd msg | |
getPost model msg postId = | |
let | |
url = | |
model.apiUrl | |
++ "/posts/" | |
++ String.fromInt postId | |
++ (if model.environment == "mock" || model.environment == "test" then | |
".json" |
OlderNewer