Skip to content

Commit

Permalink
Add config to limit max number of ids for getPlayersWithIds api (#88)
Browse files Browse the repository at this point in the history
Close #36
  • Loading branch information
lenguyenthanh authored Jun 9, 2024
2 parents 2dea0bd + c92b669 commit 9f87e45
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,12 @@ object providers:

given RefinementProvider[PageSizeFormat, Int, Natural] =
Refinement.drivenBy(Natural.either, identity)

given [A]: RefinementProvider[NonEmptySetFormat, Set[A], NonEmptySet[A]] =
Refinement.drivenBy[NonEmptySetFormat](
NonEmptySet.either,
(b: NonEmptySet[A]) => b.value
)

given RefinementProvider.Simple[smithy.api.Length, fide.types.NonEmptySet[fide.spec.PlayerId]] =
RefinementProvider.lengthConstraint(x => x.value.size)
8 changes: 8 additions & 0 deletions modules/api/src/main/smithy/_global.smithy
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ structure PageFormat {}
)
structure PageSizeFormat { }

@trait(selector: "list")
@refinement(
targetType: "fide.types.NonEmptySet",
providerImport: "fide.spec.providers.given"
parameterised: true
)
structure nonEmptySetFormat {}

@PageFormat
@unwrap
string PageNumber
Expand Down
4 changes: 2 additions & 2 deletions modules/api/src/main/smithy/federations.smithy
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ operation GetFederationsSummary {
@httpQuery("page")
page: PageNumber = "1"
@httpQuery("page_size")
@range(max: 100)
@range(min: 1, max: 100)
pageSize: PageSize = 30
}

Expand Down Expand Up @@ -62,7 +62,7 @@ operation GetFederationPlayersById {
@httpQuery("page")
page: PageNumber = "1"
@httpQuery("page_size")
@range(max: 100)
@range(min: 1, max: 100)
pageSize: PageSize = 30
}

Expand Down
6 changes: 5 additions & 1 deletion modules/api/src/main/smithy/players.smithy
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace fide.spec

use alloy#simpleRestJson
use smithy4s.meta#scalaImports
use smithy4s.meta#unwrap

@simpleRestJson
service PlayerService {
Expand All @@ -24,7 +25,7 @@ operation GetPlayers {
page: PageNumber = "1"

@httpQuery("page_size")
@range(max: 100)
@range(min: 1, max: 100)
pageSize: PageSize = 30
}

Expand Down Expand Up @@ -72,6 +73,9 @@ map PlayerMap {
}

@uniqueItems
@length(min: 1, max: 100)
@nonEmptySetFormat
@unwrap
list SetPlayerIds {
member: PlayerId
}
Expand Down
4 changes: 2 additions & 2 deletions modules/backend/src/main/scala/service.player.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ class PlayerServiceImpl(db: Db)(using Logger[IO]) extends PlayerService[IO]:
_.fold(IO.raiseError(PlayerNotFound(id))):
_.transform.pure[IO]

override def getPlayerByIds(ids: Set[PlayerId]): IO[GetPlayerByIdsOutput] =
db.playersByIds(ids.map(_.value))
override def getPlayerByIds(ids: NonEmptySet[PlayerId]): IO[GetPlayerByIdsOutput] =
db.playersByIds(ids.value.map(_.value))
.handleErrorWith: e =>
error"Error in getPlayersByIds: $ids, $e" *>
IO.raiseError(InternalServerError("Internal server error"))
Expand Down
13 changes: 13 additions & 0 deletions modules/types/src/main/scala/NonEmptySet.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package fide.types

import cats.syntax.all.*
import io.github.iltotore.iron.*
import io.github.iltotore.iron.constraint.all.*

opaque type NonEmptySet = [A] =>> Set[A] :| MinLength[1]

object NonEmptySet:
def either[A](set: Set[A]): Either[String, NonEmptySet[A]] =
set.refineEither[MinLength[1]]

extension [A](set: NonEmptySet[A]) inline def value: Set[A] = set

0 comments on commit 9f87e45

Please sign in to comment.