-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
58 lines (48 loc) · 1.71 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Reference: https://stackabuse.com/deploying-a-flask-application-to-heroku/
# Github: https://github.com/RSwarnkar/nse-volume-api
# Profile command:
# web: gunicorn app:app
# app.py
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/getmsg/', methods=['GET'])
def respond():
# Retrieve the name from url parameter
name = request.args.get("name", None)
# For debugging
print(f"got name {name}")
response = {}
# Check if user sent a name at all
if not name:
response["ERROR"] = "no name found, please send a name."
# Check if the user entered a number not a name
elif str(name).isdigit():
response["ERROR"] = "name can't be numeric."
# Now the user entered a valid name
else:
response["MESSAGE"] = f"Welcome {name} to our awesome platform!!"
# Return the response in json format
return jsonify(response)
@app.route('/post/', methods=['POST'])
def post_something():
param = request.form.get('name')
print(param)
# You can add the test cases you made in the previous function, but in our case here you are just testing the POST functionality
if param:
return jsonify({
"Message": f"Welcome {name} to our awesome platform!!",
# Add this option to distinct the POST request
"METHOD" : "POST"
})
else:
return jsonify({
"ERROR": "no name found, please send a name."
})
# A welcome message to test our server
@app.route('/')
def index():
return "<h1>Welcome to our server !!</h1>"
if __name__ == '__main__':
# Threaded option to enable multiple instances for multiple user access support
#app.run(threaded=True, port=5000)
app.run()