Update flask example

This commit is contained in:
Roman Mogylatov 2020-09-22 22:03:02 -04:00
parent 4fab71c35b
commit 170819c6ed
16 changed files with 69 additions and 74 deletions

View File

@ -11,6 +11,7 @@ follows `Semantic versioning`_
-----
- Add ``wiring`` feature.
- Update ``aiohttp`` example.
- Update ``flask`` example.
3.44.0
------

View File

@ -1,8 +1,8 @@
Flask Dependency Injection Example
==================================
Dependency Injector + Flask Example
===================================
Application ``githubnavigator`` is a `Flask <https://flask.palletsprojects.com/>`_ +
`Dependency Injector <http://python-dependency-injector.ets-labs.org/>`_ application.
`Dependency Injector <http://python-dependency-injector.ets-labs.org/>`_ example application.
.. image:: screenshot.png
@ -90,10 +90,10 @@ The output should be something like:
Name Stmts Miss Cover
----------------------------------------------------
githubnavigator/__init__.py 0 0 100%
githubnavigator/application.py 11 0 100%
githubnavigator/containers.py 13 0 100%
githubnavigator/application.py 15 0 100%
githubnavigator/containers.py 7 0 100%
githubnavigator/services.py 14 0 100%
githubnavigator/tests.py 32 0 100%
githubnavigator/views.py 7 0 100%
githubnavigator/tests.py 34 0 100%
githubnavigator/views.py 9 0 100%
----------------------------------------------------
TOTAL 77 0 100%
TOTAL 79 0 100%

View File

@ -0,0 +1,5 @@
github:
request_timeout: 10
default:
query: "Dependency Injector"
limit: 10

View File

@ -0,0 +1,25 @@
"""Application module."""
from flask import Flask
from flask_bootstrap import Bootstrap
from .containers import Container
from . import views
def create_app():
container = Container()
container.config.from_yaml('config.yml')
container.config.github.auth_token.from_env('GITHUB_TOKEN')
container.wire(modules=[views])
app = Flask(__name__)
app.container = container
bootstrap = Bootstrap()
bootstrap.init_app(app)
app.add_url_rule('/', 'index', views.index)
return app

View File

@ -0,0 +1,22 @@
"""Application containers module."""
from dependency_injector import containers, providers
from github import Github
from . import services
class Container(containers.DeclarativeContainer):
config = providers.Configuration()
github_client = providers.Factory(
Github,
login_or_token=config.github.auth_token,
timeout=config.github.request_timeout,
)
search_service = providers.Factory(
services.SearchService,
github_client=github_client,
)

View File

@ -11,7 +11,9 @@ from .application import create_app
@pytest.fixture
def app():
return create_app()
app = create_app()
yield app
app.container.unwire()
def test_index(client, app):

View File

@ -1,14 +1,16 @@
"""Views module."""
from flask import request, render_template
from dependency_injector.wiring import Provide
from .services import SearchService
from .containers import Container
def index(
search_service: SearchService,
default_query: str,
default_limit: int,
search_service: SearchService = Provide[Container.search_service],
default_query: str = Provide[Container.config.default.query],
default_limit: int = Provide[Container.config.default.limit.as_int()],
):
query = request.args.get('query', default_query)
limit = request.args.get('limit', default_limit, int)

View File

Before

Width:  |  Height:  |  Size: 647 KiB

After

Width:  |  Height:  |  Size: 647 KiB

View File

@ -1,5 +0,0 @@
github:
request_timeout: 10
search:
default_query: "Dependency Injector"
default_limit: 10

View File

@ -1,20 +0,0 @@
"""Application module."""
from .containers import ApplicationContainer
def create_app():
"""Create and return Flask application."""
container = ApplicationContainer()
container.config.from_yaml('config.yml')
container.config.github.auth_token.from_env('GITHUB_TOKEN')
app = container.app()
app.container = container
bootstrap = container.bootstrap()
bootstrap.init_app(app)
app.add_url_rule('/', view_func=container.index_view.as_view())
return app

View File

@ -1,37 +0,0 @@
"""Application containers module."""
from dependency_injector import containers, providers
from dependency_injector.ext import flask
from flask import Flask
from flask_bootstrap import Bootstrap
from github import Github
from . import views, services
class ApplicationContainer(containers.DeclarativeContainer):
"""Application container."""
app = flask.Application(Flask, __name__)
bootstrap = flask.Extension(Bootstrap)
config = providers.Configuration()
github_client = providers.Factory(
Github,
login_or_token=config.github.auth_token,
timeout=config.github.request_timeout,
)
search_service = providers.Factory(
services.SearchService,
github_client=github_client,
)
index_view = flask.View(
views.index,
search_service=search_service,
default_query=config.search.default_query,
default_limit=config.search.default_limit,
)