Closed henhuy closed 2 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.
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.
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
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.
Thanks for thew quick answer! I will give it a try and give feedback afterwards.
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>'
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"]
Beforehand this code (python) worked in order to get SPARQL results as 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)