External Exam Download Resources Web Applications Games Recycle Bin

Building a simple API

install RESTful for Flask:

pip3 install flask_restful

simplest_API.py

from flask import *
from flask_restful import *

app = Flask(__name__)
api = Api(app)

food = {
    "burgers": {
      "beef": ["big mac", "quarter pounder"],
      "chicken": ["mc'chicken"],
    },
    "chips": ["large","small"],
}

class everything(Resource):    
    def get(self):    
        return food
         
class category(Resource):    
    def get(self, category):    
        return food[category]

api.add_resource(everything, '/')
api.add_resource(category, '/<category>/')
app.run(host='127.0.0.1',port='5000')

# try in web browser:
# http://127.0.0.1:5000/
# http://127.0.0.1:5000/burgers/
# http://127.0.0.1:5000/chips/