Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
refactor: Extract common code from responders into EntityAndClassIriS… (
- Loading branch information
Showing
39 changed files
with
526 additions
and
361 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
webapi/src/main/scala/org/knora/webapi/responders/ActorDeps.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright © 2021 - 2022 Swiss National Data and Service Center for the Humanities and/or DaSCH Service Platform contributors. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.knora.webapi.responders | ||
import akka.actor.ActorRef | ||
import akka.actor.ActorSystem | ||
import akka.util.Timeout | ||
import zio.ZIO | ||
import zio.ZLayer | ||
|
||
import scala.concurrent.ExecutionContext | ||
|
||
import org.knora.webapi.config.AppConfig | ||
import org.knora.webapi.core.AppRouter | ||
import org.knora.webapi.settings.KnoraDispatchers | ||
|
||
/** | ||
* Class encapsulating all Akka dependencies necessary to interact with the [[org.knora.webapi.core.actors.RoutingActor]] aka. "appActor" | ||
* | ||
* When using this class in a service depending on the routing actor this will provide the necessary implicit dependencies for using the ask pattern | ||
* whilst making the dependency explicit for ZIO layers. | ||
* | ||
* @example Usage in client code: | ||
* {{{ | ||
* final case class YourService(actorDeps: ActorDeps){ | ||
* private implicit val ec: ExecutionContext = actorDeps.executionContext | ||
* private implicit val timeout: Timeout = actorDeps.timeout | ||
* | ||
* private val appActor: ActorRef = actorDeps.appActor | ||
* | ||
* def someMethod = appActor.ask(SomeMessage())... | ||
* } | ||
* }}} | ||
* | ||
* @param system the akka.core.ActorSystem - used to extract the [[ExecutionContext]] from | ||
* @param appActor a reference to the [[org.knora.webapi.core.actors.RoutingActor]] | ||
* @param timeout the timeout needed for the ask pattern | ||
*/ | ||
final case class ActorDeps(system: ActorSystem, appActor: ActorRef, timeout: Timeout) { | ||
val executionContext: ExecutionContext = system.dispatchers.lookup(KnoraDispatchers.KnoraActorDispatcher) | ||
} | ||
|
||
object ActorDeps { | ||
val layer: ZLayer[AppConfig with AppRouter, Nothing, ActorDeps] = ZLayer.fromZIO { | ||
for { | ||
router <- ZIO.service[AppRouter] | ||
system = router.system | ||
appActor = router.ref | ||
timeout <- ZIO.service[AppConfig].map(_.defaultTimeoutAsDuration) | ||
} yield ActorDeps(system, appActor, timeout) | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
webapi/src/main/scala/org/knora/webapi/responders/ActorToZioBridge.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.knora.webapi.responders | ||
import akka.actor.Actor | ||
import akka.actor.ActorRef | ||
import akka.pattern.ask | ||
import akka.util.Timeout | ||
import zio.Tag | ||
import zio.Task | ||
import zio.URLayer | ||
import zio.ZIO | ||
import zio.ZLayer | ||
|
||
import scala.reflect.ClassTag | ||
|
||
import org.knora.webapi.messages.ResponderRequest | ||
|
||
/** | ||
* This trait encapsulates the [[akka.pattern.ask]] into the zio world | ||
*/ | ||
trait ActorToZioBridge { | ||
|
||
/** | ||
* Sends a message to the "appActor" [[org.knora.webapi.core.actors.RoutingActor]] using the [[akka.pattern.ask]], | ||
* casts and returns the response to the expected return type `R` as [[Task]]. | ||
* | ||
* @param message The message sent to the actor | ||
* @param tag implicit proof that the result type `R` has a [[ClassTag]] | ||
* | ||
* @tparam R The type of the expected success value | ||
* @return A Task containing either the success `R` or the failure [[Throwable]], | ||
* will fail during runtime with a [[ClassCastException]] if the `R` does not correspond | ||
* to the response of the message being sent due to the untyped nature of the ask pattern | ||
*/ | ||
def askAppActor[R: Tag](message: ResponderRequest)(implicit tag: ClassTag[R]): Task[R] | ||
|
||
} | ||
|
||
final case class ActorToZioBridgeLive(actorDeps: ActorDeps) extends ActorToZioBridge { | ||
private implicit val timeout: Timeout = actorDeps.timeout | ||
private val appActor: ActorRef = actorDeps.appActor | ||
|
||
override def askAppActor[R: Tag](message: ResponderRequest)(implicit tag: ClassTag[R]): Task[R] = | ||
ZIO.fromFuture(_ => appActor.ask(message, Actor.noSender).mapTo[R]) | ||
} | ||
|
||
object ActorToZioBridge { | ||
val live: URLayer[ActorDeps, ActorToZioBridgeLive] = ZLayer.fromFunction(ActorToZioBridgeLive.apply _) | ||
} |
Oops, something went wrong.