Posts

Assignment2

Image
 Assignment2 Add a new field to Book and BookRequest called published_date: int (for example, published_date: int = 2012). So,this book as published on the year of 2012. Enhance each Book to now have a published_date Then create a new GET request method to filter by published_date Code: from fastapi import FastAPI , Body from books3 import Book from datavalidatorhelper import BookRequest app = FastAPI () BOOKS = [     Book ( id = 1 , title = "DSA with Java" ,           author = "Puneet Kumar Singh" ,     description = "A comprehensive guide to Data Structures and Algorithms using Java." ,           rating = 5 ,           published_date = 2014 ),     Book ( id = 2 , title = "Python Programming" ,           author = "Puneet Kumar Singh" ,           description = "An introduction to Python programming for beginners." , ...

Delete Operation

Image
 Delete Operation Code from fastapi import FastAPI , Body from books3 import Book from datavalidatorhelper import BookRequest app = FastAPI () BOOKS = [     Book ( id = 1 , title = "DSA with Java" , author = "Puneet Kumar Singh" , description = "A comprehensive guide to Data Structures and Algorithms using Java." , rating = 5 ),     Book ( id = 2 , title = "Python Programming" , author = "Puneet Kumar Singh" , description = "An introduction to Python programming for beginners." , rating = 4 ),     Book ( id = 3 , title = "Machine Learning Basics" , author = "Puneet Kumar Singh" , description = "Understanding the fundamentals of Machine Learning." , rating = 4 ),     Book ( id = 4 , title = "Web Development with FastAPI" , author = "Ramesh Jha" , description = "Building web applications using FastAPI." , rating = 3 ),     Book ( id = 5 , title = "Data...

Update Operation

Image
 Update operation | HTTP PUT Request Method Here we will try to update the existing book with new book and if that book don't exist then we will provide a message book not found. Code: from fastapi import FastAPI , Body from books3 import Book from datavalidatorhelper import BookRequest app = FastAPI () BOOKS = [     Book ( id = 1 , title = "DSA with Java" , author = "Puneet Kumar Singh" , description = "A comprehensive guide to Data Structures and Algorithms using Java." , rating = 5 ),     Book ( id = 2 , title = "Python Programming" , author = "Puneet Kumar Singh" , description = "An introduction to Python programming for beginners." , rating = 4 ),     Book ( id = 3 , title = "Machine Learning Basics" , author = "Puneet Kumar Singh" , description = "Understanding the fundamentals of Machine Learning." , rating = 4 ),     Book ( id = 4 , title = "Web Development with Fas...

Read operations

Image
 Read Operations Fetch a single book Try to create an API endpoint to fetch book based on id. Code: from fastapi import FastAPI , Body from books3 import Book from datavalidatorhelper import BookRequest app = FastAPI () BOOKS = [     Book ( id = 1 , title = "DSA with Java" , author = "Puneet Kumar Singh" , description = "A comprehensive guide to Data Structures and Algorithms using Java." , rating = 5 ),     Book ( id = 2 , title = "Python Programming" , author = "Puneet Kumar Singh" , description = "An introduction to Python programming for beginners." , rating = 4 ),     Book ( id = 3 , title = "Machine Learning Basics" , author = "Puneet Kumar Singh" , description = "Understanding the fundamentals of Machine Learning." , rating = 4 ),     Book ( id = 4 , title = "Web Development with FastAPI" , author = "Ramesh Jha" , description = "Building web application...

Post Request with Pydantic usage (input validator)

Image
Post Request by using Pydantic Pydantic Python library that is used for data modeling, data parsing and has efficient error handling. Pydantics is commonly used as a resource for data validation and how to handle data coming to our FastAPI application. Pydantic implementation mechanism Create a different request model for data validation Field data validation on each variable/element BaseModel is the Pydantics BaseModel class which will provide facility to validate data. Field is the keyword to do it. As it will help to provide constraints for data validation. sample class BookRequest(BaseModel):     id: int = Field(gt=0)     title: str = Field(min_length=3)     author: str = Field(min_length=1)     description: str = Field(min_length=1, max_length=100)     rating: int = Field(gt=0, lt=5) Book Request and Book Conversion We will convert the Pydantic Request into a Book Example for post request method @app.post("/create_book") async def c...