Skip to content

Commit

Permalink
Created final Swagger model for complex classes
Browse files Browse the repository at this point in the history
  • Loading branch information
ulitol97 committed Mar 3, 2022
1 parent d7d87c9 commit a49531f
Show file tree
Hide file tree
Showing 24 changed files with 773 additions and 1,073 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import es.weso.rdfshape.server.api.routes.ApiService
import org.http4s.client.Client
import org.http4s.dsl.Http4sDsl
import org.http4s.rho.RhoRoutes
import org.http4s.rho.swagger.syntax.io._

/** API service to handle multiple general tasks (server status, etc.)
*
Expand All @@ -17,9 +18,10 @@ class BaseService(client: Client[IO]) extends Http4sDsl[IO] with ApiService {
/** Describe the API routes handled by this service and the actions performed on each of them
*/
val routes: RhoRoutes[IO] = new RhoRoutes[IO] {
GET / "health" |>> { () =>
Ok("Healthy")
}
"Perform a request to check if the API is up and running" **
GET / "health" |>> { () =>
Ok("Healthy")
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ import es.weso.rdf.{PrefixMap, RDFReasoner}
import es.weso.rdfshape.server.api.format.dataFormats.DataFormat
import es.weso.rdfshape.server.api.routes.data.logic.DataSource._
import es.weso.rdfshape.server.api.routes.data.logic.types.merged.DataCompound
import es.weso.rdfshape.server.api.utils.parameters.IncomingRequestParameters.{
CompoundDataParameter,
SourceParameter
}
import es.weso.rdfshape.server.api.utils.parameters.PartsMap
import es.weso.rdfshape.server.api.utils.parameters.IncomingRequestParameters.SourceParameter
import io.circe.{Decoder, DecodingFailure, Encoder, HCursor}

/** Common trait to all data, whichever its nature (single, compound, endpoint...)
Expand Down Expand Up @@ -94,22 +90,6 @@ object Data extends DataCompanion[Data] {
case _ => DecodingFailure(s"Invalid data source '$source'", Nil).asLeft
}
} yield decoded

/** General implementation delegating on subclasses
*/
override def mkData(partsMap: PartsMap): IO[Either[String, Data]] = for {
// 1. Make some checks on the parameters to distinguish between Data types
compoundData <- partsMap.optPartValue(CompoundDataParameter.name)

// 2. Delegate on the correct sub-class for creating the Data
maybeData <- {
// 1. Compound data
if(compoundData.isDefined) DataCompound.mkData(partsMap)
// 2. Simple data or unknown
else DataSingle.mkData(partsMap)
}

} yield maybeData
}

/** Static utilities to be used with [[Data]] representations
Expand All @@ -125,11 +105,4 @@ private[data] trait DataCompanion[D <: Data] extends LazyLogging {
/** Decoder used to extract [[Data]] instances from JSON values
*/
implicit val decode: Decoder[Either[String, D]]

/** Given a request's parameters, try to extract an instance of [[Data]] (type [[D]]) from them
*
* @param partsMap Request's parameters
* @return Either the [[Data]] instance or an error message
*/
def mkData(partsMap: PartsMap): IO[Either[String, D]]
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ import com.typesafe.scalalogging.LazyLogging
import es.weso.rdf.jena._
import es.weso.rdf.nodes.IRI
import es.weso.rdf.{InferenceEngine, NONE}
import es.weso.rdfshape.server.api.definitions.ApiDefaults
import es.weso.rdfshape.server.api.format.dataFormats.{DataFormat, RdfFormat}
import es.weso.rdfshape.server.api.routes.data.logic.DataSource
import es.weso.rdfshape.server.api.routes.data.logic.DataSource.DataSource
import es.weso.rdfshape.server.api.routes.data.logic.aux.InferenceCodecs._
import es.weso.rdfshape.server.api.utils.parameters.IncomingRequestParameters._
import es.weso.rdfshape.server.api.utils.parameters.PartsMap
import es.weso.rdfshape.server.html2rdf.HtmlToRdf
import es.weso.rdfshape.server.utils.networking.NetworkingUtils.getUrlContents
import io.circe._
Expand Down Expand Up @@ -134,10 +132,11 @@ private[api] object DataSingle
.downField(FormatParameter.name)
.as[Either[String, RdfFormat]]

// If missing, use none
inference <-
cursor
.downField(InferenceParameter.name)
.as[InferenceEngine]
.as[Option[InferenceEngine]]

source <- cursor
.downField(SourceParameter.name)
Expand All @@ -152,53 +151,11 @@ private[api] object DataSingle
for {
format <- maybeFormat
data <- Try {
DataSingle(content, format, inference, source)
DataSingle(content, format, inference.getOrElse(NONE), source)
}.toEither.leftMap(err =>
s"Could not build the schema from user data:\n ${err.getMessage}"
)
} yield data
}
}

override def mkData(partsMap: PartsMap): IO[Either[String, DataSingle]] =
for {
// Data param as sent by client
paramData <- partsMap
.optPartValue(DataParameter.name)
.map(_.getOrElse(""))
paramFormat <- DataFormat.fromRequestParams(
DataFormatParameter.name,
partsMap
)

paramInference <- partsMap.optPartValue(InferenceParameter.name)

paramDataSource <- partsMap.optPartValue(DataSourceParameter.name)

// Confirm final format and inference
inference = getInference(paramInference).getOrElse(NONE)
format = paramFormat.getOrElse(ApiDefaults.defaultDataFormat)

// Check the client's selected source
dataSource = paramDataSource.getOrElse(DataSource.default)
_ = logger.debug(s"RDF Data received - Source: $dataSource")

// Create the data instance
data = DataSingle(
content = paramData,
format = format,
inference = inference,
source = dataSource
)

} yield data.fetchedContents.map(_ => data)

/** @param inferenceStr String representing the inference value
* @return Optionally, the inference contained in a given data string
*/
private def getInference(
inferenceStr: Option[String]
): Option[InferenceEngine] = {
inferenceStr.flatMap(InferenceEngine.fromString(_).toOption)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ import es.weso.rdfshape.server.api.routes.data.logic.types.{
DataCompanion,
DataSingle
}
import es.weso.rdfshape.server.api.utils.parameters.IncomingRequestParameters.{
CompoundDataParameter,
ContentParameter
}
import es.weso.rdfshape.server.api.utils.parameters.PartsMap
import es.weso.rdfshape.server.api.utils.parameters.IncomingRequestParameters.ContentParameter
import io.circe._
import io.circe.parser._
import io.circe.syntax._
Expand Down Expand Up @@ -90,22 +86,6 @@ private[api] object DataCompound
extends DataCompanion[DataCompound]
with LazyLogging {

override def mkData(partsMap: PartsMap): IO[Either[String, DataCompound]] = {
for {
// Parse params
compoundData <- partsMap.optPartValue(CompoundDataParameter.name)
// Try to create data
maybeData: Either[String, DataCompound] =
if(compoundData.isDefined) {
DataCompound
.fromJsonString(compoundData.get)
.leftMap(err => s"Could not read compound data.\n $err")
} else Left("No compound data provided")
/* Check if the created data is empty, then an error occurred when merging
* the elements */
} yield maybeData.flatMap(_.fetchedContents.flatMap(_ => maybeData))
}

/** Encoder used to transform CompoundData instances to JSON values
*/
override implicit val encode: Encoder[DataCompound] =
Expand Down
Loading

0 comments on commit a49531f

Please sign in to comment.