Last active
September 7, 2019 03:56
-
-
Save ajford/6023148 to your computer and use it in GitHub Desktop.
Flask MethodView test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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