Skip to content

Commit

Permalink
Feature: Detect Query Kind
Browse files Browse the repository at this point in the history
Fixes #28
  • Loading branch information
Hofstetter Benjamin (extern) committed Sep 8, 2023
1 parent 1b5f63c commit c39012a
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/extension/sparql-client/simple-client.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -20,21 +18,22 @@ export class SparqlClient {
const params = new URLSearchParams();
params.append("query", sparqlQuery);

const queryKind = getSPARQLQueryKind(sparqlQuery);
const abortController = new AbortController();

const options: AxiosRequestConfig = {
headers: {
// 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);

});
Expand Down
69 changes: 69 additions & 0 deletions src/extension/sparql-client/sparql-utils.ts
Original file line number Diff line number Diff line change
@@ -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');
}
}

0 comments on commit c39012a

Please sign in to comment.