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

Conversation

dhruvkb
Copy link
Member

@dhruvkb dhruvkb commented Aug 31, 2021

Fixes

Fixes #181 by @dhruvkb
Partially addresses WordPress/openverse#747 by @dhruvkb

Description

This PR consolidates API endpoints into an inheritance based model using DRF viewsets. This vastly reduces the amount of boilerplate code.

This PR

  • replaces APIViews with code repetition into highly abstracted ViewSets
  • uses Router to automatically setup URL mappings for APIView endpoints
  • uses DRF's blessed approach for pagination
  • moves documentation logic outside views to make it easier to maintain
  • enforces nomenclature conventions in serializer classes (such as 'Request' for input serializers)
  • brings parity between image and audio views (adding report view for audio)
  • cleans up exception handling logic for consistent error messages
  • performs other housekeeping work such as
    • removing unused helper functions, serializers and utils
    • adding documentation wherever deemed inadequate
    • reducing hardcoding and boilerplate with programmatically generated values
  • updates tests so that we have ✅

Checklist

  • My pull request has a descriptive title (not a vague title like Update index.md).
  • My pull request targets the default branch of the repository (main or master).
  • My commit messages follow best practices.
  • My code follows the established code style of the repository.
  • I added or updated tests for the changes I made (if applicable).
  • I added or updated documentation (if applicable).
  • I tried running the project locally and verified that there are no visible errors.

Developer Certificate of Origin

Developer Certificate of Origin
Developer Certificate of Origin
Version 1.1

Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
1 Letterman Drive
Suite D4700
San Francisco, CA, 94129

Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.


Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.

@dhruvkb dhruvkb marked this pull request as ready for review August 31, 2021 21:37
@dhruvkb dhruvkb requested a review from a team as a code owner August 31, 2021 21:37
@dhruvkb dhruvkb changed the title Use DRF viewsets for boilerplate-free code Clean up API codebase by using idiomatic DRF and removing boilerplate Sep 1, 2021
Copy link
Member

@krysal krysal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is HUGE. The separation of documentation from the app code is really appreciated, and though seems like the extra layers of DRF make the curve steeper for new contributors, I believe the benefits should pay off in the long run, given your exposed points.

I also want to highlight this quote of yours for future PRs:

Preferably make individual atomic PRs addressing one issue at a time, so that they can be reviewed and merged faster.

That said, thanks for all this good work!

@dhruvkb
Copy link
Member Author

dhruvkb commented Sep 10, 2021

Really sorry about the size @krysal, and thanks for the review. I tried making small PRs but all the changes were so interdependent on each other. I'll definitely try harder in the next one.

@zackkrida
Copy link
Member

@obulat and/or @sarayourfriend we could use 👀 on this PR; thank you!

@sarayourfriend
Copy link
Contributor

I've tried to review this PR a couple times but I really can't comment on its changes, they're just too many for me to keep track of especially with how little knowledge I have of the pre-existing code. Hopefully @obulat is in a better position to help than I am, sorry!

Copy link
Contributor

@obulat obulat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great cleanup, and tests pass :) I've learned a lot by reviewing, thank you!

@@ -0,0 +1,116 @@
# Introduction
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these docs go to the make handbook, just like the Catalog and Frontend docs?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, because they kind of have to live here: https://api.openverse.engineering/v1/ and I don't see a strong reason to duplicate the information. We should however add a link and reference to the API docs in the handbook.

@dhruvkb dhruvkb added ✨ goal: improvement Improvement to an existing user-facing feature 🤖 aspect: dx Concerns developers' experience with the codebase labels Sep 21, 2021
@dhruvkb dhruvkb merged commit a470f3f into main Sep 21, 2021
@dhruvkb dhruvkb deleted the code_cleanup branch September 21, 2021 17:38
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🤖 aspect: dx Concerns developers' experience with the codebase ✨ goal: improvement Improvement to an existing user-facing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature] API code quality improvements
5 participants