Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Clean up API codebase by using idiomatic DRF and removing boilerplate #194

Merged
merged 37 commits into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
cc19670
Replace `str` with `uuid` to narrow matches
dhruvkb Aug 30, 2021
022ad42
Define a helper function to raise `APIExceptions` from views
dhruvkb Aug 30, 2021
064ccbe
Define a serializer for the audio waveform endpoint
dhruvkb Aug 30, 2021
40a4eb4
Add cURL example for the OEmbed endpoint
dhruvkb Aug 31, 2021
c45d395
Create serializer for `ContentProvider`
dhruvkb Aug 31, 2021
a2f700e
Move pagination attributes from search query serializer to paginator …
dhruvkb Aug 31, 2021
0e03571
Cleanup OEmbed input and output serializers
dhruvkb Aug 31, 2021
870908a
Create parent viewset for all media types
dhruvkb Aug 31, 2021
3c9d2b6
Add examples for reporting audio files
dhruvkb Aug 31, 2021
e0c0ec3
Replace `AboutMediaSerializer` superseded by `ProviderSerializer`
dhruvkb Aug 31, 2021
8d8d226
Add attribute 'page' to search results
dhruvkb Aug 31, 2021
6ec373b
Infer reported content identifier from the endpoint URL
dhruvkb Aug 31, 2021
cc009f3
Migrate endpoint documentation to a docs directory
dhruvkb Aug 31, 2021
a9f92f4
Consolidate endpoints into a DRF viewset
dhruvkb Aug 31, 2021
0acf192
Use DRF router to automatically define endpoints from the viewset
dhruvkb Aug 31, 2021
d5e050f
Update tests based on the new code structure
dhruvkb Aug 31, 2021
c152280
Fix code style violations
dhruvkb Aug 31, 2021
796cd10
Remove redundant import
dhruvkb Aug 31, 2021
a6fa43a
Make serializer nomenclature consistent
dhruvkb Aug 31, 2021
136af05
Programmatically generate list of fields
dhruvkb Aug 31, 2021
d0c8b33
Remove unused serializer
dhruvkb Aug 31, 2021
bfb25a0
Use cleaner validation for deprecated fields, making post-error redun…
dhruvkb Aug 31, 2021
b828ed6
Update error serializers
dhruvkb Aug 31, 2021
c19a0d9
Fix broken reference in audio report endpoint
dhruvkb Sep 1, 2021
ceafca2
Remove field 'id'
dhruvkb Sep 1, 2021
9d120d9
Use `HyperlinkedIdentityField` to automatically generate related URLs
dhruvkb Sep 1, 2021
4862f95
Fix nomenclature in example requests and responses
dhruvkb Sep 1, 2021
a001243
Define request response mappings and use them to run tests on the end…
dhruvkb Sep 1, 2021
545916a
Type hint `SerializerMethodField`s for Swagger
dhruvkb Sep 1, 2021
b263238
Move MD-in-Python out to its separate file
dhruvkb Sep 2, 2021
5d9d667
Add testing for the report endpoint
dhruvkb Sep 2, 2021
b745c30
Unskip thumbnail checks for audio ref. #171
dhruvkb Sep 2, 2021
a405b79
Remove unnecessary scheme replacement ref. 9d120d9
dhruvkb Sep 2, 2021
14914f7
Reformat code
dhruvkb Sep 2, 2021
f15b736
Make operation ID for OEmbed consistent
dhruvkb Sep 2, 2021
683eb17
Disable pagination on stats endpoint
dhruvkb Sep 2, 2021
b93edeb
Add examples in the help text for provider serializer
dhruvkb Sep 21, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions openverse-api/catalog/api/examples/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
image_detail_curl,
image_stats_curl,
report_image_curl,
oembed_list_curl,
)
from catalog.api.examples.image_responses import (
image_search_200_example,
Expand Down
5 changes: 5 additions & 0 deletions openverse-api/catalog/api/examples/image_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,8 @@
# Report an issue about image ID 7c829a03-fb24-4b57-9b03-65f43ed19395
curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer DLBYIcfnKfolaXKcmMC8RIDCavc2hW" -d '{"reason": "mature", "identifier": "7c829a03-fb24-4b57-9b03-65f43ed19395", "description": "This image contains sensitive content"}' https://api.openverse.engineering/v1/images/7c829a03-fb24-4b57-9b03-65f43ed19395/report
""" # noqa

oembed_list_curl = """
# Retrieve embedded content from image URL (https://wordpress.org/openverse/photos/7c829a03-fb24-4b57-9b03-65f43ed19395)
curl -H "Authorization: Bearer DLBYIcfnKfolaXKcmMC8RIDCavc2hW" https://api.openverse.engineering/v1/oembed/?url=https://wordpress.org/openverse/photos/7c829a03-fb24-4b57-9b03-65f43ed19395
""" # noqa
dhruvkb marked this conversation as resolved.
Show resolved Hide resolved
11 changes: 11 additions & 0 deletions openverse-api/catalog/api/serializers/audio_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,17 @@ def create(self, validated_data):
return AudioReport.objects.create(**validated_data)


class AudioWaveformSerializer(serializers.Serializer):
len = serializers.SerializerMethodField()
points = serializers.ListField(
serializers.FloatField(min_value=0, max_value=1)
)

@staticmethod
def get_len(obj):
return len(obj.get('points', []))


class AboutAudioSerializer(AboutMediaSerializer):
"""
Used by `AudioStats`.
Expand Down
21 changes: 21 additions & 0 deletions openverse-api/catalog/api/utils/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from rest_framework import status
from rest_framework.exceptions import APIException
from rest_framework.response import Response
"""
Override the presentation of ValidationErrors, which are deeply nested and
Expand Down Expand Up @@ -51,3 +52,23 @@ def input_error_response(errors):
'fields': fields
}
)


def get_api_exception(error_message, response_code=500, error_code=None):
"""
Returns an instance of a subclass of ``APIException`` with the given error
message, error code and response status code.

The returned object should be used with the ``raise`` keyword.

:param error_message: the detailed error message shown to the user
:param response_code: the HTTP response status code
:param error_code: the codename of the error
:return: an instance of a subclass of ``APIException``
"""

class SubAPIException(APIException):
status_code = response_code
default_detail = error_message
default_code = error_code
return SubAPIException()
8 changes: 4 additions & 4 deletions openverse-api/catalog/urls/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@
name='audio-stats'
),
path(
'<str:identifier>',
'<uuid:identifier>',
AudioDetail.as_view(),
name='audio-detail'
),
path(
'<str:identifier>/thumb',
'<uuid:identifier>/thumb',
AudioArt.as_view(),
name='audio-thumb'
),
path(
'<str:identifier>/recommendations',
'<uuid:identifier>/recommendations',
RelatedAudio.as_view(),
name='audio-related'
),
path(
'<str:identifier>/waveform',
'<uuid:identifier>/waveform',
AudioWaveform.as_view(),
name='audio-waveform'
),
Expand Down
8 changes: 4 additions & 4 deletions openverse-api/catalog/urls/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,22 @@
name='image-oembed'
),
path(
'<str:identifier>',
'<uuid:identifier>',
ImageDetail.as_view(),
name='image-detail'
),
path(
'<str:identifier>/thumb',
'<uuid:identifier>/thumb',
ProxiedImage.as_view(),
name='image-thumb'
),
path(
'<str:identifier>/recommendations',
'<uuid:identifier>/recommendations',
RelatedImage.as_view(),
name='image-related'
),
path(
'<str:identifier>/report',
'<uuid:identifier>/report',
ReportImageView.as_view(),
name='report-image'
),
Expand Down