%% @doc A basic SPARQL client.
-module(sparql_client).

-export([
    describe/2,
    query/2
]).

-include_lib("zotonic.hrl").

describe(Endpoint, <<"https://", _/binary>> = Uri) ->
    describe(Endpoint, <<"<", Uri/binary, ">">>);
describe(Endpoint, <<"http://", _/binary>> = Uri) ->
    describe(Endpoint, <<"<", Uri/binary, ">">>);
describe(Endpoint, Clause) ->
    query(Endpoint, z_convert:to_binary(z_url:url_encode(<<"DESCRIBE ", Clause/binary>>))).
    
query(Endpoint, Query) ->
    Qs = z_convert:to_binary(z_url:url_encode(Query)),
    Url = <<Endpoint/binary, "?query=", Qs/binary>>,
    case ginger_http_client:get(Url, headers()) of
        undefined ->
            undefined;
        Map ->
            decode(Map)
    end.

decode(Data) ->
    ginger_json_ld:deserialize(Data).

headers() ->
    [
        {"Accept", "application/ld+json"}
    ].

%% @doc A basic SPARQL client.
-module(sparql_client).

-export([
    describe/2,
    query/2,
    query/3
]).

-include_lib("zotonic.hrl").

describe(Endpoint, <<"https://", _/binary>> = Uri) ->
    describe(Endpoint, <<"<", Uri/binary, ">">>);
describe(Endpoint, <<"http://", _/binary>> = Uri) ->
    describe(Endpoint, <<"<", Uri/binary, ">">>);
describe(Endpoint, Clause) ->
    query(Endpoint, (<<"DESCRIBE ", Clause/binary>>)).
    
query(Endpoint, Query) ->
    query(Endpoint, Query, headers()).

-spec query(binary(), binary(), map()) -> list().
query(Endpoint, Query, Headers) ->
    Qs = z_convert:to_binary(z_url:url_encode(Query)),
    Url = <<Endpoint/binary, "?query=", Qs/binary>>,
    case ginger_http_client:get(Url, Headers) of
        undefined ->
            undefined;
        Map ->
            decode(Map)
    end.

decode(#{<<"@graph">> := _} = Data) ->
    ginger_json_ld:deserialize(Data);
decode(Data) ->
    Data.

headers() ->
    #{<<"Accept">> => <<"application/ld+json">>}.

Add plain JSON support to SPARQL client
[rdf] Add plain JSON support to SPARQL client

This is necessary for http://nl.dbpedia.org/sparql, which doesn't
return SELECT results in JSON-LD but in plain JSON.
