2018-01-18 19:15:29 +00:00
|
|
|
A library for fetching data from paginated JSON REST APIs.
|
|
|
|
|
|
|
|
# Motivation
|
|
|
|
|
|
|
|
This library is built to handle fetching data from a JSON REST API
|
|
|
|
that paginates its results. It inspects response headers, and will
|
|
|
|
prepare subsequent requests when additional pages of results remain.
|
|
|
|
|
|
|
|
Given a JSON decoder for the data type returned in the paginated
|
|
|
|
collection, it will return a data structure containing the results
|
|
|
|
fetched, and a new request object for fetching additional records from
|
|
|
|
the next page if more are available.
|
|
|
|
|
|
|
|
It is expected that the paginated REST API will provide a link for the
|
|
|
|
next page of results in the `Link` header, e.g.:
|
|
|
|
|
|
|
|
Link: <https://api.example.com/search?q=abc&page=2>; rel="next"
|
|
|
|
|
|
|
|
Absence of a link with `rel="next"` in the response headers is assumed
|
|
|
|
to mean the final page of results has been reached.
|
|
|
|
|
|
|
|
# Example Usage
|
|
|
|
|
|
|
|
import Http
|
|
|
|
import Json.Decode exposing (string)
|
|
|
|
import Paginated exposing (Response(..))
|
|
|
|
|
|
|
|
type alias Model =
|
2018-01-22 22:02:21 +00:00
|
|
|
{ results : Maybe (List String) }
|
2018-01-18 19:15:29 +00:00
|
|
|
|
|
|
|
type Msg
|
|
|
|
= Search
|
2018-01-22 22:02:21 +00:00
|
|
|
| Results (Result Http.Error (List String))
|
2018-01-18 19:15:29 +00:00
|
|
|
|
|
|
|
update : Msg -> Model -> ( Model, Cmd Msg )
|
|
|
|
update msg model =
|
|
|
|
case msg of
|
|
|
|
Search ->
|
|
|
|
( model, doSearch )
|
|
|
|
|
2018-01-22 22:02:21 +00:00
|
|
|
Results results ->
|
|
|
|
( { model | results = Result.toMaybe results }
|
|
|
|
, Cmd.none
|
|
|
|
)
|
2018-01-18 19:15:29 +00:00
|
|
|
|
|
|
|
doSearch : Cmd Msg
|
|
|
|
doSearch =
|
|
|
|
Paginated.send Results <|
|
2018-01-22 22:02:21 +00:00
|
|
|
Paginated.get "http://example.com/search?q=abc" string
|