-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support pagination even for queries that can not specify order-by
Get rid of Aux[Q,R] Allow specifying direction to get next search query We're trying to support LightBox functionality that was introduced here: guardian/frontend#20462 Co-authored-by: Roberto Tyley <[email protected]> Support direction for all paginatable queries, and remove unused PaginatedApiResponse classes
- Loading branch information
Showing
8 changed files
with
230 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 22 additions & 36 deletions
58
client/src/main/scala/com.gu.contentapi.client/model/Decoder.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,34 @@ | ||
package com.gu.contentapi.client | ||
|
||
import com.gu.contentapi.client.model._ | ||
import com.gu.contentapi.client.model.v1._ | ||
import com.gu.contentapi.client.thrift.ThriftDeserializer | ||
import com.twitter.scrooge.{ThriftStruct, ThriftStructCodec} | ||
|
||
/** Typeclass witnessing how to unmarshall a Thrift stream of bytes | ||
* into a concrete data type | ||
* @tparam Query the query type | ||
*/ | ||
trait Decoder[Query] { | ||
/** the response type corresponding to `Query` */ | ||
type Response <: ThriftStruct | ||
/** the codec unmarshalling instances of `Response` */ | ||
def codec: ThriftStructCodec[Response] | ||
/** performs the unmarshalling | ||
* @return a function taking an array of bytes into a `Response` | ||
*/ | ||
def decode: Array[Byte] => Response = ThriftDeserializer.deserialize(_, codec) | ||
} | ||
|
||
private[client] object Decoder { | ||
class Decoder[Response <: ThriftStruct](codec: ThriftStructCodec[Response]) { | ||
def decode(data: Array[Byte]): Response = ThriftDeserializer.deserialize(data, codec) | ||
} | ||
|
||
type Aux[Q, R] = Decoder[Q] { type Response = R } | ||
trait PaginationDecoder[Response, Element] { | ||
val pageSize: Response => Int | ||
val elements: Response => collection.Seq[Element] | ||
} | ||
|
||
private def apply[Q, R <: ThriftStruct](c: ThriftStructCodec[R]) = new Decoder[Q] { | ||
type Response = R | ||
def codec = c | ||
} | ||
object Decoder { | ||
type PageableResponseDecoder[Response <: ThriftStruct, Element] = Decoder[Response] with PaginationDecoder[Response, Element] | ||
|
||
private def atomsDecoder[Query] = apply[Query, AtomsResponse](AtomsResponse) | ||
def pageableResponseDecoder[R <: ThriftStruct, E](c: ThriftStructCodec[R])(ps: R => Int, el: R => collection.Seq[E]): PageableResponseDecoder[R, E] = | ||
new Decoder[R](c) with PaginationDecoder[R, E] { | ||
val pageSize: R => Int = ps | ||
val elements: R => collection.Seq[E] = el | ||
} | ||
|
||
implicit val itemQuery = apply[ItemQuery, ItemResponse](ItemResponse) | ||
implicit val tagsQuery = apply[TagsQuery, TagsResponse](TagsResponse) | ||
implicit val sectionsQuery = apply[SectionsQuery, SectionsResponse](SectionsResponse) | ||
implicit val editionsQuery = apply[EditionsQuery, EditionsResponse](EditionsResponse) | ||
implicit val videoStatsQuery = apply[VideoStatsQuery, VideoStatsResponse](VideoStatsResponse) | ||
implicit val atomsQuery = atomsDecoder[AtomsQuery] | ||
implicit val recipesQuery = atomsDecoder[RecipesQuery] | ||
implicit val reviewsQuery = atomsDecoder[ReviewsQuery] | ||
implicit val gameReviewsQuery = atomsDecoder[GameReviewsQuery] | ||
implicit val restaurantReviewsQuery = atomsDecoder[RestaurantReviewsQuery] | ||
implicit val filmReviewsQuery = atomsDecoder[FilmReviewsQuery] | ||
implicit def searchQueryBase[T <: SearchQueryBase[T]] = apply[T, SearchResponse](SearchResponse) | ||
implicit def nextQuery[Q <: PaginatedApiQuery[Q]](implicit d: Decoder[Q]) = apply[NextQuery[Q], d.Response](d.codec) | ||
implicit def atomsUsageQuery = apply[AtomUsageQuery, AtomUsageResponse](AtomUsageResponse) | ||
implicit val itemDecoder: Decoder[ItemResponse] = new Decoder(ItemResponse) | ||
implicit val tagsDecoder: PageableResponseDecoder[TagsResponse, Tag] = pageableResponseDecoder(TagsResponse)(_.pageSize, _.results) | ||
implicit val sectionsQuery: Decoder[SectionsResponse] = new Decoder(SectionsResponse) | ||
implicit val editionsDecoder: Decoder[EditionsResponse] = new Decoder(EditionsResponse) | ||
implicit val videoStatsDecoder: Decoder[VideoStatsResponse] = new Decoder(VideoStatsResponse) | ||
implicit val atomsDecoder: Decoder[AtomsResponse] = new Decoder(AtomsResponse) | ||
implicit val searchDecoder: PageableResponseDecoder[SearchResponse, Content] = pageableResponseDecoder(SearchResponse)(_.pageSize, _.results) | ||
implicit val atomUsageDecoder: Decoder[AtomUsageResponse] = new Decoder(AtomUsageResponse) | ||
} |
31 changes: 0 additions & 31 deletions
31
client/src/main/scala/com.gu.contentapi.client/model/PaginatedApiResponse.scala
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.