Skip to content

Commit

Permalink
make sparql_client wasm-friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
pchampin committed Dec 4, 2024
1 parent a7f746e commit bfa1f37
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
3 changes: 2 additions & 1 deletion sparql_client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ sophia_iri.workspace = true
sophia_turtle.workspace = true
sophia_xml.workspace = true
quick-xml = "0.37"
reqwest = { version = "0.12", features = ["blocking", "json"] }
reqwest = { version = "0.12", features = ["json"] }
rio_turtle.workspace = true
rio_xml.workspace = true
serde.workspace = true
serde_json = "1.0"
thiserror.workspace = true
tokio.workspace = true

[dev-dependencies]
sophia_isomorphism.workspace = true
44 changes: 28 additions & 16 deletions sparql_client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
//! [Sophia]: https://docs.rs/sophia/
#![deny(missing_docs)]

use reqwest::{blocking::Client, Error as ReqwestError};
use reqwest::{Client, Error as ReqwestError};
use sophia_api::prelude::*;
use sophia_api::sparql::{
IntoQuery, Query as SparqlQuery, SparqlBindings, SparqlDataset, SparqlResult,
Expand Down Expand Up @@ -114,16 +114,8 @@ impl SparqlClient {
);
Ok(SparqlResult::Triples(it))
}
}

impl SparqlDataset for SparqlClient {
type BindingsTerm = StaticTerm;
type BindingsResult = Bindings;
type TriplesResult = Box<dyn Iterator<Item = Result<[StaticTerm; 3], Error>>>;
type SparqlError = Error;
type Query = Query;

fn query<Q>(&self, query: Q) -> Result<SparqlResult<Self>, Error>
async fn async_query<Q>(&self, query: Q) -> Result<SparqlResult<Self>, Error>
where
Q: IntoQuery<Query>,
{
Expand All @@ -135,7 +127,8 @@ impl SparqlDataset for SparqlClient {
.header("Content-type", "application/sparql-query")
.header("User-Agent", "Sophia SPARQL Client")
.body(query.borrow().0.to_string())
.send()?;
.send()
.await?;
let ctype = resp
.headers()
.get("content-type")
Expand All @@ -144,27 +137,27 @@ impl SparqlDataset for SparqlClient {
.next()
.unwrap();
match ctype {
"application/sparql-results+json" => match resp.json::<ResultsDocument>()? {
"application/sparql-results+json" => match resp.json::<ResultsDocument>().await? {
ResultsDocument::Boolean { boolean, .. } => Ok(SparqlResult::Boolean(boolean)),
ResultsDocument::Bindings { doc } => Ok(SparqlResult::Bindings(doc)),
},
"application/sparql-results+xml" => {
let body: Cursor<Vec<u8>> = Cursor::new(resp.bytes()?.into());
let body: Cursor<Vec<u8>> = Cursor::new(resp.bytes().await?.into());
match ResultsDocument::from_xml(body)? {
ResultsDocument::Boolean { boolean, .. } => Ok(SparqlResult::Boolean(boolean)),
ResultsDocument::Bindings { doc } => Ok(SparqlResult::Bindings(doc)),
}
}
"text/turtle" => {
let body: Cursor<Vec<u8>> = Cursor::new(resp.bytes()?.into());
let body: Cursor<Vec<u8>> = Cursor::new(resp.bytes().await?.into());
Self::wrap_triple_source(turtle::parse_bufread(body))
}
"application/n-triples" => {
let body: Cursor<Vec<u8>> = Cursor::new(resp.bytes()?.into());
let body: Cursor<Vec<u8>> = Cursor::new(resp.bytes().await?.into());
Self::wrap_triple_source(nt::parse_bufread(body))
}
"application/rdf+xml" => {
let body: Cursor<Vec<u8>> = Cursor::new(resp.bytes()?.into());
let body: Cursor<Vec<u8>> = Cursor::new(resp.bytes().await?.into());
Self::wrap_triple_source(rdfxml::parse_bufread(body))
}
_ => Err(Error::Unsupported(format!(
Expand All @@ -175,6 +168,25 @@ impl SparqlDataset for SparqlClient {
}
}

impl SparqlDataset for SparqlClient {
type BindingsTerm = StaticTerm;
type BindingsResult = Bindings;
type TriplesResult = Box<dyn Iterator<Item = Result<[StaticTerm; 3], Error>>>;
type SparqlError = Error;
type Query = Query;

fn query<Q>(&self, query: Q) -> Result<SparqlResult<Self>, Error>
where
Q: IntoQuery<Query>,
{
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.expect("Could not build tokio runtime");
rt.block_on(self.async_query(query))
}
}

impl SparqlBindings<SparqlClient> for Bindings {
fn variables(&self) -> Vec<&str> {
self.head
Expand Down

0 comments on commit bfa1f37

Please sign in to comment.