-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add resource practice routes --------- Co-authored-by: Simon Dumas <simon.dumas@epfl.ch>
- Loading branch information
Showing
41 changed files
with
1,098 additions
and
390 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
162 changes: 162 additions & 0 deletions
162
delta/app/src/main/scala/ch/epfl/bluebrain/nexus/delta/routes/ResourcesTrialRoutes.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,162 @@ | ||
package ch.epfl.bluebrain.nexus.delta.routes | ||
|
||
import akka.http.scaladsl.server.Directives._ | ||
import akka.http.scaladsl.server.Route | ||
import cats.syntax.all._ | ||
import ch.epfl.bluebrain.nexus.delta.kernel.effect.migration._ | ||
import ch.epfl.bluebrain.nexus.delta.rdf.Vocabulary.schemas | ||
import ch.epfl.bluebrain.nexus.delta.rdf.jsonld.context.RemoteContextResolution | ||
import ch.epfl.bluebrain.nexus.delta.rdf.utils.JsonKeyOrdering | ||
import ch.epfl.bluebrain.nexus.delta.routes.ResourcesTrialRoutes.SchemaInput._ | ||
import ch.epfl.bluebrain.nexus.delta.routes.ResourcesTrialRoutes.{GenerateSchema, GenerationInput} | ||
import ch.epfl.bluebrain.nexus.delta.sdk.SchemaResource | ||
import ch.epfl.bluebrain.nexus.delta.sdk.acls.AclCheck | ||
import ch.epfl.bluebrain.nexus.delta.sdk.circe.CirceUnmarshalling | ||
import ch.epfl.bluebrain.nexus.delta.sdk.directives.DeltaDirectives._ | ||
import ch.epfl.bluebrain.nexus.delta.sdk.directives.{AuthDirectives, DeltaSchemeDirectives} | ||
import ch.epfl.bluebrain.nexus.delta.sdk.identities.Identities | ||
import ch.epfl.bluebrain.nexus.delta.sdk.identities.model.Caller | ||
import ch.epfl.bluebrain.nexus.delta.sdk.marshalling.RdfMarshalling | ||
import ch.epfl.bluebrain.nexus.delta.sdk.model.IdSegment.IriSegment | ||
import ch.epfl.bluebrain.nexus.delta.sdk.model.{BaseUri, IdSegment} | ||
import ch.epfl.bluebrain.nexus.delta.sdk.permissions.Permissions.resources.{write => Write} | ||
import ch.epfl.bluebrain.nexus.delta.sdk.resources.NexusSource.DecodingOption | ||
import ch.epfl.bluebrain.nexus.delta.sdk.resources.model.ResourceRejection | ||
import ch.epfl.bluebrain.nexus.delta.sdk.resources.{NexusSource, ResourcesTrial} | ||
import ch.epfl.bluebrain.nexus.delta.sdk.schemas.Schemas | ||
import ch.epfl.bluebrain.nexus.delta.sdk.schemas.model.SchemaRejection | ||
import ch.epfl.bluebrain.nexus.delta.sourcing.model.ProjectRef | ||
import io.circe.generic.extras.Configuration | ||
import io.circe.generic.extras.semiauto.deriveConfiguredDecoder | ||
import io.circe.{Decoder, Json} | ||
import monix.bio.IO | ||
import monix.execution.Scheduler | ||
|
||
import scala.annotation.nowarn | ||
|
||
/** | ||
* The resource trial routes allowing to do read-only operations on resources | ||
*/ | ||
final class ResourcesTrialRoutes( | ||
identities: Identities, | ||
aclCheck: AclCheck, | ||
generateSchema: GenerateSchema, | ||
resourcesTrial: ResourcesTrial, | ||
schemeDirectives: DeltaSchemeDirectives | ||
)(implicit | ||
baseUri: BaseUri, | ||
s: Scheduler, | ||
cr: RemoteContextResolution, | ||
ordering: JsonKeyOrdering, | ||
decodingOption: DecodingOption | ||
) extends AuthDirectives(identities, aclCheck) | ||
with CirceUnmarshalling | ||
with RdfMarshalling { | ||
|
||
import schemeDirectives._ | ||
def routes: Route = | ||
baseUriPrefix(baseUri.prefix) { | ||
concat(validateRoute, generateRoute) | ||
} | ||
|
||
private def validateRoute: Route = | ||
pathPrefix("resources") { | ||
extractCaller { implicit caller => | ||
resolveProjectRef.apply { project => | ||
authorizeFor(project, Write).apply { | ||
(get & idSegment & idSegmentRef & pathPrefix("validate") & pathEndOrSingleSlash) { (schema, id) => | ||
val schemaOpt = underscoreToOption(schema) | ||
emit( | ||
resourcesTrial.validate(id, project, schemaOpt).leftWiden[ResourceRejection] | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private def generateRoute: Route = | ||
(get & pathPrefix("trial") & pathPrefix("resources")) { | ||
extractCaller { implicit caller => | ||
(resolveProjectRef & pathEndOrSingleSlash) { project => | ||
authorizeFor(project, Write).apply { | ||
(entity(as[GenerationInput])) { input => | ||
generate(project, input) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Call the generate method matching the schema input | ||
private def generate(project: ProjectRef, input: GenerationInput)(implicit caller: Caller) = | ||
input.schema match { | ||
case ExistingSchema(schemaId) => | ||
emit(resourcesTrial.generate(project, schemaId, input.resource).flatMap(_.asJson)) | ||
case NewSchema(schemaSource) => | ||
emit( | ||
generateSchema(project, schemaSource, caller).flatMap { schema => | ||
resourcesTrial.generate(project, schema, input.resource).flatMap(_.asJson) | ||
} | ||
) | ||
} | ||
|
||
} | ||
|
||
object ResourcesTrialRoutes { | ||
|
||
type GenerateSchema = (ProjectRef, Json, Caller) => IO[SchemaRejection, SchemaResource] | ||
|
||
sealed private[routes] trait SchemaInput extends Product | ||
|
||
private[routes] object SchemaInput { | ||
|
||
// Validate the generated resource with an existing schema | ||
final case class ExistingSchema(id: IdSegment) extends SchemaInput | ||
|
||
// Validate the generated resource with the new schema bundled in the request | ||
final case class NewSchema(json: Json) extends SchemaInput | ||
|
||
implicit val schemaInputDecoder: Decoder[SchemaInput] = | ||
Decoder.instance { hc => | ||
val value = hc.value | ||
val existingSchema = value.asString.map { s => ExistingSchema(IdSegment(s)) } | ||
val newSchema = NewSchema(value) | ||
Right(existingSchema.getOrElse(newSchema)) | ||
} | ||
} | ||
|
||
private val noSchema = ExistingSchema(IriSegment(schemas.resources)) | ||
|
||
final private[routes] case class GenerationInput(schema: SchemaInput = noSchema, resource: NexusSource) | ||
|
||
private[routes] object GenerationInput { | ||
@nowarn("cat=unused") | ||
implicit def generationInputDecoder(implicit decodingOption: DecodingOption): Decoder[GenerationInput] = { | ||
implicit val configuration: Configuration = Configuration.default.withDefaults | ||
implicit val nexusSourceDecoder: Decoder[NexusSource] = NexusSource.nexusSourceDecoder | ||
deriveConfiguredDecoder[GenerationInput] | ||
} | ||
} | ||
|
||
def apply( | ||
identities: Identities, | ||
aclCheck: AclCheck, | ||
schemas: Schemas, | ||
resourcesTrial: ResourcesTrial, | ||
schemeDirectives: DeltaSchemeDirectives | ||
)(implicit | ||
baseUri: BaseUri, | ||
s: Scheduler, | ||
cr: RemoteContextResolution, | ||
ordering: JsonKeyOrdering, | ||
decodingOption: DecodingOption | ||
): ResourcesTrialRoutes = | ||
new ResourcesTrialRoutes( | ||
identities, | ||
aclCheck, | ||
(project, source, caller) => schemas.createDryRun(project, source)(caller).toBIO[SchemaRejection], | ||
resourcesTrial, | ||
schemeDirectives | ||
) | ||
} |
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
81 changes: 81 additions & 0 deletions
81
delta/app/src/main/scala/ch/epfl/bluebrain/nexus/delta/wiring/ResourcesTrialModule.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,81 @@ | ||
package ch.epfl.bluebrain.nexus.delta.wiring | ||
|
||
import cats.effect.Clock | ||
import ch.epfl.bluebrain.nexus.delta.Main.pluginsMinPriority | ||
import ch.epfl.bluebrain.nexus.delta.kernel.utils.UUIDF | ||
import ch.epfl.bluebrain.nexus.delta.rdf.jsonld.api.JsonLdApi | ||
import ch.epfl.bluebrain.nexus.delta.rdf.jsonld.context.RemoteContextResolution | ||
import ch.epfl.bluebrain.nexus.delta.rdf.utils.JsonKeyOrdering | ||
import ch.epfl.bluebrain.nexus.delta.routes.ResourcesTrialRoutes | ||
import ch.epfl.bluebrain.nexus.delta.sdk.PriorityRoute | ||
import ch.epfl.bluebrain.nexus.delta.sdk.acls.AclCheck | ||
import ch.epfl.bluebrain.nexus.delta.sdk.directives.DeltaSchemeDirectives | ||
import ch.epfl.bluebrain.nexus.delta.sdk.identities.Identities | ||
import ch.epfl.bluebrain.nexus.delta.sdk.model.BaseUri | ||
import ch.epfl.bluebrain.nexus.delta.sdk.projects.FetchContext | ||
import ch.epfl.bluebrain.nexus.delta.sdk.projects.FetchContext.ContextRejection | ||
import ch.epfl.bluebrain.nexus.delta.sdk.resolvers.ResolverContextResolution | ||
import ch.epfl.bluebrain.nexus.delta.sdk.resources.model.ResourceRejection.ProjectContextRejection | ||
import ch.epfl.bluebrain.nexus.delta.sdk.resources.{Resources, ResourcesConfig, ResourcesTrial, ValidateResource} | ||
import ch.epfl.bluebrain.nexus.delta.sdk.schemas.Schemas | ||
import distage.ModuleDef | ||
import izumi.distage.model.definition.Id | ||
import monix.bio.UIO | ||
import monix.execution.Scheduler | ||
|
||
/** | ||
* Resources trial wiring | ||
*/ | ||
object ResourcesTrialModule extends ModuleDef { | ||
|
||
make[ResourcesTrial].from { | ||
( | ||
resources: Resources, | ||
validate: ValidateResource, | ||
fetchContext: FetchContext[ContextRejection], | ||
contextResolution: ResolverContextResolution, | ||
api: JsonLdApi, | ||
clock: Clock[UIO], | ||
uuidF: UUIDF | ||
) => | ||
ResourcesTrial( | ||
resources.fetch(_, _, None), | ||
validate, | ||
fetchContext.mapRejection(ProjectContextRejection), | ||
contextResolution | ||
)(api, clock, uuidF) | ||
} | ||
|
||
make[ResourcesTrialRoutes].from { | ||
( | ||
identities: Identities, | ||
aclCheck: AclCheck, | ||
schemas: Schemas, | ||
resourcesTrial: ResourcesTrial, | ||
schemeDirectives: DeltaSchemeDirectives, | ||
baseUri: BaseUri, | ||
s: Scheduler, | ||
cr: RemoteContextResolution @Id("aggregate"), | ||
ordering: JsonKeyOrdering, | ||
config: ResourcesConfig | ||
) => | ||
ResourcesTrialRoutes( | ||
identities, | ||
aclCheck, | ||
schemas, | ||
resourcesTrial, | ||
schemeDirectives | ||
)( | ||
baseUri, | ||
s, | ||
cr, | ||
ordering, | ||
config.decodingOption | ||
) | ||
} | ||
|
||
many[PriorityRoute].add { (route: ResourcesTrialRoutes) => | ||
PriorityRoute(pluginsMinPriority - 1, route.routes, requiresStrictEntity = true) | ||
} | ||
|
||
} |
Oops, something went wrong.