get_rivers()   B
last analyzed

Complexity

Conditions 4

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 38
rs 8.5806
cc 4
1
"""
2
3
Endpoints:
4
----------
5
6
- **/api/1.0/regions/<id>** - **GET** Detailed information about region *id*
7
- **/api/1.0/rivers/ - **GET** List all rivers
8
9
"""
10
from flask import jsonify, request, url_for, current_app
11
12
from ..models import River
13
from .blueprint import api
14
15
16
@api.route('/rivers/')
17
def get_rivers():
18
    """
19
    List all rivers
20
21
    Example response: ::
22
23
        { "count": 7,
24
          "next": "http://riverflo.ws/api/1.0/sensors/?page=2",
25
          "prev": null,
26
          "rivers": [
27
            { "id": 1,
28
              "name": "Kennebec",
29
              "url": "http://riverflo.ws/api/1.0/rivers/1"
30
            },
31
            { "id": 2,
32
              "name": "Androscoggin",
33
              "url": "http://riverflo.ws/api/1.0/rivers/2"
34
            }
35
          ]
36
        }
37
    """
38
    page = request.args.get('page', 1, type=int)
39
    pagination = River.query.paginate(page,
40
                                      per_page=current_app.config['API_GAGES_PER_PAGE'],  # noqa
41
                                      error_out=False)
42
    rivers = pagination.items
43
    prev = None
44
    if pagination.has_prev:
45
        prev = url_for('.get_rivers', page=page-1, _external=True)
46
    next_p = None
47
    if pagination.has_next:
48
        next_p = url_for('.get_rivers', page=page+1, _external=True)
49
    return jsonify({
50
        'rivers': [river.to_json() for river in rivers],
51
        'prev': prev,
52
        'next': next_p,
53
        'count': pagination.total
54
    })
55
56
57
@api.route('/rivers/<int:rid>')
58
def get_river(rid):
59
    """
60
    Detailed information about river *id*
61
62
    Parameters:
63
        id (int): Primary id key for river
64
65
    Example response: ::
66
67
        { "downstream": {
68
            "id": 1,
69
            "name": "Kennebec",
70
            "url": "http://riverflo.ws/api/1.0/rivers/1"
71
          },
72
          "gages": [
73
            { "id": 5,
74
              "location": "Androscoggin River downstream of I-95 in Auburn ME",
75
              "name": "Androscoggin River at Auburn",
76
              "url": "http://riverflo.ws/api/1.0/gages/5"
77
            },
78
            { "id": 4,
79
              "location": "Androscoggin River below lower power plant",
80
              "name": "Androscoggin River at Rumford",
81
              "url": "http://riverflo.ws/api/1.0/gages/4"
82
            }
83
          ],
84
          "id": 2,
85
          "name": "Androscoggin",
86
          "sections": [],
87
          "tributaries": [
88
            { "id": 3,
89
              "name": "Sunday River",
90
              "url": "http://riverflo.ws/api/1.0/rivers/3"
91
            },
92
            { "id": 5,
93
              "name": "Wild River (Androscoggin)",
94
              "url": "http://riverflo.ws/api/1.0/rivers/5"
95
            },
96
            { "id": 6,
97
              "name": "Peapody River",
98
              "url": "http://riverflo.ws/api/1.0/rivers/6"
99
            },
100
            { "id": 7,
101
              "name": "Bear River (Androscoggin)",
102
              "url": "http://riverflo.ws/api/1.0/rivers/7"
103
            }
104
          ],
105
          "url": "http://riverflo.ws/api/1.0/rivers/2"
106
        }
107
    """
108
    river = River.query.get_or_404(rid)
109
    return jsonify(river.to_long_json())
110