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

codacy.events.EventHandler.scala Maven / Gradle / Ivy

The newest version!
package codacy.events

import codacy.events.internal.{defaultEventLogger, EventLogger, Handlers}
import io.circe.Json
import org.slf4j.{Logger, LoggerFactory}

import scala.concurrent.Future

abstract class EventHandler[E <: Event](implicit
    logger: EventLogger = defaultEventLogger,
    decoding: Event.DecodingProps[E]
) {
  import decoding._
  def handleEvent(e: E): Future[Unit]

  final private[events] def generic: Handlers.RequestHandler = {
    (
      implicitly[PathHolder[E]].path,
      (path: String, raw: Json) =>
        raw
          .as[E](decoding.decoder)
          .fold(
            failure => {
              logger.warn(s"$path could not be parsed, will discard event!", failure)
              Future.successful(())
            },
            event => handleEvent(event)
          )
    )
  }
}

object EventHandler {

  def apply[E <: Event: Event.DecodingProps](f: E => Future[Unit])(implicit logger: EventLogger): EventHandler[E] = {
    new EventHandler { override def handleEvent(e: E) = f(e) }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy