2020-10-09 22:16:27 +03:00
|
|
|
Flask + Dependency Injector Example
|
|
|
|
===================================
|
2020-07-11 19:15:00 +03:00
|
|
|
|
2020-10-09 22:16:27 +03:00
|
|
|
This is a `Flask <https://flask.palletsprojects.com/>`_ +
|
2020-10-20 00:18:39 +03:00
|
|
|
`Dependency Injector <https://python-dependency-injector.ets-labs.org/>`_ example application.
|
2020-10-09 22:16:27 +03:00
|
|
|
|
|
|
|
The example application helps to search for repositories on the Github.
|
2020-07-11 19:15:00 +03:00
|
|
|
|
2020-07-20 23:58:18 +03:00
|
|
|
.. image:: screenshot.png
|
|
|
|
|
2020-07-11 19:15:00 +03:00
|
|
|
Run
|
|
|
|
---
|
|
|
|
|
2020-07-14 05:45:15 +03:00
|
|
|
Create virtual environment:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
virtualenv venv
|
|
|
|
. venv/bin/activate
|
|
|
|
|
2020-07-11 19:15:00 +03:00
|
|
|
Install requirements:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
pip install -r requirements.txt
|
|
|
|
|
|
|
|
To run the application do:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2020-07-14 05:45:15 +03:00
|
|
|
export FLASK_APP=githubnavigator.application
|
2020-07-11 19:15:00 +03:00
|
|
|
export FLASK_ENV=development
|
|
|
|
flask run
|
|
|
|
|
|
|
|
The output should be something like:
|
|
|
|
|
|
|
|
.. code-block::
|
|
|
|
|
2020-07-14 05:45:15 +03:00
|
|
|
* Serving Flask app "githubnavigator.application" (lazy loading)
|
2020-07-11 19:15:00 +03:00
|
|
|
* Environment: development
|
|
|
|
* Debug mode: on
|
|
|
|
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
|
|
|
|
* Restarting with fsevents reloader
|
|
|
|
* Debugger is active!
|
|
|
|
* Debugger PIN: 473-587-859
|
|
|
|
|
|
|
|
After that visit http://127.0.0.1:5000/ in your browser.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
2020-10-09 22:16:27 +03:00
|
|
|
Github has a rate limit. When the rate limit is exceed you will see an exception
|
2020-07-11 19:15:00 +03:00
|
|
|
``github.GithubException.RateLimitExceededException``. For unauthenticated requests, the rate
|
|
|
|
limit allows for up to 60 requests per hour. To extend the limit to 5000 requests per hour you
|
|
|
|
need to set personal access token.
|
|
|
|
|
|
|
|
It's easy:
|
|
|
|
|
|
|
|
- Follow this `guide <https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token>`_ to create a token.
|
|
|
|
- Set a token to the environment variable:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
export GITHUB_TOKEN=<your token>
|
|
|
|
|
|
|
|
- Restart the app with ``flask run``
|
|
|
|
|
|
|
|
`Read more on Github rate limit <https://developer.github.com/v3/#rate-limiting>`_
|
|
|
|
|
|
|
|
Test
|
|
|
|
----
|
|
|
|
|
2020-07-29 02:19:05 +03:00
|
|
|
This application comes with the unit tests.
|
2020-07-11 19:15:00 +03:00
|
|
|
|
|
|
|
To run the tests do:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
py.test githubnavigator/tests.py --cov=githubnavigator
|
|
|
|
|
|
|
|
The output should be something like:
|
|
|
|
|
|
|
|
.. code-block::
|
|
|
|
|
2021-11-01 03:31:39 +03:00
|
|
|
platform darwin -- Python 3.10.0, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
|
|
|
|
plugins: cov-3.0.0, flask-1.2.0
|
2020-07-11 19:15:00 +03:00
|
|
|
collected 2 items
|
|
|
|
|
|
|
|
githubnavigator/tests.py .. [100%]
|
|
|
|
|
2021-11-01 03:31:39 +03:00
|
|
|
---------- coverage: platform darwin, python 3.10.0-final-0 ----------
|
2020-07-11 19:15:00 +03:00
|
|
|
Name Stmts Miss Cover
|
|
|
|
----------------------------------------------------
|
|
|
|
githubnavigator/__init__.py 0 0 100%
|
2021-11-01 03:31:39 +03:00
|
|
|
githubnavigator/application.py 13 0 100%
|
|
|
|
githubnavigator/containers.py 8 0 100%
|
2020-07-14 05:45:15 +03:00
|
|
|
githubnavigator/services.py 14 0 100%
|
2020-10-09 22:16:27 +03:00
|
|
|
githubnavigator/tests.py 34 0 100%
|
2020-11-16 00:06:42 +03:00
|
|
|
githubnavigator/views.py 10 0 100%
|
2020-07-11 19:15:00 +03:00
|
|
|
----------------------------------------------------
|
2021-11-01 03:31:39 +03:00
|
|
|
TOTAL 79 0 100%
|