Skip to content

Commit

Permalink
fix: handle bad mediatype request
Browse files Browse the repository at this point in the history
  • Loading branch information
aaxelb committed Dec 11, 2024
1 parent 9b7a24d commit 743299e
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 14 deletions.
4 changes: 2 additions & 2 deletions trove/render/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
_renderer_type.MEDIATYPE: _renderer_type
for _renderer_type in RENDERERS
}
DEFAULT_RENDERER = RdfJsonapiRenderer # the most stable one
DEFAULT_RENDERER_TYPE = RdfJsonapiRenderer # the most stable one


def get_renderer_type(request: http.HttpRequest) -> type[BaseRenderer]:
Expand All @@ -45,5 +45,5 @@ def get_renderer_type(request: http.HttpRequest) -> type[BaseRenderer]:
_chosen_renderer_type = _renderer_type
break
if _chosen_renderer_type is None:
_chosen_renderer_type = DEFAULT_RENDERER
_chosen_renderer_type = DEFAULT_RENDERER_TYPE
return _chosen_renderer_type
12 changes: 10 additions & 2 deletions trove/views/indexcard.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
from primitive_metadata import gather

from trove import exceptions as trove_exceptions
from trove.render import get_renderer_type
from trove.render import (
DEFAULT_RENDERER_TYPE,
get_renderer_type,
)
from trove.trovesearch.trovesearch_gathering import trovesearch_by_indexstrategy
from trove.vocab.namespaces import TROVE
from trove.vocab.trove import trove_indexcard_iri
Expand All @@ -14,8 +17,8 @@

class IndexcardView(View):
def get(self, request, indexcard_uuid):
_renderer_type = get_renderer_type(request)
try:
_renderer_type = get_renderer_type(request)
_search_gathering = trovesearch_by_indexstrategy.new_gathering({
# TODO (gather): allow omitting kwargs that go unused
'search_params': None,
Expand All @@ -32,6 +35,11 @@ def get(self, request, indexcard_uuid):
content_rendering=_renderer.render_document(),
http_request=request,
)
except trove_exceptions.CannotRenderMediatype as _error:
return make_http_error_response(
error=_error,
renderer=DEFAULT_RENDERER_TYPE(_indexcard_iri),
)
except trove_exceptions.TroveError as _error:
return make_http_error_response(
error=_error,
Expand Down
19 changes: 16 additions & 3 deletions trove/views/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
)
from trove.trovesearch.trovesearch_gathering import trovesearch_by_indexstrategy
from trove.vocab.namespaces import TROVE
from trove.render import get_renderer_type
from trove.render import (
DEFAULT_RENDERER_TYPE,
get_renderer_type,
)
from ._responder import (
make_http_error_response,
make_http_response,
Expand Down Expand Up @@ -49,8 +52,8 @@

class CardsearchView(View):
def get(self, request):
_renderer_type = get_renderer_type(request)
try:
_renderer_type = get_renderer_type(request)
_search_iri, _search_gathering = _parse_request(request, _renderer_type, CardsearchParams)
_search_gathering.ask(
DEFAULT_CARDSEARCH_ASK, # TODO: build from `include`/`fields`
Expand All @@ -61,6 +64,11 @@ def get(self, request):
content_rendering=_renderer.render_document(),
http_request=request,
)
except trove_exceptions.CannotRenderMediatype as _error:
return make_http_error_response(
error=_error,
renderer=DEFAULT_RENDERER_TYPE(_search_iri),
)
except trove_exceptions.TroveError as _error:
return make_http_error_response(
error=_error,
Expand All @@ -70,8 +78,8 @@ def get(self, request):

class ValuesearchView(View):
def get(self, request):
_renderer_type = get_renderer_type(request)
try:
_renderer_type = get_renderer_type(request)
_search_iri, _search_gathering = _parse_request(request, _renderer_type, ValuesearchParams)
_search_gathering.ask(
DEFAULT_VALUESEARCH_ASK, # TODO: build from `include`/`fields`
Expand All @@ -82,6 +90,11 @@ def get(self, request):
content_rendering=_renderer.render_document(),
http_request=request,
)
except trove_exceptions.CannotRenderMediatype as _error:
return make_http_error_response(
error=_error,
renderer=DEFAULT_RENDERER_TYPE(_search_iri),
)
except trove_exceptions.TroveError as _error:
return make_http_error_response(
error=_error,
Expand Down
33 changes: 26 additions & 7 deletions trove/views/vocab.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
from django import http
from django.views import View

from trove.render import get_renderer_type
from trove import exceptions as trove_exceptions
from trove.render import (
DEFAULT_RENDERER_TYPE,
get_renderer_type,
)
from trove.vocab.namespaces import TROVE
from trove.vocab.trove import TROVE_API_THESAURUS
from ._responder import make_http_response
from ._responder import (
make_http_error_response,
make_http_response,
)


class TroveVocabView(View):
Expand All @@ -14,8 +21,20 @@ def get(self, request, vocab_term):
_data = {_iri: TROVE_API_THESAURUS[_iri]}
except KeyError:
raise http.Http404
_renderer = get_renderer_type(request)(_iri, _data)
return make_http_response(
content_rendering=_renderer.render_document(),
http_request=request,
)
try:
_renderer_type = get_renderer_type(request)
_renderer = _renderer_type(_iri, _data)
return make_http_response(
content_rendering=_renderer.render_document(),
http_request=request,
)
except trove_exceptions.CannotRenderMediatype as _error:
return make_http_error_response(
error=_error,
renderer=DEFAULT_RENDERER_TYPE(_iri),
)
except trove_exceptions.TroveError as _error:
return make_http_error_response(
error=_error,
renderer=_renderer_type(_iri),
)

0 comments on commit 743299e

Please sign in to comment.