jinjor/elm-req/1.2.0

NOTE: the latest version is 3.0.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

type alias ReqWithError a =
{ request : Req
, error : Error a
}

Req with Error

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
{ decoder : Decoder a
, errorDecoder : Metadata -> Decoder e
}
-> Req
-> Response String
-> Result (ReqWithError e) a

resolve Json and return Error with Req.

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.