|
| 1 | +package org.knora.webapi.routing.admin |
| 2 | + |
| 3 | +import akka.actor.ActorRef |
| 4 | +import akka.pattern.ask |
| 5 | +import akka.util.Timeout |
| 6 | +import zhttp.http._ |
| 7 | +import zio.ZIO |
| 8 | +import zio.ZLayer |
| 9 | + |
| 10 | +import java.net.URLDecoder |
| 11 | + |
| 12 | +import dsp.errors.BadRequestException |
| 13 | +import dsp.errors.InternalServerException |
| 14 | +import dsp.errors.KnoraException |
| 15 | +import dsp.errors.RequestRejectedException |
| 16 | +import org.knora.webapi.config.AppConfig |
| 17 | +import org.knora.webapi.core.AppRouter |
| 18 | +import org.knora.webapi.http.handler.ExceptionHandlerZ |
| 19 | +import org.knora.webapi.messages.admin.responder.projectsmessages.ProjectGetRequestADM |
| 20 | +import org.knora.webapi.messages.admin.responder.projectsmessages.ProjectGetResponseADM |
| 21 | +import org.knora.webapi.messages.admin.responder.projectsmessages.ProjectIdentifierADM |
| 22 | +import org.knora.webapi.messages.util.KnoraSystemInstances |
| 23 | + |
| 24 | +final case class ProjectsRouteZ(router: AppRouter, appConfig: AppConfig) { |
| 25 | + implicit val sender: ActorRef = router.ref |
| 26 | + implicit val timeout: Timeout = appConfig.defaultTimeoutAsDuration |
| 27 | + |
| 28 | + def getProjectByIri(iri: String): ZIO[Any, KnoraException, Response] = |
| 29 | + for { |
| 30 | + user <- ZIO.succeed(KnoraSystemInstances.Users.SystemUser) |
| 31 | + iriDecoded <- |
| 32 | + ZIO |
| 33 | + .attempt(URLDecoder.decode(iri, "utf-8")) |
| 34 | + .orElseFail(BadRequestException(s"Failed to decode IRI $iri")) |
| 35 | + iriValue <- ProjectIdentifierADM.IriIdentifier |
| 36 | + .fromString(iriDecoded) |
| 37 | + .toZIO |
| 38 | + message = ProjectGetRequestADM(identifier = iriValue, requestingUser = user) |
| 39 | + response <- ZIO.fromFuture(_ => router.ref.ask(message)).map(_.asInstanceOf[ProjectGetResponseADM]).orDie |
| 40 | + } yield Response.json(response.toJsValue.toString()) |
| 41 | + |
| 42 | + val route: HttpApp[Any, Nothing] = |
| 43 | + (Http |
| 44 | + .collectZIO[Request] { |
| 45 | + // TODO : Add user authentication, make tests run with the new route |
| 46 | + // Returns a single project identified through the IRI. |
| 47 | + case Method.GET -> !! / "admin" / "projects" / "iri" / iri => |
| 48 | + getProjectByIri(iri) |
| 49 | + }) |
| 50 | + .catchAll { |
| 51 | + case RequestRejectedException(e) => |
| 52 | + ExceptionHandlerZ.exceptionToJsonHttpResponseZ(e, appConfig) |
| 53 | + case InternalServerException(e) => |
| 54 | + ExceptionHandlerZ.exceptionToJsonHttpResponseZ(e, appConfig) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +object ProjectsRouteZ { |
| 59 | + val layer: ZLayer[AppRouter with AppConfig, Nothing, ProjectsRouteZ] = ZLayer.fromFunction { (router, config) => |
| 60 | + ProjectsRouteZ(router, config) |
| 61 | + } |
| 62 | +} |
0 commit comments