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

Allow multiple flask instances per Api instance #28

Closed
wants to merge 2 commits into from

Conversation

lafrech
Copy link
Member

@lafrech lafrech commented Nov 21, 2018

Closes #27.

TODO:

  • Changelog
  • Documentation

@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling d9ccd3b on dev_allow_multiple_apps into 9914da9 on master.

@coveralls
Copy link

coveralls commented Nov 21, 2018

Coverage Status

Coverage remained the same at 100.0% when pulling d26ba69 on dev_allow_multiple_apps into 4193ddf on master.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling d9ccd3b on dev_allow_multiple_apps into 9914da9 on master.

@@ -132,13 +140,18 @@ def _register_redoc_rule(self, blueprint):
redoc_url = (
'https://cdn.jsdelivr.net/npm/redoc@'
'{}/bundles/redoc.standalone.js'.format(redoc_version))
self._redoc_url = redoc_url

Copy link

Choose a reason for hiding this comment

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

I'm not sure why you moved this function here. I think you could use current_app.name as in _openapi_json. Same thing goes for openapi_swagger_ui below.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for reviewing.

I also need to pass redoc_url, swagger_ui_url and swagger_ui_supported_submit_methods.

I find it easier to use a closure than to pass them via the app object.

Or maybe I misunderstood your suggestion.

Are you suggesting I leave them as private attributes to the spec object (which always looked patchy to me, BTW) and I pass the spec object via the app object as app.extensions['flask-rest-api']['spec'] ?

Copy link

Choose a reason for hiding this comment

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

Sure. I haven't noticed those, sorry. I don't have a strong argument towards one approach or the other (I'm still familiarizing with your code).

def _register_openapi_json_rule(self, app, blueprint):
"""Serve json spec file"""
json_path = app.config.get('OPENAPI_JSON_PATH', 'openapi.json')

Copy link

Choose a reason for hiding this comment

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

Similar to my other comment, I think you could use current_app instead of a closure with app. Except that here you may need to use: current_app._get_current_object() to access self.specs

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, perhaps I could store the spec object in app.extensions['flask-rest-api']['spec'] and access it from there.

I don't have a strong feeling about it. Any downside with the closure approach?

Copy link

Choose a reason for hiding this comment

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

I don't have a strong argument against the closure approach. I just find a bit confusing having references to both current_app and app in the same function. I would pick one way to reference the application instance. Or maybe I'm missing something (?)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I agree it looks strange to use both. As I mentioned in the comment, I was using current_app here on the wrong assumption that current_app.response_class might be resolved at function definition time and would be wrong if the response class is changed after Api.init_app. I wan't sure about that but was too sloppy to check.

Here's the check:

def outside(obj):
    def inside():
        return obj.attr
    return inside

class Obj:
    def __init__(self, val):
        self.attr = val

o = Obj(12)
f = outside(o)
assert f() == 12
o.attr = 42
assert f() == 42

Great. So I can change that to

            return app.response_class(
                json.dumps(self.specs[app].to_dict(), indent=2),
                mimetype='application/json')

Copy link

Choose a reason for hiding this comment

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

Nice!

@lafrech lafrech force-pushed the dev_allow_multiple_apps branch 2 times, most recently from 70ec56a to 5b34024 Compare November 21, 2018 21:01
@marshmallow-code marshmallow-code deleted a comment Nov 21, 2018
@marshmallow-code marshmallow-code deleted a comment Nov 21, 2018
@marshmallow-code marshmallow-code deleted a comment Nov 21, 2018
@lafrech lafrech force-pushed the dev_allow_multiple_apps branch 2 times, most recently from 113f5e6 to 8a7ff4d Compare November 27, 2018 21:46
@lafrech lafrech changed the title WIP - Allow multiple flask instances per Api instance Allow multiple flask instances per Api instance Nov 29, 2018
@lafrech
Copy link
Member Author

lafrech commented Nov 29, 2018

This POC looks functional, but I'm still worried it might be opening the door to corner case issues. It changes the philosophy of the library.

I'd like to see a convincing use case before pushing this further.

Holding this for now.

@svidela
Copy link

svidela commented Nov 30, 2018

Awesome! Thanks for considering and coding this.

Regarding use cases (I'm thinking at loud here...), I think it would be nice to have this in case you want to implement some kind of application dispatching combining various Flask application instances (http://flask.pocoo.org/docs/1.0/patterns/appdispatch/#application-dispatching). I will try to code some examples to see how that would actually work.

cheers!

@lafrech
Copy link
Member Author

lafrech commented Jun 11, 2019

Closing this for now.

See #38 (comment).

@lafrech lafrech closed this Jun 11, 2019
@lafrech lafrech deleted the dev_allow_multiple_apps branch June 11, 2019 10:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot create a second flask application
3 participants