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"