diff --git a/src/extension/sparql-client/simple-client.ts b/src/extension/sparql-client/simple-client.ts index 510dabb..0b0aa33 100644 --- a/src/extension/sparql-client/simple-client.ts +++ b/src/extension/sparql-client/simple-client.ts @@ -1,14 +1,12 @@ import axios, { AxiosInstance, AxiosRequestConfig } from 'axios'; +import { getSPARQLQueryKind, getAcceptHeader } from './sparql-utils'; export class SparqlClient { private endpoint: AxiosInstance; - // "https://int.lindas.admin.ch/query" constructor(endpointUrl: string, user: string, password: string) { this.endpoint = axios.create({ baseURL: endpointUrl, - // timeout: 1000, - // headers: { "X-Custom-Header": "foobar" }, auth: { username: user, password: password, @@ -20,6 +18,7 @@ export class SparqlClient { const params = new URLSearchParams(); params.append("query", sparqlQuery); + const queryKind = getSPARQLQueryKind(sparqlQuery); const abortController = new AbortController(); const options: AxiosRequestConfig = { @@ -27,14 +26,14 @@ export class SparqlClient { // eslint-disable-next-line @typescript-eslint/naming-convention 'Content-Type': 'application/x-www-form-urlencoded', // eslint-disable-next-line @typescript-eslint/naming-convention - Accept: 'application/sparql-results+json,text/turtle', + Accept: getAcceptHeader(queryKind), }, signal: abortController.signal }; if (execution) { execution.token.onCancellationRequested((_: any) => { console.warn('Request cancelled'); - const k = abortController.abort(); + abortController.abort(); console.log('kill', abortController); }); diff --git a/src/extension/sparql-client/sparql-utils.ts b/src/extension/sparql-client/sparql-utils.ts new file mode 100644 index 0000000..088aaa2 --- /dev/null +++ b/src/extension/sparql-client/sparql-utils.ts @@ -0,0 +1,69 @@ +export enum SPARQLQueryKind { + select = 'select', + construct = 'construct', + describe = 'describe', + ask = 'ask', + update = 'update', + load = 'load', + clear = 'clear', + drop = 'drop', + create = 'create', + json = 'json'// fuski has that +} + +export function getSPARQLQueryKind(query: string): SPARQLQueryKind { + const lines = query.split('\n').map(line => line.trim().toLowerCase()); + const linesWithoutCommentsAndEmpty = lines.filter(line => line !== '' && !line.startsWith('#')); + const linesWithoutPrefix = linesWithoutCommentsAndEmpty.filter(line => { + return !line.startsWith('prefix'); + }); + const firstLine = linesWithoutPrefix[0]; + if (firstLine !== undefined) { + if (firstLine.startsWith(SPARQLQueryKind.select)) { + return SPARQLQueryKind.select; + } else if (firstLine.startsWith(SPARQLQueryKind.construct)) { + return SPARQLQueryKind.construct; + } else if (firstLine.startsWith(SPARQLQueryKind.describe)) { + return SPARQLQueryKind.describe; + } else if (firstLine.startsWith(SPARQLQueryKind.ask)) { + return SPARQLQueryKind.ask; + } else if (firstLine.startsWith(SPARQLQueryKind.update)) { + return SPARQLQueryKind.update; + } else if (firstLine.startsWith(SPARQLQueryKind.load)) { + return SPARQLQueryKind.load; + } else if (firstLine.startsWith(SPARQLQueryKind.clear)) { + return SPARQLQueryKind.clear; + } else if (firstLine.startsWith(SPARQLQueryKind.drop)) { + return SPARQLQueryKind.drop; + } else if (firstLine.startsWith(SPARQLQueryKind.create)) { + return SPARQLQueryKind.create; + } else if (firstLine.startsWith(SPARQLQueryKind.json)) { + return SPARQLQueryKind.json; + } + } + throw new Error('Unknown query type'); +} + + + + +export function getAcceptHeader(queryType: SPARQLQueryKind): string { + switch (queryType) { + case SPARQLQueryKind.ask: + case SPARQLQueryKind.select: + return 'application/sparql-results+json'; + case SPARQLQueryKind.construct: + case SPARQLQueryKind.describe: + return 'text/turtle'; + case SPARQLQueryKind.update: + case SPARQLQueryKind.load: + case SPARQLQueryKind.clear: + case SPARQLQueryKind.drop: + case SPARQLQueryKind.create: + return '*/*'; + case SPARQLQueryKind.json: + return 'application/json'; + default: + throw new Error('Unknown query type'); + } +} \ No newline at end of file