A simple python api developed using fastapi to learn basic syntax and types and specially pydantic
Python version - 3.8
FastAPI - 0.63.0
sqlalchemy - 1.4.9
pydantic - 1.8.2
This api allows a user to create an account, and add contacts to his account.
-
Clone the repository
cd phonebook
-
Create a virtual environment with python3.8
reference: https://stackoverflow.com/questions/1534210/use-different-python-version-with-virtualenv -
pip install -r requirements.txt
-
uvicorn main:phonebook --reload
-
Token Auth: What is Token Auth?
A very simple authorization using a unique string per user that is provided while creating one's account.
Unique numeric string will be generated using a hash function, that will be provided via response for the user account creation request. That token has to provided whenever the user wants to make any changes to his/her data. -
Relational database Schema:
User:
id: int
name: string
mail: string
phonenumber: string
premium: bool
contacts: list of Contacts
Contact:
id: int
name: string
mail: string
phonenumber: string
owner_id: int user_id -
SQLite database is used for this mini project, it is advisable to use postgres or other suitable databases for production purposes.
-
Validation for mail and phone number.
-
Create user - POST
http://ip:port/users/addUser/
Request Body:
{
"name": "string",
"email": "string",
"phonenumber": "string"
} -
Get user data - GET
http://ip:port/users/{param}/
param can be mail or phone number
token is for authorization for the requested user
Request Body:
{
"param": "string"
"token": "string"
} -
Add contact details - POST
http://ip:port/users/{param}/addContact
param can be mail or phone number
token must be provided for authorization
Request Body:
{
"name": "string",
"email": "string",
"phonenumber": "string"
"token" : "string"
} -
Get contacts of a user - GET
http://ip:port/users/{param}/contacts
param can be mail or phone number
token must be provided for authorization
Request Body:
{
"param": "string"
"token" : "string"
} -
Update user email - PUT
http://ip:port/users/{param}/updateUserEmail
param can be mail or phone number for identifying the user
token must be provided for authorization
Request Body:
{
"param": "string"
"token" : "string"
"update_param" : "string" (to be updated email)
} -
Update user phone number - PUT
http://ip:port/users/{param}/updateUserPhonenumber
param can be mail or phone number for identifying the user
token must be provided for authorization
Request Body:
{
"param": "string"
"token" : "string"
"update_param" : "string" (to be updated phone number)
} -
Delete user - DELETE
http://ip:port/users/{param}/deleteUser
param can be mail or phone number for identifying the user
token must be provided for authorization
Request Body:
{
"param": "string"
"token" : "string"
} -
Update user contact email - PUT
http://ip:port/users/{param}/updateContactEmail
param can be mail or phone number for identifying the user
token must be provided for authorization
Request Body:
{
"param": "string"
"token" : "string"
"email" : "string"
"newmail" : "string"
} -
Update user contact phone number - PUT
http://ip:port/users/{param}/updateContactPhonenumber
param can be mail or phone number for identifying the user
token must be provided for authorization
Request Body:
{
"param": "string"
"token" : "string"
"phonenumber" : "string"
"newphonenumber" : "string"
} -
Delete user contact - PUT
http://ip:port/users/{param}/deleteUserContact
param can be mail or phone number for identifying the user
token must be provided for authorization
contact_param can be mail or phone number for identifying contact of the user
Request Body:
{
"param": "string"
"token" : "string"
"contact_param" : "string"
} -
Make Premium User - PUT
http://ip:port/admin/{param}/premiumUser
param can be mail or phone number for identifying the user
Admin token must be provided for authorization
Request Body:
{
"param": "string"
"admin_token" : "string"
} -
Get user (for premium users)- GET
http://ip:port/premiumUser/getUser/{param}
param can be mail or phone number for identifying the user
Premium user param can be mail or phone number for identifying the premium user
Premium token must be provided for authorization
Request Body:
{
"param": "string"
"premium_user_param" : "string"
"premium_user_token" : "string"
}
Used Http Error Exceptions for error handling
(https://en.wikipedia.org/wiki/List_of_HTTP_status_codes)
Basic exceptions were used.