A rest API to manage calories consumed by users, written in Python.
- Libraries/Tools used
- Features
- Roles
- Database
- API Endpoints
- Core Calories Logic
- Installation
- Directory
- Testing
- References
- FastAPI - for creating REST APIs
- SQLite - for storing user data and login sessions
- Pytest and Playwright - for testing
- Black - for reformatting
- User can register and login.
- User can add and delete the calories consumed by him/her.
- Daily calorie goal can be set by the user and can regularly monitor them.
- User can also view the calories consumed by him/her in a particular date range.
- Admins and user managers can control other user's calorie data and their daily goals.
There are three roles
-
User: Users can enter their own calorie data and view them. Not allowed to view or edit any other user's data.
-
User Manager: User manager or simply manager can view and edit any user's calorie data. They can also view and edit any user's daily calorie goal.
-
Admin: Admins can do everything that a manager can do along with the priviledge of removing any user's / manager's data.
Note: For simplicity, the roles are depicted by integers. 0 stands for user, 1 for manager, 2 for admin. Refer utilities/roles
for more.
-
user
: stores athe user data with the following fieldsID
: unique UUID for each userusername
: username of the userpassword
: hashed password of the useremail
: email of the userrole
: role of the user (user, manager, admin)
-
sessions
: stores the session with the following fieldsid
: unique id of the sessionjwt_token
: jwt token of the sessionusername
: user signed in
-
expected_calories
: stores the daily calorie goal of the user with the following fieldsID
: unique id of the goalusername
: username of the usercalories
: daily calorie goal of the userdate
: date of the goal
-
<username>_calorie
: stores the list of food consumed along with the date and time with the following fieldsID
: unique id of the foodfood_name
: name of the foodcalories
: calories of the fooddate
: date when the food was consumedtime
: time when the food was consumed
This table is created after signing up.
Resource | POST | GET | DELETE |
---|---|---|---|
/auth/signup/ |
Create a new user with any of the following roles - user , manager , admin |
- | - |
/auth/login/ |
Login with username and password |
- | - |
/auth/logout/ |
Logs out and deletes the session | - | - |
/calories/entry/ |
Enter a new food with valid calorie. If calorie is not provided, the calorie is automatically taken from the nutritionix api. | - | Deletes the food entered before with food-id belonging to username (optional) |
/calories/list |
- | Get the list of food consumed from from_date to to_date by username . All the params are optional, by-default the list is shown on daily basis by logged-in user. |
- |
/calories/goal/ |
sets the calorie goal for the day. | Shows the calorie goal summed up from from-date to to-date of username . params=status just shows whether you have fulfilled the goal or not. |
- |
Note:
- The
username
param / json key is only accessible by the admins and the user managers. That means users can't change or view other's progress.
The request / params model is shown below
/auth/signup/
- JSON Request
{
"username": <unique-string>,
"name": <string>,
"email": <email>,
"password": <password>,
"role": <admin/manager/user>
}
- JSON Response
{
"username": <string>,
"email": <string>,
"role": <string>,
"msg": "User created successfully"
}
/auth/login/
- Form body request
Key | Value |
---|---|
username | string |
password | string |
- JSON Response
{
"access_token": <JWT-Token>,
"msg": "Logged in"
}
-
/auth/logout/
- no request body needed -
/calories/entry/
POST
- JSON Request
{
"food_name": <string>,
"calories": <float/integer>
"username": <string>
}
- JSON Response
{
"payload": {
"food_name": "bacon",
"calories": 161.46,
"date": "2023-06-18",
"time": "11:20:08.909626"
},
"msg": "Food entered successfully",
"goal_reached": null
}
DELETE
- Params
Key | Value |
---|---|
username | string |
food_id | integer |
/calories/goal/
GET
- Params
Key | Value |
---|---|
username | string |
from_date | date |
to_date | date |
- JSON Response
{
"msg": 3500
}
POST
- JSON Request
{
"calories": <int>,
"username": <string>
}
- JSON Response
{
"payload": {
"username": <string>,
"calories": 3500,
"date": "2023-06-18"
},
"msg": "Limit set successfully"
}
/calories/list/
GET
- Params
Key | Value |
---|---|
username | string |
from_date | date |
to_date | date |
- JSON Response
{
"msg": [
{
"id": 2,
"food_name": "bacon",
"calories": 100
},
{
"id": 3,
"food_name": "bacon",
"calories": 100
},
]
}
/admin/delete/{username}
- enter the username and POST.
- All the endpoints are authenticated. Users need to create an account and login.
- The name of the food as well as the calories can be mentioned or will be fetched automatically from nutritionix API if not specified. While POSTing the food details,
goal_reached
status will also be shown whether you have reached the goal (true) or not (false). If no daily goal is set, then it will show null. - Goals can be set on a daily basis and the total calorie goals can be shown on a daily basis or for a particular period of days. For example, you can see how much you have set the calorie goal for 5 days cumulative and as well as the
goal_reached
status whether you have reached the goal by summing up those 5 days food data. - Admins can enter, delete and view other user's and manager's food data and daily goals. Same goes for user managers, but they can't change an admin's data.
- Clone this repository
- Install all the libraries / dependencies
pip install -r requirements.txt
- Rename the file
secrets.demo.cfg
tosecrets.cfg
and enter the required values.
Note:
- To generate the secret key to encode the JWT token, execute the following command and paste it in the config file.
openssl rand -hex 32
- To get the nutritionix api key, create an account in nutritionix and get the api key from the developer portal. Don't exclose the keys in single/double quotes in config file
- Run the server, the api will be live in
https://localhost:8000/
uvicorn main:app --reload
├── auth/
├── db/
├── routes/
├── schemas/
├── tests/
│ ├── e2e/
│ └── unit/
└── utilities/
- Install a dependency for pytest plugin for playwright
pip install pytest-playwright
- To run all the unit tests, run the following command
python -m pytest tests/
-
All the tests are written with the convention
test_<function_name>.py
and the functions astest_<function_name>
. -
Unit tests are inside
unit/
directory and e2e tests are insidee2e/
directory.