rubensworks / fetch-sparql-endpoint.js

A simple, lightweight module to send queries to SPARQL endpoints and retrieve their results in a streaming fashion.
MIT License
21 stars 12 forks source link

Using additional URL parameters in SparqlEndpointFetcher #45

Closed ewsterrenburg closed 1 year ago

ewsterrenburg commented 2 years ago

I was trying to send some queries to a GraphDB endpoint that allows for some additional URL parameters that impact the results that are actually being returned ("Include inferred data in results" & "Expand results over owl:sameAs").

Would it be possible to add support for additional URL parameters to the library?

I tried to turn this into a pull-request, yet my TypeScript is very limited and I didn't get to a solution where the tests were actually running, yet see below what changes in the Javascript I'm talking about.

...
/**
 * A SparqlEndpointFetcher can send queries to SPARQL endpoints,
 * and retrieve and parse the results.
 */
class SparqlEndpointFetcher {
    constructor(args) {
        args = args || {};
        this.method = args.method || 'POST';
        this.additionalUrlParams = args.additionalUrlParams || {};
        this.fetchCb = args.fetch;
...
    async fetchRawStream(endpoint, query, acceptHeader) {
        let url = this.method === 'POST' ? endpoint : endpoint + '?query=' + encodeURIComponent(query);
        // Initiate request
        const headers = new Headers();
        let body;
        headers.append('Accept', acceptHeader);
        if (this.method === 'POST') {
            headers.append('Content-Type', 'application/x-www-form-urlencoded');
            body = new URLSearchParams();
            body.set('query', query);
            for (const [key, value] of Object.entries(this.additionalUrlParams)) {
                body.set(key, value);
            }
            headers.append('Content-Length', body.toString().length.toString());
        } else {
            const additionalUrlSearchParams = new URLSearchParams(this.additionalUrlParams)
            url += `&${additionalUrlSearchParams.toString()}`;
        }
        return this.handleFetchCall(url, { headers, method: this.method, body });
    }
...
rubensworks commented 2 years ago

Sure, sounds valuable! PR is definitely welcome.

ewsterrenburg commented 2 years ago

Ok, will ask a colleague who is more at home in type script to help me out, then will make a pull request here :)