forked from KeithGalli/python-api-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_template.py
More file actions
41 lines (34 loc) · 1.28 KB
/
main_template.py
File metadata and controls
41 lines (34 loc) · 1.28 KB
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
import librosa
from flask import Flask, request, jsonify
from flask_caching import Cache
from werkzeug.datastructures.structures import ImmutableDict, ImmutableMultiDict, TypeConversionDict
app = Flask(__name__)
cache = Cache(app, config={'CACHE_TYPE': 'simple'})
# Default value
cache.set('v', 101)
def returnbin(val):
return bin(val)[2:] # [2:] removes the '0b' prefix
@app.route('/', methods=['GET', 'POST'])
def process_request():
# Check if TransmitterId cookie is set and equals 1
# if transmitter_id or transmitter_id == '1':
if request.method == 'GET':
print("GET")
# Increment 'v' and return its binary value
v = cache.get('v')
return jsonify({'value': returnbin(v)})
if request.method == 'POST':
print("POST")
# Get the POST value
value = request.form.get('value')
print(value)
# Convert binary to decimal
decimal_value = int(value,2)
# Set 'v' to the decimal value
cache.set('v', decimal_value)
return jsonify({'value': returnbin(v)})
return jsonify({'value': returnbin(v)})
# If TransmitterId is not set or not equal to 1, return error response
# return jsonify({'value': '0'})
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5000)