3.2 Start Rest API (Swagger UI)
5. Calculation rules for a Streak
Everybody wants to stop unhealthy habits and create good habits in its place. They are turning to technology for assistance to achieve this. I was tasked to create a backend for a habit tracking application that will assist users to reach their goals.
Python 3.7 or later. (Python 3.10.3 was used for development). Download the latest version here
-
Clone the repository locally
-
Open CommandPrompt on Windows or Terminal on Mac to the cloned folder
-
Go to the root folder (OOFPP-Habits-Phase3) of the project where you will see a setup.py file
-
Run the following command
pip install .
-
If you see
Successfully installed OOFPP-Habits-Phase3-1.0
the application is ready to be used.
From the CommandPrompt or Terminal window, go to the habits_backend folder (OOFPP-Habits-Phase3) of the application. Run the following command to see the cli help menu.
python main.py --help
You should see the following help menu
To load or clear the test data, you can run the following commands
python main.py data-clear
python main.py data-seed
Analysis cli commands
Show all the tracked habits (Habit that was completed at least once. The testing data has habits that was not completed.)
python main.py analyse-tracked-habits
Habits with the same periodicity ('daily','weekly','monthly')
python analyse-equal-periodicity --frequency daily
Longest streak for a specific habit
python main.py analyse-streak-habit --habit_id 1
Longest streak for any habit
python main.py analyse-longest-streak
From the CommandPrompt or Terminal window, go to the habits_backend folder (OOFPP-Habits-Phase3) of the application.
Run the following command to start the REST API.
python main.py start-rest-api
To see the OpenAPI documentation browse to http://127.0.0.1:8000/docs or http://localhost:8000/docs
Using the Swagger interface
- Click on the color coded method (e.g. GET /habits/{id} to open up the interface)
- Click on the Try it out button
- If there are details required, fill it in e.g. habit_id
- Click on the blue Execute button
You can use the seed and clear methods under data section to load/clear testing data
Tests was created using pytest.
To run the tests, follow the following steps
-
Open a CommandPrompt or Terminal window
-
Go to the tests folder in the root (OOFPP-Habits-Phase3) of the application
-
To execute all the analysis tests, run the following command
pytest test_modules_analysis.py
View running tests from Command Prompt
View running tests in the IDE
-
To execute all the cli tests, run the following command
pytest test_main.py
-
To run individual tests, run the following commands
pytest test_modules_analysis.py::test_is_tracked_true pytest test_modules_analysis.py::test_is_tracked_false pytest test_modules_analysis.py::test_is_equal_period_true pytest test_modules_analysis.py::test_is_equal_period_false
- Day:
- Less than or equal to 1 day is counted as a day. (e.g. "2022-04-27T14:17:45" to "2022-04-28T15:57:21" is one day)
- Hours, minutes and seconds are ignored.
- If the day count is more than 1, the streak was broken
- Week:
- Less than or equal to 7 days is treated as a week.
- Hours, minutes and seconds are ignored.
- If the week count is more than 1, the streak was broken
- Month
- Less than or equal to a month is treated as a month
- Days, hours, minutes and seconds are ignored.
- If the year count is more than 1, the streak was broken
- Python version 3.10.3 (Project requirement to use 3.7 or later.)
- PyCharm 2022.1.1 (Popular IDE / source-code editor that runs on Windows, Linux and macOS. I found that it works better than Visual Studio Code for python development.)
- sqlite3 (It is a library that provides lightweight disk-based database to persist the data.)
- pytest (Framework for writing tests.)
- FastAPI (Framework for building APIs with python. This will provide an alternative for the CLI.)
- click (Python library for creating command line interfaces.)
- Pylint (Linting tool that checks for coding errors and enforce coding standards.)
- Swagger UI (Interactive exploration to call and test your API from the browser.)
- SQLAlchemy (Accessing data stored in the database.)
- pydantic (Data parsing and validation.)
- n.a. (n.d). Google Python Style Guide – Naming https://google.github.io/styleguide/pyguide.html#316-naming
- Lutz, M (2013). OOP: The Big Picture, Learning Python 5th Edition. O’Reilly
- Marcus, S (2021, July 16). Test Driven Development with pytest https://stackabuse.com/test-driven-development-with-pytest/
- n.a. (n.d). Zalando RESTful API and Event Guidelines https://opensource.zalando.com/restful-api-guidelines/
- n.a. (n.d). FastAPI - SQL (Relational) Databases https://fastapi.tiangolo.com/tutorial/sql-databases/
- n.a. (n.d). click https://click.palletsprojects.com/en/8.1.x/
- Martin, Robert C (2012, August 13) – The Clean Architecture https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html