Skip to content

Conversation

@dato
Copy link

@datodato commented Jun 17, 2020

A useful pattern with Flask extensions is to instantiate the extension in
a separate module, then import that in wsgi.py, calling ext.init_app(app)
there. This way, in the case of github_webhook e.g., webhooks can be defined
(decorated) in a separate module, before the Flask app has even been created.

For this to work, however, self._hooks (which is used by the decorator) must
be defined even if no app was given at instantiationtime. Previously, it was
only being created as part of init_app().

This commit:

  • creates self._hooks early, in Webhook.init

  • defines self.app in init_app(), so that it does not stay as None if
    init_app() is called in a delayed fashion.

On the other hand, self._logger is kept in init_app(), so that Flask's log
system has had a chance to initialize. It is not used by the decorator.

Signed-off-by: Dato Simó dato@net.com.org.es

A useful pattern with Flask extensions is to instantiate the extension in a separate module, then import that in wsgi.py, calling ext.init_app(app) there. This way, in the case of github_webhook e.g., webhooks can be defined (decorated) in a separate module, before the Flask app has even been created. For this to work, however, self._hooks (which is used by the decorator) must be defined even if no app was given at instantiationtime. Previously, it was only being created as part of init_app(). This commit: - creates self._hooks early, in Webhook.__init__ - defines self.app in init_app(), so that it does not stay as None if init_app() is called in a delayed fashion. On the other hand, self._logger is kept in init_app(), so that Flask's log system has had a chance to initialize. It is not used by the decorator. Signed-off-by: Dato Simó <dato@net.com.org.es>
@datodatoforce-pushed the hooks_before_init_app branch from 106f74e to 65a962dCompareJune 17, 2020 15:30
@shawalli
Copy link

shawalli commented Apr 10, 2021

Does this library do what you want?

@dato
Copy link
Author

dato commented Aug 10, 2021

I'll check it out, thank you

@datodato closed this Aug 10, 2021
Sign up for freeto 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.

2 participants

@dato@shawalli