Skip to content

Commit

Permalink
Merge pull request #169 from RDFLib/pr_sparql_post_request
Browse files Browse the repository at this point in the history
Allows the use of the Prez /sparql endpoint using POST method
  • Loading branch information
hjohns authored Jan 17, 2025
2 parents fc6f2e8 + 9f6f31b commit 3387511
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 16 deletions.
8 changes: 4 additions & 4 deletions src/components/search/CatPrezSearchMap.vue
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ type SparqlBinding = {
const apiBaseUrl = inject(apiBaseUrlConfigKey) as string;
const { loading: catalogLoading, error: catalogError, apiGetRequest: catalogApiGetRequest } = useApiRequest();
const { loading: themesLoading, error: themesError, sparqlGetRequest: themesSparqlGetRequest } = useSparqlRequest();
const { loading: searchLoading, error: searchError, sparqlGetRequest: searchSparqlGetRequest } = useSparqlRequest();
const { loading: themesLoading, error: themesError, sparqlGetRequest: themesSparqlGetRequest, sparqlPostRequest: themesSparqlPostRequest } = useSparqlRequest();
const { loading: searchLoading, error: searchError, sparqlGetRequest: searchSparqlGetRequest, sparqlPostRequest: searchSparqlPostRequest } = useSparqlRequest();
const { store, parseIntoStore, qnameToIri } = useRdfStore();
const LIVE_SEARCH = true;
Expand Down Expand Up @@ -135,7 +135,7 @@ async function getCatalogs() {
* Gets a list of themes from a SPARQL query from the API & creates the list of theme options
*/
async function getThemes() {
const themesData = await themesSparqlGetRequest(`${apiBaseUrl}/sparql`, getThemesQuery(selectedCatalogs.value));
const themesData = await themesSparqlPostRequest(`${apiBaseUrl}/sparql`, getThemesQuery(selectedCatalogs.value));
if (themesData && !themesError.value) {
themes.value = (themesData.results.bindings as SparqlBinding[]).map(result => {
return {
Expand All @@ -150,7 +150,7 @@ async function getThemes() {
* Performs search via a SPARQL query
*/
async function doSearch() {
const searchData = await searchSparqlGetRequest(`${apiBaseUrl}/sparql`, query.value);
const searchData = await searchSparqlPostRequest(`${apiBaseUrl}/sparql`, query.value);
if (searchData && !searchError.value) {
results.value = (searchData.results.bindings as SparqlBinding[]).map(result => {
return {
Expand Down
4 changes: 2 additions & 2 deletions src/components/search/SpacePrezSearchMap.vue
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const mapConfig = inject(mapConfigKey) as MapConfig;
const { loading: datasetLoading, error: datasetError, apiGetRequest: datasetApiGetRequest } = useApiRequest(); // list of datasets
const { loading: fcLoading, hasError: fcError, concurrentApiRequests: fcConcurrentApiRequests } = useConcurrentApiRequests(); // concurrent lists of feature collections
const { loading: searchLoading, error: searchError, sparqlGetRequest: searchSparqlGetRequest } = useSparqlRequest(); // spatial search SPARQL query
const { loading: searchLoading, error: searchError, sparqlGetRequest: searchSparqlGetRequest, sparqlPostRequest: searchSparqlPostRequest } = useSparqlRequest(); // spatial search SPARQL query
const { store, parseIntoStore, qnameToIri } = useRdfStore();
const LIVE_SEARCH = true;
Expand Down Expand Up @@ -208,7 +208,7 @@ async function getDatasets() {
async function doSearch() {
if (shape.value.coords.length > 0) {
const searchData = await searchSparqlGetRequest(`${apiBaseUrl}/sparql`, query.value);
const searchData = await searchSparqlPostRequest(`${apiBaseUrl}/sparql`, query.value);
if (searchData && !searchError.value) {
results.value = (searchData.results.bindings as SparqlBinding[]).map(result => {
return {
Expand Down
43 changes: 33 additions & 10 deletions src/composables/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,23 +253,45 @@ export function useSparqlRequest() {
* @returns data
*/
async function sparqlGetRequest(url: string, query: string) {
return await sparqlRequest(url, query, "GET");
};

/**
* Perform an async SPARQL POST request
*
* @param path
* @returns data
*/
async function sparqlPostRequest(url: string, query: string) {
return await sparqlRequest(url, query, "POST");
};

async function sparqlRequest(url: string, query: string, method: string) {
loading.value = true;
let data: any = "";
let isGraphQuery = ["CONSTRUCT", "DESCRIBE"].some(e => query.includes(e));

let query_url = "";
let query_body = undefined;
let headers: any = {
"Accept": isGraphQuery ? "text/turtle" : "application/sparql-results+json"
};
try {
const r = await fetch(`${url}?query=${encodeURIComponent(query)}`, {
method: "GET",
headers: {
"Accept": isGraphQuery ? "text/turtle" : "application/sparql-results+json"
}
if (method === "GET") {
query_url =`${url}?query=${encodeURIComponent(query)}`
} else {
query_url = url;
query_body = `query=${encodeURIComponent(query)}`;
headers["Content-Type"] = "application/x-www-form-urlencoded";
}
const r = await fetch(query_url, {
method: method,
headers: headers,
body: query_body
});

if (!r.ok) {
throw new NetworkError(`Network error - status code ${r.status}: ${r.statusText}`);
}



data = isGraphQuery ? await r.text() : await r.json();
} catch (e) {
if (e instanceof TypeError) { // TypeError - fetch error
Expand All @@ -288,6 +310,7 @@ export function useSparqlRequest() {
return {
loading,
error,
sparqlGetRequest
sparqlGetRequest,
sparqlPostRequest
};
};

0 comments on commit 3387511

Please sign in to comment.