Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

Remove total field in Pager, add count fn. #50

Merged
merged 5 commits into from
Nov 5, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 38 additions & 14 deletions src/Kinto.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Kinto exposing
( Client, client, Auth(..), headersForAuth, Resource, bucketResource, collectionResource, recordResource, decodeData, encodeData, errorDecoder, errorToString
, Request, withQueryParam
, get, create, update, replace, delete
, getList
, getList, count
, Pager, emptyPager, updatePager, loadNextPage
, sort, limit, filter, Filter(..)
, Endpoint(..), endpointUrl, ErrorDetail, Error(..), expectJson, expectPagerJson
Expand Down Expand Up @@ -51,7 +51,7 @@ Item endpoints are:

## Resource list requests

@docs getList
@docs getList, count


### Paginated list
Expand Down Expand Up @@ -249,7 +249,6 @@ type alias Pager a =
{ client : Client
, objects : List a
, decoder : Decode.Decoder (List a)
, total : Int
, nextPage : Maybe Url
}

Expand All @@ -264,7 +263,6 @@ emptyPager clientInstance resource =
{ client = clientInstance
, objects = []
, decoder = resource.listDecoder
, total = 0
, nextPage = Nothing
}

Expand All @@ -278,8 +276,7 @@ to the previous list.
updatePager : Pager a -> Pager a -> Pager a
updatePager nextPager previousPager =
{ previousPager
| total = nextPager.total
, nextPage = nextPager.nextPage
| nextPage = nextPager.nextPage
, objects = previousPager.objects ++ nextPager.objects
}

Expand Down Expand Up @@ -448,16 +445,10 @@ expectPagerJson toMsg clientInstance decoder =
nextPage =
Dict.get "next-page" headers

total =
Dict.get "total-records" headers
|> Maybe.map (String.toInt >> Maybe.withDefault 0)
|> Maybe.withDefault 0

createPager objects =
{ client = clientInstance
, objects = objects
, decoder = decoder
, total = total
, nextPage = nextPage
}
in
Expand All @@ -481,6 +472,26 @@ expectPagerJson toMsg clientInstance decoder =
NetworkError anyError |> Err


{-| Extract the value of the `Total-Records` header
-}
expectTotalCount : (Result Error Int -> msg) -> Http.Expect msg
expectTotalCount toMsg =
Http.expectStringResponse toMsg <|
\response ->
case response of
Http.BadStatus_ { statusCode, statusText } body ->
Err <| extractKintoError statusCode statusText body

Http.GoodStatus_ { headers } _ ->
Dict.get "total-records" headers
|> Maybe.map (String.toInt >> Maybe.withDefault 0)
|> Maybe.withDefault 0
|> Ok

anyError ->
NetworkError anyError |> Err


extractKintoError : StatusCode -> StatusMsg -> String -> Error
extractKintoError statusCode statusMsg body =
case Decode.decodeString errorDecoder body of
Expand Down Expand Up @@ -702,6 +713,20 @@ get resource itemId toMsg clientInstance =
|> HttpBuilder.withExpect (expectJson toMsg resource.itemDecoder)


{-| Count the number of records within a given collection

client
|> count resource CountReceived

-}
count : Resource a -> (Result Error Int -> msg) -> Client -> Request msg
count resource toMsg clientInstance =
endpointUrl clientInstance.baseUrl resource.listEndpoint
|> HttpBuilder.head
|> HttpBuilder.withHeaders clientInstance.headers
|> HttpBuilder.withExpect (expectTotalCount toMsg)


{-| Create a GET request on one of the plural endpoints. As lists are always
possibly paginated, When the request is succesful, a `Pager` is attached to the
reponse message.
Expand All @@ -715,8 +740,7 @@ getList resource toMsg clientInstance =
endpointUrl clientInstance.baseUrl resource.listEndpoint
|> HttpBuilder.get
|> HttpBuilder.withHeaders clientInstance.headers
|> HttpBuilder.withExpect
(expectPagerJson toMsg clientInstance resource.listDecoder)
|> HttpBuilder.withExpect (expectPagerJson toMsg clientInstance resource.listDecoder)


{-| If a pager has a `nextPage`, creates a GET request to retrieve the next page of objects.
Expand Down