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

Allows the use of the Prez /sparql endpoint using POST method #169

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
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
};
};
Loading