-
Notifications
You must be signed in to change notification settings - Fork 0
SPARQL API
Jelte van Boheemen edited this page Nov 12, 2020
·
8 revisions
graph | query endpoint | update endpoint |
---|---|---|
https://read-it.hum.uu.nl/item/ | /sparql/item/query | /sparql/item/update |
https://read-it.hum.uu.nl/vocab# | /sparql/vocab/query | |
https://read-it.hum.uu.nl/source/ | /sparql/source/query | /sparql/source/update |
https://read-it.hum.uu.nl/ontology# | /sparql/ontology/query | |
https://read-it.hum.uu.nl/nlp-ontology# | /sparql/nlp-ontology/query | /sparql/nlp-ontology/update |
Example full url for endpoint: https://read-it.hum.uu.nl/sparql/item/query
- GET request to a query endpoint.
- Obtain the triples in the corresponding graph.
- POST request to a query endpoint.
- Obtain query results.
- Only the corresponding graph will be queried.
- Request body should contain an
query
parameter containing SPARQL QUERY.DESCRIBE
queries are not supported. - An
Accept
header can be included to specify the desired output format, see below for options.
Accept header |
Format | Applicable query types |
---|---|---|
application/sparql-results+json (default) | SPARQL 1.1 Query Results JSON Format | ASK, SELECT |
application/sparql-results+xml | SPARQL 1.1 Query Results XML Format | ASK, SELECT |
text/turtle | Turtle | ASK, SELECT, CONSTRUCT |
Retrieve 5 sources:
curl -d "query=SELECT ?s ?p ?o WHERE {?s ?p ?o } LIMIT 5" -X POST -H "Accept: text/turtle" https://read-it.hum.uu.nl/sparql/source/query
Uses the requests
package.
import requests
q = 'SELECT ?s ?p ?o WHERE {?s ?p ?o } LIMIT 5'
endpoint = 'https://read-it.hum.uu.nl/sparql/source/query'
response = requests.post(endpoint,
data={'query': q},
headers={'Accept': 'text/turtle'})
results = response.text
print(results)
Make a POST request to https://read-it.hum.uu.nl/rest-auth/login/ (note the trailing slash) with your username and password in the body to obtain an authentication token.
curl -d "username=yourusername&password=yourpassword" -X POST https:/read-it.hum.uu.nl/rest-auth/login/
import requests
login = requests.post('https://read-it.hum.uu.nl/rest-auth/login/',
data={'username': 'yourusername',
'password': 'yourpassword'})
key = login.json()['key']
- POST request to an update endpoint.
- Only the corresponding graph can be updated.
- Only specific users are allowed to UPDATE. Contact admins for information.
- Request body should contain an
update
parameter containing valid SPARQL UPDATE. - An
Authorization
header should be included containing yourToken <your_token>
. See above. - The following operations are not supported, because they make no sense in the context of a single graph, or are unsafe:
LOAD
,CLEAR
,DROP
,ADD
,MOVE
,COPY
,CREATE
.
import requests
# Authentication
login = requests.post('https://read-it.hum.uu.nl/rest-auth/login/',
data={'username': 'yourusername',
'password': 'yourpassword'})
key = login.json()['key']
# Update
q = 'INSERT DATA { <https://read-it.hum.uu.nl/nlp-ontology#content> a <http://www.w3.org/2000/01/rdf-schema#Class> }'
endpoint = 'https://read-it.hum.uu.nl/sparql/nlp-ontology/update'
response = requests.post(endpoint,
data={'update': q},
headers={'Authorization': 'Token {}'.format(key)})
succes_msg = response.text
print(succes_msg)
print(response.status_code)