Commit 9904b765 authored by Bruce Flynn's avatar Bruce Flynn

Implement API limitations. Closes #3.

parent d3ea828a
import logging
import os
from datetime import datetime
from datetime import datetime, timedelta
from tempfile import mkstemp
from formencode import Schema, Invalid
......@@ -76,6 +76,28 @@ def validate_symbols(form):
return symbols
SINGLE_STATION_MAX_RANGE = timedelta(days=5 * 365)
MULTI_STATION_MAX_RANGE = timedelta(days=1 * 365)
def check_data_limits_policy(start, end, stations):
if len(stations) == 0:
return
req_range = (end - start)
if len(stations) == 1 and req_range > SINGLE_STATION_MAX_RANGE:
raise HTTPBadRequest((
'Maximum time range allowed is {:d} days for single station query, '
'the current request is for {:d} days.')
.format(SINGLE_STATION_MAX_RANGE.days, req_range.days)
)
elif len(stations) > 1 and req_range > MULTI_STATION_MAX_RANGE:
raise HTTPBadRequest((
'Maximum time range allowed is {:d} days for multi station query'
'the current request is for {:d} days.')
.format(SINGLE_STATION_MAX_RANGE.days, req_range.days)
)
@view_config(route_name='data_form_csv')
def data_csv_view(request):
# query parameter parsing
......@@ -87,6 +109,8 @@ def data_csv_view(request):
end = stamp_to_dt(form['end'])
avg = int(form['avg']) * 60 if form['avg'] else None
check_data_limits_policy(start, end, stations)
db = request.registry.settings['db']
get_slice = getattr(db, 'get_{}_slice'.format(form['type']))
data = get_slice(stations, symbols, start, end, avg=avg)
......@@ -120,6 +144,8 @@ def data_netcdf_view(request):
end = stamp_to_dt(form['end'])
avg = int(form['avg']) * 60 if form['avg'] else None
check_data_limits_policy(start, end, stations)
# fetch data from database
db = request.registry.settings['db']
get_slice = getattr(db, 'get_{}_slice'.format(form['type']))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment