-
Notifications
You must be signed in to change notification settings - Fork 13.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: extract json_required view decorator #18170
Conversation
Thank you for the PR @ad-m. Grabbing an example from Java, I really like the abstraction of
Since request.is_json is pretty much checking the MIME type we could create a generic decorator the same way JAX-RS does. This will allow us to validate all request types in our endpoints. It would also be a good idea to keep the MIME types as constants if Python/Flask does not provide them yet. |
I like to make it even more generic. I will apply you suggestion soon!
śr., 26 sty 2022, 12:21 użytkownik Michael S. Molina <
***@***.***> napisał:
… Thank you for the PR @ad-m <https://github.com/ad-m>. Grabbing an example
from Java, I really like the abstraction of @produces and @consumes
annotations of JAX-RS
<https://docs.oracle.com/cd/E19798-01/821-1841/gipzh/index.html>. They
accept the MIME type as an argument.
@consumes("application/json")
Since request.is_json
<https://flask.palletsprojects.com/en/2.0.x/api/#flask.Request.is_json>
is pretty much checking the MIME type we could create a generic decorator
the same way JAX-RS does. This will allow us to validate all request types
in our endpoints.
—
Reply to this email directly, view it on GitHub
<#18170 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AA3TNL3EOCQQLMKJOC56K4LUX7KLRANCNFSM5MZ3RLVA>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
I'm not sure we need to make it generic. Which other request payload type we must handle? |
I'd recommend use a single decorator to keep things simple---so you don't have to import both the decorator and the constant. It may be more appropriate to name the function with a verb, i.e. |
The
While checking the API docs I found |
I think eventually we would want everything to be submitted via JSON, except maybe when uploading files, in which case you can just not use this decorator. |
Codecov Report
@@ Coverage Diff @@
## master #18170 +/- ##
==========================================
+ Coverage 66.05% 66.08% +0.02%
==========================================
Files 1591 1591
Lines 62418 62413 -5
Branches 6286 6286
==========================================
+ Hits 41228 41243 +15
+ Misses 19568 19548 -20
Partials 1622 1622
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
677693a
to
00b2d09
Compare
I renamed decorator to I inspected code looking for other request formats and I notice that have I decided to use two decorators because I considered a small number of scenarios, I felt there was no reason to add more responsibility to one function (which increases complexity) since there are only two simple scenarios. The current approach is simple - readable and easy to use. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
* refactor: extract json_required view decorator * chore: rename json_required to requires_json * refactor: add requires_form_data decorator and use it * fix: fix lint issue, raise InvalidPayloadFormatError for invalid payload
* refactor: extract json_required view decorator * chore: rename json_required to requires_json * refactor: add requires_form_data decorator and use it * fix: fix lint issue, raise InvalidPayloadFormatError for invalid payload
* refactor: extract json_required view decorator * chore: rename json_required to requires_json * refactor: add requires_form_data decorator and use it * fix: fix lint issue, raise InvalidPayloadFormatError for invalid payload
SUMMARY
During review #18151 there was a comment about code duplication of snippet like:
Originally posted by @ktmud in #18151 (comment)
I would like to get familiar with part of the codebase of the project written in Python, so I perceived that as a good first issue for a new contributor.
BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
N/A
TESTING INSTRUCTIONS
Python test handle all scenarios. I added a new one integration tests to verify behaviour of extracted decorator.
ADDITIONAL INFORMATION
@ktmud, @michael-s-molina Can I request reviews? I am aware this is a trivial change, but I want to try to grab the project style & structure to be able to make bigger changes, and the beginning has to be somewhere.