Skip to content

Commit

Permalink
Add api for structured variants
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcardell committed May 1, 2024
1 parent 150d859 commit 4c7ad0d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 4c7ad0d

Please sign in to comment.