codacy.events.EventHandler.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of events-rabbitmq_2.13 Show documentation
Show all versions of events-rabbitmq_2.13 Show documentation
A library to send events on rabbit-mq
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) }
}
}