FastAPI Notes - vimlesh-verma16/Notes GitHub Wiki

FastAPI Official Documentation today: https://fastapi.xiniushu.com/az/

Section: 1 Installing python

First check on cmd by typing python
Download Python from here: https://www.python.org/downloads/
set customization in advanced option : install python for all user
type python on cmd and cmd in vscode terminal


Section: 2 Creating a virtual environments

Creating a python virtual environment : https://docs.python.org/3/library/venv.html

Go to that folder from CMD and run below command

python -m venv fastapi-venv [Create virtual environments]  
fastapi-venv\scripts\activate.bat [Activate the environment]  

Now install packages inside environment

1. pip install fastapi  
2. Check Fastapi version : pip show fastapi 
3. pip install uvicorn  
4. uvicorn --version  

run the server with below command

uvicorn main:app --reload


Open the docs at:

Swagger UI: http://127.0.0.1:8000/docs
ReDoc: http://127.0.0.1:8000/redoc


till section 4 in main copy (For beginning)


link to public repo https://github.com/aruj900/Python/blob/89ffaf9871c806f933d66b7065bc715184675764/FastApi/blog/hashing.py


Workflow order for FastAPI:

Models:
Description: This step involves creating the database models. These models define the structure of the database tables and their relationships.
Action: Make the database and connect the models with each other. It is suggested to delete the database before saving the file to ensure a clean state.

Schemas:
UserBase and ArticleBase:
Description: These schemas are created to define the structure of the data received from the user. They are used when saving data to the database.
UserDisplay and ArticlesDisplay:
Description: These schemas are created to define the structure of the data that will be displayed to the user or sent to the frontend. They ensure that only the necessary fields are exposed.

Database Functions:
DbArticles (Database -> DB -> db_articles):
Description: This section involves creating functions that will handle database operations. These functions will create, read, update, and delete data in the database.
Action: Create functions that will create the data in the database and also create functions that will help in other database operations.

Router:
Description: Define routers for different operations such as create_article or get_article.
Action:

  • Create Router: Define routers for different operations such as create_article or get_article.
  • GET and POST Query Parameters:
  • ArticleDisplay: Use ArticleDisplay schema for query parameters in GET and POST requests. This schema is used to define the structure of the data that will be displayed to the user or sent to the frontend.
  • ArticleBase: Use ArticleBase schema within the function. This schema is used to define the structure of the data received from the user. It is used when saving data to the database.

🚦 HTTP Status Code Categories

🟢 1xx – Informational (Request received, continuing process)

100 Continue: Request is okay so far; continue sending body.
101 Switching Protocols: Server agrees to switch protocols.
102 Processing: Server is processing (WebDAV).

✅ 2xx – Success (Request was successfully received, understood, and accepted)

200 OK: Request successful.
201 Created: Resource created successfully.
202 Accepted: Request accepted, processing not completed.
204 No Content: Successful request but no content to return.

🔁 3xx – Redirection (Further action needed to complete request)

301 Moved Permanently: Resource moved to a new URL.
302 Found: Temporary redirection.
304 Not Modified: Cached version is still valid; no need to resend.

⚠️ 4xx – Client Errors (Request has an error from the client side)

400 Bad Request: Malformed request.
401 Unauthorized: Authentication required.
403 Forbidden: Access denied.
404 Not Found: Resource not found.
408 Request Timeout: Client took too long to send request.

🔥 5xx – Server Errors (Server failed to fulfill a valid request)

500 Internal Server Error: General server error.
502 Bad Gateway: Invalid response from upstream server.
503 Service Unavailable: Server temporarily overloaded or under maintenance.
504 Gateway Timeout: Upstream server didn’t respond in time.

Reacts Commands

Create react_app methods
npx create-react-app test-app
cd test-app
npm start

Generating random ssl key from python from terminal

python -c "import secrets; print(secrets.token_hex(32))"


Authorisation

Create a user first from create_user
then try to authicate from authorisation once successfull it will show a lock sign
then add the same thing as shown is get_article id function

this - > current_user: UserBase = Depends(get_current_user) helps in adding authorisation at all the function

username - > vim for username
password -> vim


Debugging in FastAPI

Debug Restart -> Python Debugger -> FastAPI


Pytest-Testing

pytest #for running tests    
pytest -s #for printing print statments in python