Skip to content
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

Enable Middleware Support for Blueprint Groups #1399

Conversation

harshanarayana
Copy link
Contributor

@harshanarayana harshanarayana commented Nov 6, 2018

This commit will enable the users to implement a middleware at the blueprint group level whereby enforcing the middleware automatically to each of the available Blueprints that are part of the group.

This will enable a simple way in which a certain set of common features and criteria can be enforced on a Blueprint group. i.e. authentication and authorization

This commit will address the feature request raised as part of Issue #1386

@codecov-io
Copy link

codecov-io commented Nov 6, 2018

Codecov Report

Merging #1399 into master will decrease coverage by 0.66%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1399      +/-   ##
==========================================
- Coverage   91.45%   90.78%   -0.67%     
==========================================
  Files          17       18       +1     
  Lines        1731     1900     +169     
  Branches      330      377      +47     
==========================================
+ Hits         1583     1725     +142     
- Misses        123      151      +28     
+ Partials       25       24       -1
Impacted Files Coverage Δ
sanic/app.py 92.01% <100%> (+0.29%) ⬆️
sanic/blueprint_group.py 100% <100%> (ø)
sanic/reloader_helpers.py 15.57% <0%> (-0.34%) ⬇️
sanic/request.py 100% <0%> (+0.48%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 52deeba...930ec49. Read the comment docs.

@harshanarayana harshanarayana changed the title [WIP] - Enable Middleware Support for Blueprint Groups Enable Middleware Support for Blueprint Groups Nov 6, 2018
Copy link
Contributor

@sjsadowski sjsadowski left a comment

Choose a reason for hiding this comment

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

Please update the documentation related to blueprints before we merge this.

@sjsadowski sjsadowski added this to the 19.03 milestone Nov 6, 2018
@harshanarayana
Copy link
Contributor Author

@sjsadowski I am working on adding the documents. I will update the PR with the changes shortly.

Copy link
Member

@vltr vltr left a comment

Choose a reason for hiding this comment

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

Can you please add a more comprehensive number of tests, with parameters in both the route and url_prefix? Also, add tests with other HTTP methods such as POST and OPTIONS, so everything can be checked successfully.

@harshanarayana
Copy link
Contributor Author

@vltr Sure. Consider it done.

@harshanarayana
Copy link
Contributor Author

@sjsadowski I added the basic documentation on usage and tested a local sphinx build to ensure the changes are working as expected.

@vltr I've added additional unit test cased with few more HTTP methods as well as url_prefix and some parameterized route

@sjsadowski
Copy link
Contributor

@harshanarayana thanks. This won't go in to 18.12 but it will go in after that. Appreciate your help.

@harshanarayana
Copy link
Contributor Author

@sjsadowski I'm OK with this getting merged into any version you see fit. No issues from my side. Just give me a heads-up if anything else needs to be addressed in this PR.

sanic/blueprint_group.py Outdated Show resolved Hide resolved
sanic/blueprint_group.py Outdated Show resolved Hide resolved
sanic/blueprint_group.py Outdated Show resolved Hide resolved
sanic/blueprints.py Outdated Show resolved Hide resolved
sanic/blueprints.py Outdated Show resolved Hide resolved
@harshanarayana harshanarayana force-pushed the feature/GIT-1386_Enable_Blueprint_group_middlewares branch 2 times, most recently from 7e86025 to c652f7f Compare November 9, 2018 17:18
@harshanarayana harshanarayana force-pushed the feature/GIT-1386_Enable_Blueprint_group_middlewares branch from c652f7f to fa4260a Compare November 29, 2018 18:03
@ahopkins ahopkins dismissed sjsadowski’s stale review December 25, 2018 10:04

Documentation is added.

@ahopkins
Copy link
Member

@yunstanford Are you good with this one?

This commit will enable the users to implement a middleware at the
blueprint group level whereby enforcing the middleware automatically to
each of the available Blueprints that are part of the group.

This will eanble a simple way in which a certain set of common features
and criteria can be enforced on a Blueprint group. i.e. authentication
and authorization

This commit will address the feature request raised as part of Issue sanic-org#1386

Signed-off-by: Harsha Narayana <harsha2k4@gmail.com>
Signed-off-by: Harsha Narayana <harsha2k4@gmail.com>
Signed-off-by: Harsha Narayana <harsha2k4@gmail.com>
Signed-off-by: Harsha Narayana <harsha2k4@gmail.com>
Signed-off-by: Harsha Narayana <harsha2k4@gmail.com>
Signed-off-by: Harsha Narayana <harsha2k4@gmail.com>
Signed-off-by: Harsha Narayana <harsha2k4@gmail.com>
Signed-off-by: Harsha Narayana <harsha2k4@gmail.com>
Signed-off-by: Harsha Narayana <harsha2k4@gmail.com>
@harshanarayana harshanarayana force-pushed the feature/GIT-1386_Enable_Blueprint_group_middlewares branch from fa4260a to 4f92e37 Compare February 23, 2019 22:07
@harshanarayana
Copy link
Contributor Author

@huge-success/sanic-core-devs Can you please take a look at this and let me know if there are any other changes required to be done on this PR?

@ashleysommer
Copy link
Member

ashleysommer commented Feb 23, 2019

@harshanarayana
Overall this looks pretty good.
When this gets merged, and users start to use it in their applications, tools like SanicPluginsFramework, and other plugins may no longer work as expected, because they tend to assume the only classes that can have middlewares on them are Sanic App and Blueprints. They may need to be modified to be BlueprintGroup aware, but that is inevitable fallout.

The only code issue I see is the use of group.blueprint = bp where you override the property setter to append bp to a list. That is a counter-intuitive pattern to me, as usually the python assignment operator = is used to replace the target, not append to it.

The way I've usually implemented this kind of functionality is to create a method called .append() on the class, and call .append(bp) as if the class itself is a list. This also matches the other list-like functionality on BlueprintGroup like the iteration behaviour.

@harshanarayana
Copy link
Contributor Author

harshanarayana commented Feb 24, 2019

@ashleysommer Thank you very much for the review and suggestions.

tools like SanicPluginsFramework, and other plugins may no longer work as expected, because they tend to assume the only classes that can have middlewares on them are Sanic App and Blueprints

The idea was to keep almost all the existing behaviors of Blueprint.group intact so that the other plugins and frameworks don't run into any major breaking issues. I will take a look around the existing plugins and see if any of them will actually have a problem because of this change.

The way I've usually implemented this kind of functionality is to create a method called .append() on the class, and call .append(bp) as if the class itself is a list.

That sounds like a really good thing to do. Let me get that change sorted.

…nstead of properly to add blueprint to group

Signed-off-by: Harsha Narayana <harsha2k4@gmail.com>
@ashleysommer
Copy link
Member

The idea was to keep almost all the existing behaviors of Blueprint.group intact so that the other plugins and frameworks don't run into any major breaking issues.

Yeah, it's a good implementation. I didn't mean to suggest it might cause breaking changes. I don't think that. I just mean that some plugins will need to be BlueprintGroup aware, specifically if it is a middleware-based plugin and it wants to apply its plugin/middleware functionality to blueprint groups.

@harshanarayana
Copy link
Contributor Author

I didn't mean to suggest it might cause breaking changes

Sorry if my message came across that way. I wasn't meaning that. After you mentioned, I realized that there might be some breaking changes wrt these plugins if they are doing an isinstance like check for something.

@sjsadowski sjsadowski closed this Mar 3, 2019
@ashleysommer
Copy link
Member

@sjsadowski
Why was this closed?
I thought it should be merged.

@sjsadowski
Copy link
Contributor

Ugh. It has to do with how projects work. I closed all the PRs that looked like they were merged from the project screen. Gotta go fix now...

@sjsadowski sjsadowski reopened this Mar 3, 2019
@sjsadowski sjsadowski dismissed yunstanford’s stale review March 3, 2019 22:25

Stale review, looks like we should be good to go.

Copy link
Contributor

@sjsadowski sjsadowski left a comment

Choose a reason for hiding this comment

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

I'm good with this, even though it could break some projects (known)

@sjsadowski sjsadowski merged commit 348964f into sanic-org:master Mar 3, 2019
lixxu pushed a commit to lixxu/sanic that referenced this pull request Mar 5, 2019
* enable blueprint group middleware support

This commit will enable the users to implement a middleware at the
blueprint group level whereby enforcing the middleware automatically to
each of the available Blueprints that are part of the group.

This will eanble a simple way in which a certain set of common features
and criteria can be enforced on a Blueprint group. i.e. authentication
and authorization

This commit will address the feature request raised as part of Issue sanic-org#1386

Signed-off-by: Harsha Narayana <harsha2k4@gmail.com>

* enable indexing of BlueprintGroup object

Signed-off-by: Harsha Narayana <harsha2k4@gmail.com>

* rename blueprint group file to fix spelling error

Signed-off-by: Harsha Narayana <harsha2k4@gmail.com>

* add documentation and additional unit tests

Signed-off-by: Harsha Narayana <harsha2k4@gmail.com>

* cleanup and optimize headers in unit test file

Signed-off-by: Harsha Narayana <harsha2k4@gmail.com>

* fix Bluprint Group iteratable method

Signed-off-by: Harsha Narayana <harsha2k4@gmail.com>

* add additional unit test to check StopIteration condition

Signed-off-by: Harsha Narayana <harsha2k4@gmail.com>

* cleanup iter protocol implemenation for blueprint group and add slots

Signed-off-by: Harsha Narayana <harsha2k4@gmail.com>

* fix blueprint group middleware invocation identification

Signed-off-by: Harsha Narayana <harsha2k4@gmail.com>

* feat: enable list behavior on blueprint group object and use append instead of properly to add blueprint to group

Signed-off-by: Harsha Narayana <harsha2k4@gmail.com>

add port to url_for, and update doc for it, reset scheme and external if server not specified in url_for

add port to url_for, and update doc for it, reset scheme and external if server not specified in url_for

Revert "add port to url_for, and update doc for it, reset scheme and external if server not specified in url_for"

This reverts commit b073dfa.
@ahopkins
Copy link
Member

ahopkins commented Mar 5, 2019

I agree. I think this is a positive breaking change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants