Skip to content

Commit

Permalink
OEKG-UI: cleanup view module and enhance error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
jh-RLI committed Feb 20, 2025
1 parent 77e075e commit e221d5f
Showing 1 changed file with 9 additions and 47 deletions.
56 changes: 9 additions & 47 deletions oekg/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@

from oekg.utils import execute_sparql_query

from oekg.utils import validate_public_sparql_query
from oeplatform.settings import OEKG_SPARQL_ENDPOINT_URL



@login_required
def main_view(request):
Expand All @@ -19,66 +15,32 @@ def main_view(request):
return response


# TODO: This endpoint requires a CSRF token, to be usable via a
# https call using CURL or requests (what the user expects when using a web-api)
# this endpoint must also accessible using the API token via the RestFramework.
@require_POST
def sparql_endpoint(request):
"""
Public SPARQL endpoint. Must only allow read queries.
Internal SPARQL endpoint. Must only allow read queries. Intended to be use
with a djago app frontend as it requires an CSRF token.
Note: The http based oekg sparql endpoint is implemented in api/views.py
OekgSparqlAPIView. It is usable by providing a valid API key in the request
"""
sparql_query = request.POST.get("query", "")
response_format = request.POST.get("format", "json") # Default format

try:
content, content_type = execute_sparql_query(sparql_query, response_format)
except ValueError as e:
return HttpResponseBadRequest(str(e))
return HttpResponseBadRequest(
f"{str(e)}. Please provide a valid SPARQL query."
"This does not include update/delete queries."
)

if content_type == "application/sparql-results+json":
return JsonResponse(json.loads(content), safe=False)
else:
return HttpResponse(content, content_type=content_type)


# def sparql_endpoint(request):
# sparql_query = request.POST.get("query", "")
# response_format = request.POST.get("format", "json") # Default format

# # Whitelist of supported formats
# supported_formats = {
# "json": "application/sparql-results+json",
# "json-ld": "application/ld+json",
# "xml": "application/rdf+xml",
# "turtle": "text/turtle",
# }

# if not sparql_query:
# return HttpResponseBadRequest("Missing 'query' parameter.")

# if not validate_sparql_query(sparql_query):
# raise SuspiciousOperation("Invalid SPARQL query.")

# # Validate and map the requested format
# if response_format not in supported_formats:
# return HttpResponseBadRequest(f"Unsupported format: {response_format}")


# endpoint_url = OEKG_SPARQL_ENDPOINT_URL
# headers = {"Accept": supported_formats[response_format]}

# response = requests.post(
# endpoint_url, data={"query": sparql_query}, headers=headers
# )

# # Handle different response types
# content_type = supported_formats[response_format]
# if content_type == "application/sparql-results+json":
# return JsonResponse(response.json(), safe=False)
# else:
# return HttpResponse(response.content, content_type=content_type)


@require_GET
def sparql_metadata(request):
supported_formats = {
Expand Down

0 comments on commit e221d5f

Please sign in to comment.