All Downloads are FREE. Search and download functionalities are using the official Maven repository.

kinesis4cats.smithy4s.client.producer.fs2.localstack.LocalstackFS2KinesisProducer.scala Maven / Gradle / Ivy

There is a newer version: 0.0.32
Show newest version
/*
 * Copyright 2023-2023 etspaceman
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package kinesis4cats.smithy4s.client
package producer
package fs2
package localstack

import _root_.fs2.compression.Compression
import _root_.fs2.concurrent.Channel
import _root_.fs2.io.file.Files
import cats.effect._
import cats.effect.kernel.DeferredSink
import cats.effect.syntax.all._
import cats.syntax.all._
import com.amazonaws.kinesis.PutRecordsOutput
import org.http4s.client.Client
import org.typelevel.log4cats.StructuredLogger
import org.typelevel.log4cats.noop.NoOpLogger
import smithy4s.aws.kernel.AwsRegion

import kinesis4cats.localstack.LocalstackConfig
import kinesis4cats.localstack.TestStreamConfig
import kinesis4cats.models.StreamNameOrArn
import kinesis4cats.producer.Producer
import kinesis4cats.producer.Record
import kinesis4cats.producer.ShardMap
import kinesis4cats.producer.ShardMapCache
import kinesis4cats.producer.fs2.FS2Producer
import kinesis4cats.smithy4s.client.producer.localstack.LocalstackKinesisProducer

/** Like KinesisProducer, but also includes the
  * [[kinesis4cats.smithy4s.client.middleware.LocalstackProxy LocalstackProxy]]
  * middleware, and leverages mock AWS credentials
  */
object LocalstackFS2KinesisProducer {
  final case class Builder[F[_]: Compression: Files] private (
      client: Client[F],
      region: AwsRegion,
      localstackConfig: LocalstackConfig,
      config: FS2Producer.Config[F],
      logger: StructuredLogger[F],
      encoders: LocalstackKinesisProducer.LogEncoders[F],
      logRequestsResponses: Boolean,
      streamsToCreate: List[TestStreamConfig[F]],
      shardMapF: (
          KinesisClient[F],
          StreamNameOrArn
      ) => F[Either[ShardMapCache.Error, ShardMap]]
  )(implicit F: Async[F]) {

    def withLocalstackConfig(localstackConfig: LocalstackConfig): Builder[F] =
      copy(localstackConfig = localstackConfig)
    def withConfig(config: FS2Producer.Config[F]): Builder[F] = copy(
      config = config
    )
    def withClient(client: Client[F]): Builder[F] = copy(client = client)
    def withRegion(region: AwsRegion): Builder[F] = copy(region = region)
    def withLogger(logger: StructuredLogger[F]): Builder[F] =
      copy(logger = logger)
    def withLogEncoders(
        encoders: LocalstackKinesisProducer.LogEncoders[F]
    ): Builder[F] =
      copy(encoders = encoders)
    def withLogRequestsResponses(logRequestsResponses: Boolean): Builder[F] =
      copy(logRequestsResponses = logRequestsResponses)
    def enableLogging: Builder[F] = withLogRequestsResponses(true)
    def disableLogging: Builder[F] = withLogRequestsResponses(false)
    def withStreamsToCreate(streamsToCreate: List[TestStreamConfig[F]]) =
      copy(streamsToCreate = streamsToCreate)
    def withShardMapF(
        shardMapF: (
            KinesisClient[F],
            StreamNameOrArn
        ) => F[Either[ShardMapCache.Error, ShardMap]]
    ): Builder[F] = copy(
      shardMapF = shardMapF
    )

    def build: Resource[F, FS2KinesisProducer[F]] = for {
      underlying <- LocalstackKinesisProducer.Builder
        .default[F](
          client,
          region,
          config.producerConfig.streamNameOrArn,
          localstackConfig
        )
        .withConfig(config.producerConfig)
        .withLogEncoders(encoders)
        .withLogger(logger)
        .withLogRequestsResponses(logRequestsResponses)
        .withStreamsToCreate(streamsToCreate)
        .withShardMapF(shardMapF)
        .build
      channel <- Channel
        .bounded[
          F,
          (Record, DeferredSink[F, F[Producer.Result[PutRecordsOutput]]])
        ](config.queueSize)
        .toResource
      producer = new FS2KinesisProducer[F](
        logger,
        config,
        channel,
        underlying
      )
      _ <- producer.resource
    } yield producer
  }

  object Builder {
    def default[F[_]: Async: Compression: Files](
        client: Client[F],
        region: AwsRegion,
        streamNameOrArn: StreamNameOrArn,
        prefix: Option[String] = None
    ): F[Builder[F]] = LocalstackConfig
      .load(prefix)
      .map(default(client, region, streamNameOrArn, _))

    def default[F[_]: Async: Compression: Files](
        client: Client[F],
        region: AwsRegion,
        streamNameOrArn: StreamNameOrArn,
        config: LocalstackConfig
    ): Builder[F] =
      Builder[F](
        client,
        region,
        config,
        FS2Producer.Config.default[F](streamNameOrArn),
        NoOpLogger[F],
        LocalstackKinesisProducer.LogEncoders.show,
        logRequestsResponses = true,
        Nil,
        (client: KinesisClient[F], snoa: StreamNameOrArn) =>
          KinesisProducer.getShardMap(client, snoa)
      )

    @annotation.unused
    private def unapply[F[_]](builder: Builder[F]): Unit = ()
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy