encode / apistar

The Web API toolkit. 🛠
https://docs.apistar.com
BSD 3-Clause "New" or "Revised" License
5.57k stars 411 forks source link

Dependency injection on event hooks #615

Closed viniciuschiele closed 5 years ago

viniciuschiele commented 6 years ago

Hi,

It would be good if we could inject arguments to event hook's constructor.

Below is my use case for that.

def create_app(config) -> App:
    client = StatsClient(config.stats_host, config.stats_port)

    app = App(
        components=[
            StatsComponent(client),
        ],
        event_hooks=[
            MetricHook
        ],
        routes=[
            Route('/', 'GET', views.home, name="index"),
        ])

    return app

class MetricHook:
    def __init__(self, client: StatsClient):
         self._client = client

    def on_response(self, route: Route):
        self._client.count("metric", 1)
tomchristie commented 5 years ago

Closing this off given that 0.6 is moving to a framework-agnostic suite of API tools, and will no longer include the server. See https://discuss.apistar.org/t/api-star-as-a-framework-independant-tool/614 and #624.