From 4c7ad0d4224c7ca64b05f76b2917a4c401e7b2b2 Mon Sep 17 00:00:00 2001 From: alexcardell <29524087+alexcardell@users.noreply.github.com> Date: Wed, 1 May 2024 17:30:02 +0100 Subject: [PATCH] Add api for structured variants --- .../scala/io/cardell/ff4s/flipt/FliptApi.scala | 5 +++++ .../io/cardell/ff4s/flipt/FliptApiImpl.scala | 13 +++++++++++++ .../ff4s/flipt/model/EvaluationResponse.scala | 18 ++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/flipt/sdk-server/src/main/scala/io/cardell/ff4s/flipt/FliptApi.scala b/flipt/sdk-server/src/main/scala/io/cardell/ff4s/flipt/FliptApi.scala index 94739fd..376b2ac 100644 --- a/flipt/sdk-server/src/main/scala/io/cardell/ff4s/flipt/FliptApi.scala +++ b/flipt/sdk-server/src/main/scala/io/cardell/ff4s/flipt/FliptApi.scala @@ -25,10 +25,15 @@ import io.cardell.ff4s.flipt.model.BooleanEvaluationResponse import io.cardell.ff4s.flipt.model.VariantEvaluationResponse import io.cardell.ff4s.flipt.model.BatchEvaluationRequest import io.cardell.ff4s.flipt.model.BatchEvaluationResponse +import io.circe.Decoder +import io.cardell.ff4s.flipt.model.StructuredVariantEvaluationResponse trait FliptApi[F[_]] { def evaluateBoolean(request: EvaluationRequest): F[BooleanEvaluationResponse] def evaluateVariant(request: EvaluationRequest): F[VariantEvaluationResponse] + def evaluateStructuredVariant[A: Decoder]( + request: EvaluationRequest + ): F[StructuredVariantEvaluationResponse[A]] def evaluateBatch(request: BatchEvaluationRequest): F[BatchEvaluationResponse] } diff --git a/flipt/sdk-server/src/main/scala/io/cardell/ff4s/flipt/FliptApiImpl.scala b/flipt/sdk-server/src/main/scala/io/cardell/ff4s/flipt/FliptApiImpl.scala index 2f81f36..6543c32 100644 --- a/flipt/sdk-server/src/main/scala/io/cardell/ff4s/flipt/FliptApiImpl.scala +++ b/flipt/sdk-server/src/main/scala/io/cardell/ff4s/flipt/FliptApiImpl.scala @@ -26,6 +26,8 @@ import io.cardell.ff4s.flipt.model.BooleanEvaluationResponse import io.cardell.ff4s.flipt.model.VariantEvaluationResponse import io.cardell.ff4s.flipt.model.BatchEvaluationRequest import io.cardell.ff4s.flipt.model.BatchEvaluationResponse +import io.circe.Decoder +import io.cardell.ff4s.flipt.model.StructuredVariantEvaluationResponse protected[flipt] class FliptApiImpl[F[_]: Concurrent]( client: Client[F], @@ -53,6 +55,17 @@ protected[flipt] class FliptApiImpl[F[_]: Concurrent]( client.expect[VariantEvaluationResponse](req) } + override def evaluateStructuredVariant[A: Decoder]( + request: EvaluationRequest + ): F[StructuredVariantEvaluationResponse[A]] = { + val req = Request[F]( + method = Method.POST, + uri = baseUri / "evaluate" / "v1" / "variant" + ).withEntity(request) + + client.expect[StructuredVariantEvaluationResponse[A]](req) + } + override def evaluateBatch( request: BatchEvaluationRequest ): F[BatchEvaluationResponse] = { diff --git a/flipt/sdk-server/src/main/scala/io/cardell/ff4s/flipt/model/EvaluationResponse.scala b/flipt/sdk-server/src/main/scala/io/cardell/ff4s/flipt/model/EvaluationResponse.scala index 466d4d2..d90b60f 100644 --- a/flipt/sdk-server/src/main/scala/io/cardell/ff4s/flipt/model/EvaluationResponse.scala +++ b/flipt/sdk-server/src/main/scala/io/cardell/ff4s/flipt/model/EvaluationResponse.scala @@ -19,6 +19,7 @@ package io.cardell.ff4s.flipt.model import cats.syntax.functor._ import io.circe.Decoder import io.circe.generic.semiauto.deriveDecoder +import scala.annotation.unused sealed trait EvaluationResponse @@ -58,6 +59,23 @@ object VariantEvaluationResponse { implicit val d: Decoder[VariantEvaluationResponse] = deriveDecoder } +case class StructuredVariantEvaluationResponse[A]( + `match`: Boolean, + segmentKeys: List[String], + reason: EvaluationReason, + flagKey: String, + variantKey: String, + variantAttachment: A, + requestDurationMillis: Float, + timestamp: String +) extends EvaluationResponse + +object StructuredVariantEvaluationResponse { + implicit def d[A](implicit + @unused da: Decoder[A] + ): Decoder[StructuredVariantEvaluationResponse[A]] = deriveDecoder +} + case class ErrorEvaluationResponse( flagKey: String, namespaceKey: String,