Skip to content

Commit

Permalink
feat: use post request instead of get
Browse files Browse the repository at this point in the history
  • Loading branch information
SmallhillCZ committed Jun 11, 2024
1 parent cddd629 commit 060ecbd
Showing 1 changed file with 18 additions and 27 deletions.
45 changes: 18 additions & 27 deletions catalog/src/app/services/sparql.service.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,52 @@
import { Injectable, Query } from '@angular/core';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';

import { ConfigService } from './config.service';

import { QueryDefinitionPrefixes } from 'app/lib/sparql-builder';

export interface SparqlResult<T extends { [key: string]: any }> {
head: { link: string[], vars: string[] };
head: { link: string[]; vars: string[] };

results: {
distinct: boolean,
ordered: boolean,
distinct: boolean;
ordered: boolean;
bindings: {
[K in keyof T]: {
type: string;
value: T[K];
datatype?: string;
"xml:lang"?: string;
}
'xml:lang'?: string;
};
}[];
}

};
}

export interface DocumentFields {
field: string;
value: any;
}


@Injectable({
providedIn: 'root'
providedIn: 'root',
})
export class SparqlService {

constructor(
private configService: ConfigService,
private http: HttpClient,
) {
}


constructor(private configService: ConfigService, private http: HttpClient) {}

async query<T>(query: string): Promise<T[]> {
const response = await this.rawQuery<any>(query)
const response = await this.rawQuery<any>(query);

return response.results.bindings.map(doc => {
return response.results.bindings.map((doc) => {
return Object.entries(doc).reduce((acc, [key, value]) => {
acc[key] = value.value;
return acc;
}, {} as any);
});

}

rawQuery<T>(query: string) {
return this.http.get<SparqlResult<T>>(this.configService.config.endpoint, { params: { query }, headers: { "Accept": "application/json" } }).toPromise()
rawQuery<T extends { [key: string]: any }>(query: string) {
const headers = {
Accept: 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
};
return this.http.post<SparqlResult<T>>(this.configService.config.endpoint, { query }, { headers }).toPromise();
}

}

0 comments on commit 060ecbd

Please sign in to comment.