prikhi/http-tasks

Convenience Functions for Building HTTP Requests as Tasks


Keywords
elm, http, tasks
License
MIT
Install
elm-package install prikhi/http-tasks 1.0.0

Documentation

HTTP Tasks

Build Status

This library includes some convenience functions for building HTTP requests as Tasks instead of Cmds, including things like making GET requests & decoding JSON:

import Http
import Http.Tasks exposing (get, resolveString, resolveJson)
import Json.Decode as Decode exposing (Decoder)
import Task exposing (Task)


type Msg
    = ReceiveData (Result Http.Error MyType)

type alias MyType =
    { fieldOne : String
    , fieldTwo : String
    }

decodeMyType : Decoder MyType
decodeMyType =
    Decode.map MyType
        |> Decode.field "fieldOne" Decode.string
        |> Decode.field "fieldTwo" Decode.string


firstTask : Task Http.Error String
firstTask =
    get
        { url = "https://some.domains.api"
        , resolver = resolveString
        }

secondTask : String -> Task Http.Error MyType
secondTask somePath =
    get
        { url = "https://some/domains.api/" ++ somePath ++ "/"
        , resolver = resolveJson decodeMyType
        }

myCommand : Cmd Msg
myCommand =
    firstTask
        |> Task.andThen secondTask
        |> Task.attempt ReceiveData

License

MIT, but exceptions possible.