jinjor/elm-req/1.1.0

NOTE: the latest version is 3.0.0

Rerouted (retagged, needs approval). The tag now points at a different commit than when this version was published. The Lamdera package server serves a copy recovered from https://github.com/jinjor/elm-req.

What we checked: Tag 1.1.0 is still on github.com/jinjor/elm-req; its commit dddb0d30ed61 is from 2018-12-01 18:17 UTC, after the version was published (2018-12-01 17:33 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 and nothing else confirms its contents.

We could not verify this copy against what was published, so the Lamdera compiler asks before using it (y/n). Without a terminal, as in CI, it stops with an error unless approved:

  • lamdera make --auto-approve-package-rerouting approves rerouted versions for that run,
  • LAMDERA_AUTO_APPROVE_PACKAGE_REROUTING=1 does the same for every run in that environment,
  • LAMDERA_CACHE=off skips the Lamdera package server altogether: packages come from package.elm-lang.org and GitHub only, as with elm, and a rerouted version then fails to download.

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

Req

An experimental alternative for elm/http.

See more details in elm/http.

Types

type alias Req =
{ method : String
, url : String
, headers : List ( String, String )
, body : Body
, timeout : Maybe Float
, allowCookiesFromOtherDomains : Bool
}

Request

type Body
= EmptyBody
| StringBody String String
| JsonBody Value
| FileBody File
| BytesBody String Bytes
| MultipartBody (List Part)

Body

type Part
= StringPart String String
| FilePart String File
| BytesPart String String Bytes

Part

type Error a
= BadUrl String
| Timeout
| NetworkError
| BadStatus Metadata a
| BadBody Metadata String

Similar to Http.Error but have more informarion

Methods

init : String -> String -> Req

Arbitrary method

get : String -> Req

GET

post : String -> Req

POST

put : String -> Req

PUT

patch : String -> Req

PATCH

delete : String -> Req

DELETE

Body

withStringBody : String -> String -> Req -> Req

Add text with mime type.

withJsonBody : Value -> Req -> Req

Add JSON for POST etc.

withFileBody : File -> Req -> Req

Add file to upload

withBytesBody : String -> Bytes -> Req -> Req

Add bytes with mime type.

withMultipartBody : List Part -> Req -> Req

Add multipart body.

Parts

stringPart : String -> String -> Part

String part (key and value)

filePart : String -> File -> Part

String part (key and file)

bytesPart : String -> String -> Bytes -> Part

Bytes part (key and mime and bytes)

Options

withHeader : String -> String -> Req -> Req

Add header

withTimeout : Float -> Req -> Req

Add timeout

allowCookiesFromOtherDomains : Bool -> Req -> Req

Allow cookies from other domains.

Task

stringTask : (Req -> Response String -> Result x a) -> Req -> Task x a

Create a task to parse string body.

bytesTask : (Req -> Response Bytes -> Result x a) -> Req -> Task x a

Create a task to parse bytes body.

whateverTask : msg -> Req -> Task x msg

Ignore the result.

toTask : Resolver x a -> Req -> Task x a

Create a task with existing Http.Resolver.

Resolver

simplyResolveJson : Decoder a -> Req -> Response String -> Result Error a

Make a resolver function that returns Http.Error.

getUserSimple : String -> Task Http.Error User
getUserSimple userName =
    Req.get ("https://api.github.com/users/" ++ userName)
        |> Req.stringTask (Req.simplyResolveJson userDecoder)
{ decoder : Decoder a
, errorDecoder : Metadata -> Decoder e
}
-> Req
-> Response String
-> Result (Error e) a

Make a resolver function that returns Req.Error.

getUserSimple : String -> Task Http.Error User
getUserSimple userName =
    Req.get ("https://api.github.com/users/" ++ userName)
        |> Req.stringTask
            (Req.resolveJson
                { decoder = userDecoder
                , errorDecoder = errorDecoder
                }
            )


- First decoder is used for good body
- First decoder is used for bad body
- Decoding errors of both go to BadBody

Tracking

String
-> (Result x a -> msg)
-> (Req -> Response String -> Result x a)
-> Req
-> Cmd msg

Track progress of responses with string body.

String
-> (Result x a -> msg)
-> (Req -> Response Bytes -> Result x a)
-> Req
-> Cmd msg

Track progress of responses with bytes body.

trackWhatever : String -> (Result Never () -> msg) -> Req -> Cmd msg

Track something but ignore the result.

track : String -> Expect msg -> Req -> Cmd msg

Make a traking Cmd using existing Http.Expect.