dbpedia / databus

A digital factory platform for managing files online with stable IDs, high-quality metadata, powerful API and tools for building on data: find, access, make interoperable, re-use
Apache License 2.0
36 stars 16 forks source link

Cannot request collection as JSON from SPARQL #133

Closed henhuy closed 2 months ago

henhuy commented 8 months ago

Beforehand this code (python) worked in order to get SPARQL results as JSON:

collection = "https://databus.openenergyplatform.org/felixmaur/collections/modex_test_renewable"
response = requests.get(collection, headers={"Content-Type": "text/sparql"}, timeout=90)
data = response.json()

Now, I get an JSONDecoderError as result is a SPARQL-string, not dict of SPARQL result (as before). Did the API change? (this could explain my other issue #132 as well)

manonthegithub commented 8 months ago

There were some minor changes in the api, please change your code correspondingly, I don't know exactly what were the changes, but could be the changes in response formatting. @holycrab13 do you know?

We are not planning to change it in recent future again, so you can just fix this with the response you get.

henhuy commented 8 months ago

Adapted my code to the following:

import requests
from SPARQLWrapper import JSON, SPARQLWrapper2

response = requests.get(collection, headers={"Accept": "text/sparql"}, timeout=90)
sparql = SPARQLWrapper2("https://databus.openenergyplatform.org/sparql")
sparql.setReturnFormat(JSON)
sparql.setQuery(response.text)
data = sparql.query()

This works for me.

henhuy commented 2 months ago

Just recently, the code from above gives me "HTTP Error 502: Bad Gateway". Full running example here: https://gist.github.com/henhuy/4d6fcdf9c08466cd989830eb5f01d717 Nevertheless, in Sparql Editor the query is working. What changed? What am I doing wrong? Thanks in advance

manonthegithub commented 2 months ago

What is this SPARQLWrapper2? I would assume something was wrong with the endpoint (maybe someone rebooted nginx/databus). I did a sample request to https://databus.openenergyplatform.org/sparql just now and it should work, at least I got 200, not 502. If still not working I would assume you are sending query in a wrong way. The query should be sent as x-www-formurlencoded parameter named query, or in a body as a json { "query" : "<yourquery>"} but with the Content-Type: application/json. I think you can also send the query as a query string parameter query as well, but didn't check that.

henhuy commented 2 months ago

Thanks for thew quick answer! I will give it a try and give feedback afterwards.

manonthegithub commented 2 months ago

Here is curl for for example for x-www-formurlencoded

curl --location 'https://databus.openenergyplatform.org/sparql' \
--header 'Accept: text/csv' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'query=<your query>'
henhuy commented 2 months ago

Thank you for your hint. - this solved my issue. Could get it to run by following code (including dismissing of SPARQLWrapper2, which was a wrapper around requests but has to much overhead):

response = requests.post(
    DATABUS_ENDPOINT,
    headers={"Accept": "application/json, text/plain, */*", "Content-Type": "application/x-www-form-urlencoded"},
    data={"query": query},
    timeout=90,
)
data = response.json()
return data["results"]["bindings"]