[Python] python flask 간단 예제
Flask 실습
CODE
from flask import Flask
from flask_restful import reqparse, abort, Api, Resource
app = Flask(__name__)
api = Api(app)
ORDERS = {
'order1' : {'coffee' : 'americano'},
'order2': {'coffee': 'latte'},
'order3': {'coffee': 'espresso'},
}
def abort_if_todo_doesnt_exist(order_id):
if order_id not in ORDERS:
abort(404, message = "STARBUCKS ORDER {} doesn't exist".format(order_id))
parser = reqparse.RequestParser()
parser.add_argument('coffee')
class Order(Resource):
def get(self, order_id):
abort_if_todo_doesnt_exist(order_id)
return ORDERS[order_id]
def delete(self, order_id):
abort_if_todo_doesnt_exist(order_id)
del ORDERS[order_id]
return '', 204
def put(self, order_id):
args = parser.parse_args()
coffee= {'coffee' : args['coffee']}
ORDERS[order_id] = coffee
return coffee, 201
class OrderList(Resource):
def get(self):
return ORDERS;
def post(self):
args = parser.parse_args()
order_id = int(max(ORDERS.keys()).lstrip()) + 1
order_id = 'order_id%i' % order_id
ORDERS[order_id] = {'coffee' : args['coffee']}
return ORDERS[order_id], 201
api.add_resource(OrderList, '/orderlist')
api.add_resource(Order, '/order/<order_id>')
if __name__ == '__main__':
app.run(debug=True)
URL TEST
1) GET
GET http://127.0.0.1:5000/orderlist
GET http://127.0.0.1:5000/order/order3
2) DELETE
DELETE http://127.0.0.1:5000/order/order2
2) PUT
PUT http://127.0.0.1:5000/order/order2?coffee=iceamericano
UPDATE
PUT http://127.0.0.1:5000/order/order2?coffee=latte
댓글남기기