Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#125 refactor sparql js #150

Merged
merged 6 commits into from
Mar 25, 2022
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions app/src/modules/sparql.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@ async function querySparql (query, endpoint = SPARQL_ENDPOINT) {
}
return await fetch(urlEncodedQuery, requestOptions)
.then(async (res) => {
if (!res.ok) {
throw new Error(`HTTP error. Status: ${res.status} ${res.statusText}`)
}
const results = await res.json()
return results
})
.catch((err) => console.log(err))
.catch((err) => {
throw new Error('Error retrieving query response', { cause: err })
})
}

function parseSparql (response) {
const queryResults = []
if (response) {
try {
for (const row of response.results.bindings) {
const rowData = {}
queryResults.push(rowData)
Expand All @@ -34,8 +39,14 @@ function parseSparql (response) {
rowData[field] = value
})
}
} catch (err) {
throw new Error('Error parsing response', { cause: err })
roryschadler marked this conversation as resolved.
Show resolved Hide resolved
}
return queryResults
}

export { querySparql, parseSparql }
async function queryAndParseSparql (query, endpoint = SPARQL_ENDPOINT) {
return parseSparql(await querySparql(query, endpoint))
}

export { querySparql, parseSparql, queryAndParseSparql } // queryAndParseSparql as default ?