-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
92 lines (76 loc) · 2.37 KB
/
Copy pathserver.py
File metadata and controls
92 lines (76 loc) · 2.37 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from bottle import Bottle, run, request, response
from controller import spreadsheet as ss
app = Bottle()
@app.post('/api/v1/cell/append')
def post_cell_append():
"""
Append a cell with 'value' to the next available row in 'col' of
'worksheetKey'.
POST a JSON with 'worksheetKey', 'col', and 'value' keys.
HTTP request header must set Content-Type to application/json.
"""
data = request.json
attr = ('worksheetKey', 'col', 'value')
if data is not None and all(key in data for key in attr):
worksheet = data[attr[0]]
col = data[attr[1]]
row = ss.next_append_row(worksheet, col)
value = data[attr[2]]
ss.write_cell(worksheet, row, col, value)
response.status = '201 Created'
else:
response.status = '400 Bad Request'
return response
@app.post('/api/v1/cell')
def post_cell():
"""
POST to a specific cell to write value into location specified
"""
data = request.json
attr = ('worksheetKey', 'row', 'col', 'value')
if data is not None and all(key in data for key in attr):
worksheet = data[attr[0]]
row = data[attr[1]]
col = data[attr[2]]
value = data[attr[3]]
ss.write_cell(worksheet, row, col, value)
response.status = '201 Created'
else:
response.status = '400 Bad Request'
return response
@app.get('/api/v1/cell')
def get_cell():
"""
GET a specific cell
"""
q = request.query.decode()
worksheet = q['worksheetKey']
row = q['row']
col = q['col']
value = ss.read_cell(worksheet, row, col)
response.status = '200 OK'
response.content_type = 'application/json'
return {'value': value,
'row': row,
'col': col,
'worksheetKey': worksheet}
@app.get('/api/v1/column')
def get_column():
"""
GET a specific column
"""
q = request.query.decode()
worksheet = q['worksheetKey']
col = q['col']
values = ss.read_column(worksheet, col)
if values is not None:
response.status = '200 OK'
response.content_type = 'application/json'
return {'values': values,
'col': col,
'worksheetKey': worksheet}
else:
response.status = '400 Bad Request'
return response
if __name__ == "__main__":
run(app, host='localhost', port=8080, reloader=True, debug=True)