-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample_server.py
55 lines (45 loc) · 1.16 KB
/
sample_server.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
from flask import Flask
from flask import request
app = Flask(__name__)
records = {'1' : {"aaa":"bbb"}}
id = 2
def add(data) :
global records
global id
records[str(id)] = data
id = id + 1
def get(id) :
return records[id]
# GET endpoint - accepts query parameters
@app.get('/api/search')
def search():
searchword = request.args.get('text', '')
result = {}
for k in records.keys() :
for v in records[k].values() :
if v.find(searchword) != -1 :
result[k] = records[k]
return result
# GET endpoint - does not expect any parameters
@app.get('/api/items')
def getall() :
return records
# GET endpoint - accepts path paramters
@app.get('/api/item/<id>')
def get_item(id):
return records[id]
# POST endpoint - expects data JSON
@app.post('/api/items')
def create_entry():
data = request.get_json(force=True)
add(data)
return data
#POST endpoint - expects multipart/form-data
@app.post('/api/upload')
def upload() :
file = request.files['file']
for line in file.stream :
[k, v] = line.decode().rstrip().split(',')
add({k:v})
return("Received file " + file.filename)
#