update API response structure #71 #76

Merged
Oleg-Komarov merged 2 commits from api-response-format into main 2024-04-11 14:54:42 +02:00
3 changed files with 16 additions and 11 deletions
Showing only changes of commit 11e2dea9f6 - Show all commits

View File

@ -52,8 +52,10 @@ class PublicViewsTest(_BaseTestCase):
response = self.client.get(url, HTTP_ACCEPT=HTTP_ACCEPT) response = self.client.get(url, HTTP_ACCEPT=HTTP_ACCEPT)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response['Content-Type'], 'application/json') self.assertEqual(response['Content-Type'], 'application/json')
self.assertEqual(len(response.json()), 3) json = response.json()
for _, v in response.json().items(): self.assertEqual(len(json['data']), 3)
for v in json['data']:
self.assertIn('id', v)
self.assertIn('name', v) self.assertIn('name', v)
self.assertIn('tagline', v) self.assertIn('tagline', v)
self.assertIn('version', v) self.assertIn('version', v)
@ -191,7 +193,7 @@ class ListedExtensionsTest(_BaseTestCase):
self.assertEqual(response['Content-Type'], 'application/json') self.assertEqual(response['Content-Type'], 'application/json')
# Basic sanity check to make sure we are getting the result of listed # Basic sanity check to make sure we are getting the result of listed
listed_count = len(response.json()) listed_count = len(response.json()['data'])
self.assertEqual(Extension.objects.listed.count(), listed_count) self.assertEqual(Extension.objects.listed.count(), listed_count)
return listed_count return listed_count

View File

@ -18,7 +18,6 @@ urlpatterns = [
path('api/v1/extensions/', api.ExtensionsAPIView.as_view(), name='api'), path('api/v1/extensions/', api.ExtensionsAPIView.as_view(), name='api'),
# Public pages # Public pages
path('', public.HomeView.as_view(), name='home'), path('', public.HomeView.as_view(), name='home'),
path('', api.ExtensionsAPIView.as_view(), name='home-api'),
path('search/', public.SearchView.as_view(), name='search'), path('search/', public.SearchView.as_view(), name='search'),
path('author/<int:user_id>/', public.SearchView.as_view(), name='by-author'), path('author/<int:user_id>/', public.SearchView.as_view(), name='by-author'),
path('search/', public.SearchView.as_view(), name='search'), path('search/', public.SearchView.as_view(), name='search'),

View File

@ -53,6 +53,7 @@ class ListedExtensionsSerializer(serializers.ModelSerializer):
return {} return {}
data = { data = {
'id': instance.extension_id,
'schema_version': instance.latest_version.schema_version, 'schema_version': instance.latest_version.schema_version,
'name': instance.name, 'name': instance.name,
'version': instance.latest_version.version, 'version': instance.latest_version.version,
@ -75,16 +76,12 @@ class ListedExtensionsSerializer(serializers.ModelSerializer):
'tags': [str(tag) for tag in instance.latest_version.tags.all()], 'tags': [str(tag) for tag in instance.latest_version.tags.all()],
} }
return {instance.extension_id: clean_json_dictionary_from_optional_fields(data)} return clean_json_dictionary_from_optional_fields(data)
class ExtensionsAPIView(APIView): class ExtensionsAPIView(APIView):
serializer_class = ListedExtensionsSerializer serializer_class = ListedExtensionsSerializer
@staticmethod
def _convert_list_to_dict(data):
return {k: v for d in data for k, v in d.items()}
@extend_schema( @extend_schema(
parameters=[ parameters=[
OpenApiParameter( OpenApiParameter(
@ -99,5 +96,12 @@ class ExtensionsAPIView(APIView):
serializer = self.serializer_class( serializer = self.serializer_class(
Extension.objects.listed, blender_version=blender_version, request=request, many=True Extension.objects.listed, blender_version=blender_version, request=request, many=True
) )
data_as_dict = self._convert_list_to_dict(serializer.data) data = serializer.data
return Response(data_as_dict) return Response(
{
# TODO implement extension blocking by moderators
'blocklist': [],
'data': data,
'version': 'v1',
}
)