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 26 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
Empty file.
214 changes: 214 additions & 0 deletions openverse-api/catalog/api/docs/audio_docs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
from drf_yasg import openapi

from catalog.api.docs.media_docs import (
fields_to_md,
MediaSearch,
MediaStats,
MediaDetail,
MediaRelated,
MediaComplain,
)
from catalog.api.examples import (
audio_search_curl,
audio_search_200_example,
audio_search_400_example,
recommendations_audio_read_curl,
recommendations_audio_read_200_example,
recommendations_audio_read_404_example,
audio_detail_curl,
audio_detail_200_example,
audio_detail_404_example,
audio_stats_curl,
audio_stats_200_example,
report_audio_curl,
audio_report_create_201_example,
)
from catalog.api.serializers.audio_serializers import (
AudioSearchRequestSerializer,
AudioSearchSerializer,
AudioSerializer,
AudioReportSerializer,
)
from catalog.api.serializers.error_serializers import (
InputErrorSerializer,
NotFoundErrorSerializer,
)
from catalog.api.serializers.provider_serializers import ProviderSerializer


class AudioSearch(MediaSearch):
desc = f"""
audio_search is an API endpoint to search audio files using a query string.

By using this endpoint, you can obtain search results based on specified
query and optionally filter results by
{fields_to_md(AudioSearchRequestSerializer.fields_names)}.

{MediaSearch.desc}""" # noqa

responses = {
"200": openapi.Response(
description="OK",
examples=audio_search_200_example,
schema=AudioSearchSerializer(many=True)
),
"400": openapi.Response(
description="Bad Request",
examples=audio_search_400_example,
schema=InputErrorSerializer
),
}

code_examples = [
{
'lang': 'Bash',
'source': audio_search_curl,
},
]

swagger_setup = {
'operation_id': 'audio_search',
'operation_description': desc,
'query_serializer': AudioSearchRequestSerializer,
'responses': responses,
'code_examples': code_examples
}


class AudioStats(MediaStats):
desc = f"""
audio_stats is an API endpoint to get a list of all content providers and their
respective number of audio files in the Openverse catalog.

{MediaStats.desc}""" # noqa

responses = {
"200": openapi.Response(
description="OK",
examples=audio_stats_200_example,
schema=ProviderSerializer(many=True)
)
}

code_examples = [
{
'lang': 'Bash',
'source': audio_stats_curl,
},
]

swagger_setup = {
'operation_id': 'audio_stats',
'operation_description': desc,
'responses': responses,
'code_examples': code_examples,
}


class AudioDetail(MediaDetail):
desc = f"""
audio_detail is an API endpoint to get the details of a specified audio ID.

By using this endpoint, you can get audio details such as
{fields_to_md(AudioSerializer.fields_names)}.

{MediaDetail.desc}""" # noqa

responses = {
"200": openapi.Response(
description="OK",
examples=audio_detail_200_example,
schema=AudioSerializer
),
"404": openapi.Response(
description="OK",
examples=audio_detail_404_example,
schema=NotFoundErrorSerializer
)
}

code_examples = [
{
'lang': 'Bash',
'source': audio_detail_curl,
},
]

swagger_setup = {
'operation_id': 'audio_detail',
'operation_description': desc,
'responses': responses,
'code_examples': code_examples,
}


class AudioRelated(MediaRelated):
desc = f"""
recommendations_audio_read is an API endpoint to get related audio files
for a specified audio ID.

By using this endpoint, you can get the details of related audio such as
{fields_to_md(AudioSerializer.fields_names)}.

{MediaRelated.desc}""" # noqa

responses = {
"200": openapi.Response(
description="OK",
examples=recommendations_audio_read_200_example,
schema=AudioSerializer
),
"404": openapi.Response(
description="Not Found",
examples=recommendations_audio_read_404_example,
schema=NotFoundErrorSerializer
)
}

code_examples = [
{
'lang': 'Bash',
'source': recommendations_audio_read_curl,
},
]

swagger_setup = {
'operation_id': 'audio_related',
'operation_description': desc,
'responses': responses,
'code_examples': code_examples
}


class AudioComplain(MediaComplain):
desc = f"""
audio_report_create is an API endpoint to report an issue about a specified
audio ID to Openverse.

By using this endpoint, you can report an audio file if it infringes copyright,
contains mature or sensitive content and others.

{MediaComplain.desc}""" # noqa

responses = {
"201": openapi.Response(
description="OK",
examples=audio_report_create_201_example,
schema=AudioReportSerializer
)
}

code_examples = [
{
'lang': 'Bash',
'source': report_audio_curl,
}
]

swagger_setup = {
'operation_id': 'audio_report',
'operation_description': desc,
'query_serializer': AudioReportSerializer,
'responses': responses,
'code_examples': code_examples,
}
Loading