jinjor/elm-req/1.0.0

NOTE: the latest version is 3.0.0

Rerouted (regenerated, verified). Same repository and tag, but GitHub now builds the zip with different bytes, so it no longer matches the SHA-1 package.elm-lang.org recorded. The Lamdera package server serves a copy recovered from https://github.com/jinjor/elm-req.

What we checked: Tag 1.0.0 is still on github.com/jinjor/elm-req; its commit df124f1a72a5 is from 2018-12-01 14:29 UTC, before the version was published (2018-12-01 14:34 UTC); elm.json is byte-identical to package.elm-lang.org's; the exposed modules match docs.json; the zip's SHA-1 is not the published one (GitHub's zip no longer comes out byte for byte the same), so the contents were checked instead.

The Lamdera compiler uses this copy and prints a one-line notice saying so. elm itself fails to download this version.

Details · package.elm-lang.org's zip: https://github.com/jinjor/elm-req/zipball/1.0.0/

elm-req

An experimental alternative for elm/http.

Warning: Not fully tested yet

Motivation

  • Use Tasks more.
  • Use put patch and delete.
  • Build options one by one.
  • Get full information in requests/responses to describe errors.
  • Get full information in requests to describe errors.
  • Switch from Cmd to Task gradually (and vice versa).
  • Switch from Http.Error to custom error gradually.
  • Explicitly say what is risky.

Examples

Create a task that works just like get.

getUserSimple : String -> Task Http.Error User
getUserSimple userName =
    Req.get ("https://api.github.com/users/" ++ userName)
        |> Req.stringTask (Req.simplyResolveJson userDecoder)

Work with custom error that enables

  • Full access to Req / Http.Response String.
  • Have decoded error JSON.
type alias Error =
    { method : String
    , url : String
    , details : ErrorDetails
    }


type ErrorDetails
    = BadUrl String
    | Timeout
    | NetworkError
    | BadStatus Int String (Dict String String) ErrorInfo
    | BadBody String
    | BadErrorBody String
    | Bug String


type alias ErrorInfo =
    { message : String
    }


getRepoWithDecodeError : String -> String -> Task Error Repo
getRepoWithDecodeError userName repoName =
    Req.get ("https://api.github.com/repos/" ++ userName ++ "/" ++ repoName)
        |> Req.stringTask (resolve buggyRepoDecoder)


resolve : Decoder a -> Req.Req -> Http.Response String -> Result Error a
resolve decoder req res =
    case res of
        Http.BadUrl_ url ->
            err req (BadUrl url)

        Http.Timeout_ ->
            err req Timeout

        Http.NetworkError_ ->
            err req NetworkError

        Http.BadStatus_ metadata body ->
            case D.decodeString (errorDecoder metadata.statusCode) body of
                Ok info ->
                    err req (BadStatus metadata.statusCode metadata.statusText metadata.headers info)

                Err e ->
                    err req (BadErrorBody (D.errorToString e))

        Http.GoodStatus_ _ body ->
            case D.decodeString decoder body of
                Ok a ->
                    Ok a

                Err e ->
                    err req (BadBody (D.errorToString e))


err : Req.Req -> ErrorDetails -> Result Error a
err req details =
    Err
        { method = req.method
        , url = req.url
        , details = details
        }