1
|
|
|
""" |
2
|
|
|
Endpoints: |
3
|
|
|
---------- |
4
|
|
|
|
5
|
|
|
- **/api/1.0/samples/ - **GET** List all samples |
6
|
|
|
- **/api/1.0/samples/<id> - **GET** Detailed information about sample *id* |
7
|
|
|
""" |
8
|
|
|
from flask import jsonify, request, url_for, current_app |
9
|
|
|
|
10
|
|
|
from ..models import Sample |
11
|
|
|
from .blueprint import api |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
@api.route('/samples/') |
15
|
|
|
def get_samples(): |
16
|
|
|
""" |
17
|
|
|
List all samples |
18
|
|
|
|
19
|
|
|
Example response: :: |
20
|
|
|
|
21
|
|
|
{ "count": 10696, |
22
|
|
|
"next": "http://riverflo.ws/api/1.0/samples/?page=2", |
23
|
|
|
"prev": null, |
24
|
|
|
"samples": [ |
25
|
|
|
{ "datetime": "Thu, 05 Jun 2014 13:50:27 GMT", |
26
|
|
|
"id": 52, |
27
|
|
|
"sensor": { |
28
|
|
|
"gage": { |
29
|
|
|
"id": 2, |
30
|
|
|
"location": "Wild River near RT 2 in Gilead Maine", |
31
|
|
|
"name": "Wild River at Gilead", |
32
|
|
|
"url": "http://riverflo.ws/api/1.0/gages/2" |
33
|
|
|
}, |
34
|
|
|
"id": 5, |
35
|
|
|
"type": "level", |
36
|
|
|
"url": "http://riverflo.ws/api/1.0/sensors/5" |
37
|
|
|
}, |
38
|
|
|
"url": "http://riverflo.ws/api/1.0/samples/52", |
39
|
|
|
"value": 24.0 |
40
|
|
|
}, |
41
|
|
|
{ "datetime": "Thu, 05 Jun 2014 13:50:27 GMT", |
42
|
|
|
"id": 53, |
43
|
|
|
"sensor": { |
44
|
|
|
"gage": { |
45
|
|
|
"id": 2, |
46
|
|
|
"location": "Wild River near RT 2 in Gilead Maine", |
47
|
|
|
"name": "Wild River at Gilead", |
48
|
|
|
"url": "http://127.0.0.1:5000/api/1.0/gages/2" |
49
|
|
|
}, |
50
|
|
|
"id": 8, |
51
|
|
|
"type": "volts", |
52
|
|
|
"url": "http://riverflo.ws/api/1.0/sensors/8" |
53
|
|
|
}, |
54
|
|
|
"url": "http://riverflo.ws/api/1.0/samples/53", |
55
|
|
|
"value": 4.096 |
56
|
|
|
} |
57
|
|
|
] |
58
|
|
|
} |
59
|
|
|
""" |
60
|
|
|
page = request.args.get('page', 1, type=int) |
61
|
|
|
pagination = Sample.query.paginate(page, |
62
|
|
|
per_page=current_app.config['API_GAGES_PER_PAGE'], # noqa |
63
|
|
|
error_out=False) |
64
|
|
|
samples = pagination.items |
65
|
|
|
prev = None |
66
|
|
|
if pagination.has_prev: |
67
|
|
|
prev = url_for('.get_samples', page=page-1, _external=True) |
68
|
|
|
next_p = None |
69
|
|
|
if pagination.has_next: |
70
|
|
|
next_p = url_for('.get_samples', page=page+1, _external=True) |
71
|
|
|
return jsonify({ |
72
|
|
|
'samples': [sample.to_json() for sample in samples], |
73
|
|
|
'prev': prev, |
74
|
|
|
'next': next_p, |
75
|
|
|
'count': pagination.total |
76
|
|
|
}) |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
@api.route('/samples/<int:sid>') |
80
|
|
|
def get_sample(sid): |
81
|
|
|
""" |
82
|
|
|
Detailed information about sample *id* |
83
|
|
|
|
84
|
|
|
Parameters: |
85
|
|
|
id (int): Primary id key of sample |
86
|
|
|
|
87
|
|
|
Example response: :: |
88
|
|
|
|
89
|
|
|
{ "datetime": "Thu, 05 Jun 2014 13:50:27 GMT", |
90
|
|
|
"id": 52, |
91
|
|
|
"sensor": { |
92
|
|
|
"gage": { |
93
|
|
|
"id": 2, |
94
|
|
|
"location": "Wild River near RT 2 in Gilead Maine", |
95
|
|
|
"name": "Wild River at Gilead", |
96
|
|
|
"url": "http://riverflo.ws/api/1.0/gages/2" |
97
|
|
|
}, |
98
|
|
|
"id": 5, |
99
|
|
|
"type": "level", |
100
|
|
|
"url": "http://riverflo.ws/api/1.0/sensors/5" |
101
|
|
|
}, |
102
|
|
|
"url": "http://riverflo.ws/api/1.0/samples/52", |
103
|
|
|
"value": 24.0 |
104
|
|
|
} |
105
|
|
|
""" |
106
|
|
|
sample = Sample.query.get_or_404(sid) |
107
|
|
|
return jsonify(sample.to_json()) |
108
|
|
|
|