AllFlaskProjectsPython

Flask API Part 3: Create Products & URLs | Create Json API Using Flask | Python Flask Project

Flask API Part 3: Create Products & URLs | Create Json API Using Flask | Python Flask Project

Now It Is Redy to run the api app

Lets Create the Flask API Urls To Run This App

Now You Can add in

Product.py

 def getProducts(self,limit):
        #write query for get data from database table view/show data
        self.cursor.execute("SELECT * FROM product ORDER BY id ASC LIMIT %s",limit)
        aData=self.cursor.fetchall()
        payload=[]
        content={}
        for result in aData:
            content = {
                'id':result[0],
                'title' :result[1],
                'description': result[2],
                'image_url':result[3],
                'price':result[4],
                'review':result[5],
                'rating':result[6],
                'link':result[7],
                'category':result[8],
            }
            payload.append(content)
            content={}
        return payload
def getSingleProduct(self,id:int):
        self.cursor.execute("SELECT * FROM product where id=%s",id)
        aData=self.cursor.fetchall()
        payload=[]
        content={}
        for result in aData:
            content = {
                'id':result[0],
                'title' :result[1],
                'description': result[2],
                'image_url':result[3],
                'price':result[4],
                'review':result[5],
                'rating':result[6],
                'link':result[7],
                'category':result[8],
            }
            payload.append(content)
            content={}
        return payload
    def __del__(self):
        self.cursor.close()

Create route For This functions In

flask app add route /product

and /product/1 for single product add /product/id

App.py

@app.route('/product', methods=['GET','POST'])
@cross_origin(origins="*",headers=['content-type'])
def productList():
    if(request.method=='GET'):
        if(request.args):
            limit=request.args.get('limit',type=int)
        else:
            limit=15
        data=pobj.getProducts(limit)         #we have to add responce code like 200 for succussess 400 error 
        if len(data):
            responce = jsonify({
                "result":data,
                "status":200
            })
        else:
            responce = jsonify({
                "result":data,
                "status":400
            })
        return responce
    elif(request.method=='POST'):
        data=request.json
        aData=pobj.createProduct(data['data'])
        if aData==200:
            responce = jsonify({
                "result":aData,
                "status":200,
                "massage":"created successfully"
                
            })
        else:
            responce = jsonify({
                "result":aData,
                "status":400,
                "massage":"faild to create"
            })
        return responce
#we are update data end delete the product using this url
@app.route('/product/<int:pid>',methods=['GET','PUT','DELETE','PATCH','OPTIONS'])
@cross_origin(origins="*", headers=['content-type'])
def productchenge(pid):
    if(request.method=='PUT'):
        data=request.json
        aData=pobj.updateProduct(data,pid)
        if(aData==200):
            responce=jsonify({
                "result":aData,
                "status":200,
                "massage":"update Successfully"
            })
        else:
            responce=jsonify({
                "result":aData,
                "status":400,
                "massage":"Faild to update"
            })
        return responce
    elif(request.method=='DELETE'):
        aData=pobj.deleteProduct(pid)
        if(aData==200):
            responce=jsonify({
                "result":aData,
                "status":200,
                "massage":"DELETE Successfully"
            })
        else:
            responce=jsonify({
                "result":aData,
                "status":400,
                "massage":"Faild to DELETE"
            })
        return responce
    elif(request.method=='PATCH'):
        return "Patch http method"
    elif(request.method=='GET'):
        data=pobj.getSingleProduct(pid)         #we have to add responce code like 200 for succussess 400 error 
        if len(data):
            responce = jsonify({
                "result":data,
                "status":200
            })
        else:
            responce = jsonify({
                "result":data,
                "status":400
            })
        return responce
    else:
        return "options http method"

Leave a Reply

10 Best Artificial Intelligence Software|artificial intelligence tools 5 nft games to earn money | Best NFT games for earn crypto Earn Money From Minting NFTs| How to mint NFT for free Top 10 Things You Need To Know About Python List | python lists functions 10 Popular PHP frameworks for web developers| best php frameworks 12 Tips On How To Become a Python Developer | python For beginner 12 Best Nodejs Frameworks for App Development in 2022 how to create google web stories, Steps to create web stories Top 10 Features in Angular 13 Every Developer Should Know | Angular 13 Features 10 Best Angular UI Libraries | angular ui components | angular Project 10 Best Web Development Frameworks in 2022 Frontend & Backend 18 Best Open-Source and Free Database Software | best database software for beginners Top 10+ Best Java IDEs & Online Java Compilers | best java ide for beginners top 10 besic to andvance java books |java books for beginer Top 5 Themes For Blogger, professional blogger theme download BEST Python Courses Online,Top 10 Courses to Learn Python in 2022 Top 13 python libraries for data science