Skip to content

Commit fcc3db3

Browse files
committed
End refactoring
1 parent 0695bc9 commit fcc3db3

7 files changed

Lines changed: 34 additions & 184 deletions

File tree

modules/server/src/main/scala/es/weso/rdfshape/server/api/routes/endpoint/logic/Endpoint.scala

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package es.weso.rdfshape.server.api.routes.endpoint.logic
22

3-
import cats.data.EitherT
43
import cats.effect.IO
4+
import cats.implicits.catsSyntaxEitherId
55
import com.typesafe.scalalogging.LazyLogging
66
import es.weso.rdf.RDFReader
77
import es.weso.rdf.jena.{Endpoint => EndpointJena}
@@ -65,17 +65,17 @@ private[api] object Endpoint extends LazyLogging {
6565
*/
6666
def getEndpointUrl(
6767
partsMap: PartsMap
68-
): EitherT[IO, String, URL] = for {
69-
maybeStr <- EitherT.liftF[IO, String, Option[String]](
70-
partsMap.optPartValue("endpoint")
71-
)
72-
ep <- maybeStr match {
73-
case None =>
74-
EitherT.leftT[IO, URL](s"No value provided for parameter endpoint")
75-
case Some(str) =>
76-
Try(new URL(str)) match {
77-
case Success(url) => EitherT.rightT[IO, String](url)
78-
case Failure(ex) => EitherT.leftT[IO, URL](ex.getMessage)
68+
): IO[Either[String, URL]] = for {
69+
maybeStr <- partsMap.optPartValue("endpoint").map(_.toRight(""))
70+
71+
ep = maybeStr match {
72+
case Left(_) =>
73+
val msg = s"No value provided for parameter endpoint"
74+
msg.asLeft[URL]
75+
case Right(endpointStr) =>
76+
Try(new URL(endpointStr)) match {
77+
case Success(url) => url.asRight[String]
78+
case Failure(ex) => ex.getMessage.asLeft[URL]
7979
}
8080
}
8181
} yield ep

modules/server/src/main/scala/es/weso/rdfshape/server/api/routes/endpoint/service/EndpointService.scala

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@ import es.weso.rdfshape.server.api.definitions.ApiDefinitions.api
77
import es.weso.rdfshape.server.api.routes.ApiService
88
import es.weso.rdfshape.server.api.routes.endpoint.logic.Endpoint.{
99
getEndpointAsRDFReader,
10-
getEndpointInfo,
1110
getEndpointUrl
1211
}
13-
import es.weso.rdfshape.server.api.routes.endpoint.logic.EndpointStatus._
12+
import es.weso.rdfshape.server.api.routes.endpoint.logic.Outgoing
1413
import es.weso.rdfshape.server.api.routes.endpoint.logic.Outgoing.getOutgoing
1514
import es.weso.rdfshape.server.api.routes.endpoint.logic.query.SparqlQuery
1615
import es.weso.rdfshape.server.api.routes.endpoint.logic.query.SparqlQuery.mkSparqlQuery
17-
import es.weso.rdfshape.server.api.routes.endpoint.logic.{Endpoint, Outgoing}
1816
import es.weso.rdfshape.server.api.utils.parameters.IncomingRequestParameters.{
1917
EndpointParameter,
2018
LimitParameter,
@@ -56,12 +54,14 @@ class EndpointService(client: Client[IO])
5654
* - results [Object]: Query results
5755
* - bindings: [Array]: Query results, each item being an object mapping each variable to its value
5856
*/
57+
/**
58+
*/
5959
case req @ POST -> Root / `api` / `verb` / "query" =>
6060
req.decode[Multipart[IO]] { m =>
6161
val partsMap = PartsMap(m.parts)
6262

6363
val r: EitherT[IO, String, Json] = for {
64-
endpointUrl <- getEndpointUrl(partsMap)
64+
endpointUrl <- EitherT(getEndpointUrl(partsMap))
6565
endpoint <- getEndpointAsRDFReader(endpointUrl)
6666
either <- EitherT
6767
.liftF[IO, String, Either[
@@ -73,7 +73,6 @@ class EndpointService(client: Client[IO])
7373
eitherQuery <- EitherT.fromEither[IO](either)
7474

7575
json <- {
76-
7776
eitherQuery.rawQuery.fold(
7877
err => EitherT.left(IO.pure(err)),
7978
raw => {
@@ -97,37 +96,6 @@ class EndpointService(client: Client[IO])
9796
} yield resp
9897
}
9998

100-
/** Attempt to contact an endpoint and return metadata about it.
101-
* Receives a JSON object with the input endpoint:
102-
* - endpoint [String]: Target endpoint
103-
* Returns a JSON object with the endpoint response:
104-
* - head [Object]: Query metadata
105-
* - vars: [Array]: Query variables
106-
* - results [Object]: Query results
107-
* - bindings: [Array]: Query results, each item being an object mapping each variable to its value
108-
*/
109-
case req @ POST -> Root / `api` / `verb` / "info" =>
110-
req.decode[Multipart[IO]] { m =>
111-
val partsMap = PartsMap(m.parts)
112-
for {
113-
endpointUrl <- getEndpointUrl(partsMap).value
114-
response <- endpointUrl match {
115-
case Left(err) => errorResponseJson(err, BadRequest)
116-
case Right(endpointUrl) =>
117-
val endpointInfo = getEndpointInfo(endpointUrl)
118-
endpointInfo match {
119-
case Endpoint(errMsg, OFFLINE) =>
120-
errorResponseJson(
121-
errMsg,
122-
InternalServerError
123-
)
124-
case _ => Ok(endpointInfo.asJson)
125-
}
126-
}
127-
} yield response
128-
129-
}
130-
13199
/** Attempt to contact a wikibase endpoint and return the data (triplets) about a node in it.
132100
* Receives a JSON object with the input endpoint, node and limits:
133101
* - endpoint [String]: Target endpoint

modules/server/src/main/scala/es/weso/rdfshape/server/api/routes/wikibase/logic/operations/schema/WikibaseSchemaExtract.scala

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,13 @@ private[wikibase] case class WikibaseSchemaExtract(
7171
.fromString(strRdf, Turtle.name)
7272
.flatMap(
7373
_.use(rdf =>
74-
for {
75-
rdfSerialized <- rdf.serialize(Turtle.name)
76-
nodeSelector = RDFNodeSelector(IRI(entityUri))
77-
inferred <- SchemaInfer.runInferSchema(
78-
rdf,
79-
nodeSelector,
80-
Schemas.shEx.name,
81-
IRI(s"http://example.org/Shape_${wdEntity.localName}"),
82-
InferOptions.defaultOptions.copy(maxFollowOn = 3)
83-
)
84-
} yield inferred
74+
SchemaInfer.runInferSchema(
75+
rdf,
76+
RDFNodeSelector(IRI(entityUri)),
77+
Schemas.shEx.name,
78+
IRI(s"http://example.org/Shape_${wdEntity.localName}"),
79+
InferOptions.defaultOptions.copy(maxFollowOn = 3)
80+
)
8581
)
8682
)
8783
)

modules/server/src/main/scala/es/weso/rdfshape/server/api/routes/wikibase/service/WikibaseService.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ class WikibaseService(client: Client[IO])
274274
} yield response
275275
}
276276

277-
// TODO: Needs exhaustive testing and client changes
277+
// TODO: Needs exhaustive testing. Timeouts.
278278
/** Attempts to extract an schema (ShEx) from a given entity present in wikidata
279279
* using SheXer. See [[https://github.com/DaniFdezAlvarez/shexer]].
280280
* Receives an entity URI as payload.

modules/server/src/main/scala/es/weso/rdfshape/server/utils/networking/NetworkingUtils.scala

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ object NetworkingUtils extends LazyLogging {
1616
def getUrlContents(urlString: String): Either[String, String] = {
1717
Try {
1818
val url = new URL(urlString)
19+
getUrlContents(url)
20+
} match {
21+
case Failure(exception) => Left(exception.getMessage)
22+
case Success(value) => value
23+
}
24+
}
25+
26+
def getUrlContents(url: URL): Either[String, String] = {
27+
Try {
1928
val src = Source.fromURL(url)
2029
val str = src.mkString
2130
src.close()
@@ -24,7 +33,7 @@ object NetworkingUtils extends LazyLogging {
2433
case Success(urlContent) => Right(urlContent)
2534
case Failure(exception) =>
2635
val msg =
27-
s"Could not obtain data from url $urlString."
36+
s"Could not obtain data from url $url."
2837
logger.warn(s"$msg - ${exception.getMessage}")
2938
Left(msg)
3039
}

modules/server/src/test/scala/es/weso/rdfshape/server/html2rdf/HTML2RDFTest.scala

Lines changed: 0 additions & 123 deletions
This file was deleted.

website/static/favicon.ico

11.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)