Skip to content

Instantly share code, notes, and snippets.

@ajford
Last active September 7, 2019 03:56
Show Gist options
  • Save ajford/6023148 to your computer and use it in GitHub Desktop.
Save ajford/6023148 to your computer and use it in GitHub Desktop.
Flask MethodView test
from flask import (Flask, Blueprint)
from flask.views import MethodView
DEBUG = True
app = Flask(__name__)
class TestAPI(MethodView):
"Testing Flask MethodView"
def get(self,id=None):
if id is None:
return "ID IS NONE"
else:
return "ID IS %s"%id
def post(self):
return "NEW OBJECT CREATED"
def delete(self, id):
return "DELETED", 204
app.add_url_rule('/test/',view_func = TestAPI.as_view('test_api'),
methods=['POST'])
app.add_url_rule('/test/', defaults={'id':None},
view_func = TestAPI.as_view('test_api'), methods=['GET'])
app.add_url_rule('/test/<int:id>', view_func = TestAPI.as_view('test_api'),
methods=['GET','PUT','DELETE'])
if __name__ == "__main__":
app.run()
from flask import (Flask, Blueprint)
from flask.views import MethodView
DEBUG = True
app = Flask(__name__)
class TestAPI(MethodView):
"Testing Flask MethodView"
def get(self,id=None):
if id is None:
return "ID IS NONE"
else:
return "ID IS %s"%id
def post(self):
return "NEW OBJECT CREATED"
def delete(self, id):
return "DELETED", 204
test_view = TestAPI.as_view('test_api')
app.add_url_rule('/test/',view_func = test_view, methods=['POST'])
app.add_url_rule('/test/', defaults={'id':None}, view_func=test_view,
methods=['GET'])
app.add_url_rule('/test/<int:id>', view_func = test_view,
methods=['GET','PUT','DELETE'])
if __name__ == "__main__":
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment