Skip to content

Commit

Permalink
API: on 404, return JSON instead of HTML
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Sep 5, 2024
1 parent 4d9a1fd commit 570e4e9
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 4 deletions.
2 changes: 2 additions & 0 deletions open_prices/api/locations/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def test_location_detail(self):
url = reverse("api:locations-detail", args=[999])
response = self.client.get(url)
self.assertEqual(response.status_code, 404)
self.assertEqual(response.data["code"], "not_found")
# existing location
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
Expand All @@ -115,6 +116,7 @@ def test_location_detail_by_osm(self):
url = reverse("api:locations-get-by-osm", args=["NODE", 999])
response = self.client.get(url)
self.assertEqual(response.status_code, 404)
self.assertEqual(response.data["code"], "not_found")
# existing location
url = reverse(
"api:locations-get-by-osm",
Expand Down
4 changes: 2 additions & 2 deletions open_prices/api/locations/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from django.shortcuts import get_object_or_404
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import filters, mixins, status, viewsets
from rest_framework.decorators import action
Expand All @@ -10,6 +9,7 @@
LocationCreateSerializer,
LocationSerializer,
)
from open_prices.api.utils import get_object_or_drf_404
from open_prices.locations.models import Location


Expand Down Expand Up @@ -49,6 +49,6 @@ def create(self, request: Request, *args, **kwargs):
detail=False, methods=["GET"], url_path=r"osm/(?P<osm_type>\w+)/(?P<osm_id>\d+)"
)
def get_by_osm(self, request, osm_type, osm_id):
location = get_object_or_404(Location, osm_type=osm_type, osm_id=osm_id)
location = get_object_or_drf_404(Location, osm_type=osm_type, osm_id=osm_id)
serializer = self.get_serializer(location)
return Response(serializer.data)
2 changes: 2 additions & 0 deletions open_prices/api/products/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ def test_product_detail(self):
url = reverse("api:products-detail", args=[999])
response = self.client.get(url)
self.assertEqual(response.status_code, 404)
self.assertEqual(response.data["code"], "not_found")
# existing product
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
Expand All @@ -128,6 +129,7 @@ def test_product_detail_by_code(self):
url = reverse("api:products-get-by-code", args=[999])
response = self.client.get(url)
self.assertEqual(response.status_code, 404)
self.assertEqual(response.data["code"], "not_found")
# existing product
url = reverse("api:products-get-by-code", args=[self.product.code])
response = self.client.get(url)
Expand Down
4 changes: 2 additions & 2 deletions open_prices/api/products/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from django.shortcuts import get_object_or_404
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import filters, mixins, viewsets
from rest_framework.decorators import action
Expand All @@ -7,6 +6,7 @@

from open_prices.api.products.filters import ProductFilter
from open_prices.api.products.serializers import ProductFullSerializer
from open_prices.api.utils import get_object_or_drf_404
from open_prices.products.models import Product


Expand All @@ -22,6 +22,6 @@ class ProductViewSet(

@action(detail=False, methods=["GET"], url_path=r"code/(?P<code>\d+)")
def get_by_code(self, request: Request, code):
product = get_object_or_404(Product, code=code)
product = get_object_or_drf_404(Product, code=code)
serializer = self.get_serializer(product)
return Response(serializer.data)
8 changes: 8 additions & 0 deletions open_prices/api/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.http import Http404


def get_object_or_drf_404(model, **kwargs):
try:
return model.objects.get(**kwargs)
except model.DoesNotExist:
raise Http404

0 comments on commit 570e4e9

Please sign in to comment.