Skip to content

Instantly share code, notes, and snippets.

@TomLous
Last active December 23, 2019 14:52
Show Gist options
  • Save TomLous/0f096653cd7c953e0a5f4b4e5e8ce26c to your computer and use it in GitHub Desktop.
Save TomLous/0f096653cd7c953e0a5f4b4e5e8ce26c to your computer and use it in GitHub Desktop.
val selectAllQuery = SparQLQuery("SELECT ?s ?p ?o WHERE {?s ?p ?0 .}")
// ???
for{
config <- [load config]
connection <- [connect using config]
json <- [query using connection]
}
import org.json4s.jackson.Json
case class SparQLResult(data: Json)
case class SparQLQuery(query: String)
import pureconfig._
import pureconfig.generic.auto._
import zio._
trait Config {
val config: Config.Service[Any]
}
object Config {
case class ConfigError(message: String) extends RuntimeException(message)
case class JenaApiConfig(destination:String, queryEndpoint: String)
trait Service[R] {
def config: ZIO[R, Throwable, JenaApiConfig]
}
trait Live extends Config {
val config: Config.Service[Any] = new Service[Any] {
override def config: Task[JenaApiConfig] =
ZIO
.fromEither(ConfigSource.default.load[JenaApiConfig])
.mapError(e => ConfigError(e.toList.mkString(", ")))
}
}
object factory extends Config.Service[Config] {
override def config: ZIO[Config, Throwable, JenaApiConfig] = ZIO.accessM[Config](_.config.config)
}
}
import Config.JenaApiConfig
import org.apache.jena.rdfconnection._
import zio._, console._, stream._, blocking.{Blocking, effectBlocking}
trait import Config.JenaApiConfig
import org.apache.jena.rdfconnection._
import zio._, console._, stream._, blocking.{Blocking, effectBlocking}
trait PureRDFConnection extends Serializable {
val rdfConnection: PureRDFConnection.Service[Any]
}
object PureRDFConnection {
trait Service[R] {
def runQuery(query: SparQLQuery): ZIO[R, Throwable, SparQLResult]
def connect(conf: JenaApiConfig): ZManaged[R, Throwable, RDFConnection]
}
// object > extends Service[PureRDFConnection] {
// override def runQuery(query: SparQLQuery): ZIO[PureRDFConnection, Throwable, SparQLResult] = ZIO
// .environment[PureRDFConnection]
// .flatMap(_.rdfConnection.runQuery(query))
//
// override def connect(conf: JenaApiConfig): ZManaged[PureRDFConnection, Throwable, RDFConnection] =
// ZManaged
// .environment[PureRDFConnection]
// .flatMap(_.rdfConnection.connect(conf))
// }
trait Live extends PureRDFConnection {
val rdfConnection: Service[Any] = new Service[Any] {
override def runQuery(query: SparQLQuery): ZIO[Any, Throwable, SparQLResult] = for {
conn <- ZIO.environment[RDFConnection]
res <- conn.queryResultSet(query.query,ResultSetFormatter.out)
json <- SparQLResult.parse(res)
} yield json
override def connect(conf: JenaApiConfig): ZManaged[Any, Throwable, RDFConnection] = Managed
.makeEffect(
RDFConnectionRemote
.create()
.destination(conf.destination)
.queryEndpoint(conf.queryEndpoint)
.build()
)(
_.close()
)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment