Formerly calions-app/jsonapi-http-retry. Search finds this package by either name.
Tries to perform a Task and retry it upon failure, allowing you to execute other Cmds between each failure.
The following example executes a request with a retry policy when the request fails and executes a port before each new retry:
type Msg
= OnRetry (Http.CmdRetry.RetryContext Msg Entity)
| OnEntityRetrieved (RemoteData.RemoteData Http.Error.RequestError Entity)
type alias Model =
{ entity : RemoteData.RemoteData Http.Error.RequestError Entity }
init : ( Model, Cmd Msg )
init =
( { entity = RemoteData.Loading }
, getEntity
|> Http.CmdRetry.with
[ Http.Retry.maxDuration 7000
, Http.Retry.exponentialBackoff { interval = 500, maxInterval = 3000 }
]
[ Http.Retry.onUnauthenticatedStatus ]
OnRetry
)
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
OnRetry retryContext ->
( model
, Http.CmdRetry.newAttempt
executePort
OnEntityRetrieved
retryContext
)
OnEntityRetrieved entity ->
( { model | entity = entity }, Cmd.none )
getEntity : Task Never (RemoteData.RemoteData Http.Error.RequestError Entity)
getEntity =
Request.request
{ url = "<http://endpoint">
, headers = []
, body = Json.Encode.object []
, documentDecoder = JsonApi.Decode.resources "resource-type" entityDecoder
}Attempt a new retry from your update function with the RetryContext you received.
The first parameter allows you to execute a Cmd just before the next retry.
The second parameter is the message you want to send when the request finally succeeded or failed (after all configured retries)
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
OnRetry retryContext ->
( model
, Http.CmdRetry.newAttempt
(\lastError ->
doSomethingWithLastError lastError
)
OnRequestDone
retryContext
)Type used by the module to keep the context of the retry process. You will never handle it directly
msg is your Msg type and data is the data you you to receive from your request.
Tries to execute the given task. You will receive a message with the retry context. From there you will call
newAttemptwhich will handle your request retry, allowing you to execute aCmdbefore the next retry.