Last active
April 3, 2018 18:03
-
-
Save ohoachuck/720357a01009860e36c77c52afc6be05 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
''' | |
Little snippet to simulate a REST API webserver | |
Author: Olivier HO-A-CHUCK | |
Date: April 2018 | |
Usage: | |
install FLASK (pip install Flask) | |
write your quick app.routes see samples | |
run ./rest.py | |
Note: should work for python 2.x and pyhon 3.x | |
''' | |
from flask import Flask, jsonify, request, make_response | |
app = Flask(__name__) | |
@app.route("/") | |
def hello(): | |
return "use /api/v1.0/askquestion" | |
@app.route("/api/v1.0/askquestion") | |
def rest_api(): | |
question = request.args.get('question') | |
sessionid = request.args.get('sessionid') | |
return "question is : " + question + " | " + "sessionid is : " + sessionid | |
@app.route('/api/v1.0/users/<user_id>', methods = ['GET', 'POST', 'DELETE']) | |
def user(user_id): | |
if request.method == 'GET': | |
"""return the information for <user_id>""" | |
return "I know this user! (" + user_id + ") He's a nice guy." | |
if request.method == 'POST': | |
"""modify/update the information for <user_id>""" | |
# you can use <user_id>, which is a str but could | |
# changed to be int or whatever you want, along | |
# with your lxml knowledge to make the required | |
# changes | |
data = request.form # a multidict containing POST data | |
print("Applying changes on User " + user_id) | |
return jsonify(data) | |
if request.method == 'DELETE': | |
"""delete user with ID <user_id>""" | |
return "user " + user_id + " have been deleted ! ;(" | |
else: | |
# POST Error 405 Method Not Allowed | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment