Skip to content

Commit e112fa0

Browse files
committed
temporarily disable streaming responses
1 parent 3b49d88 commit e112fa0

2 files changed

Lines changed: 37 additions & 31 deletions

File tree

open_bus_stride_api/common/sql_route.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,27 @@ def list_(sql, sql_params, default_limit, limit, offset, get_count, order_by, sk
3131
sql_order_by = ' order by ' + ', '.join([f'{fieldname} {direction}' for direction, fieldname in order_by_args])
3232
if limit is not None:
3333
sql_limit = f' limit {limit}'
34+
assert sql_limit and 0 < limit <= 1000, "due to abuse, limit must be between 1 and 1000, contact us if you need more"
3435
if offset is not None:
3536
sql_offset = f' offset {offset}'
3637
if sql_order_by or sql_limit or sql_offset:
3738
sql = f'select * from ({sql}) a {sql_order_by}{sql_limit}{sql_offset}'
38-
iterator = (o for o in session.execute(sql, sql_params, execution_options={'stream_results': True}))
39-
first_items = list(itertools.islice(iterator, QUERY_PAGE_SIZE + 1))
40-
if len(first_items) <= QUERY_PAGE_SIZE:
41-
common.debug_print(f'got {len(first_items)} items - returning without streaming')
42-
data = [common.post_process_response_obj(obj, None) for obj in first_items]
43-
session.close()
44-
return data
45-
else:
46-
raise Exception("streaming responses disabled due to abuse, if you need this feature please contact us")
47-
common.debug_print(f'got {len(first_items)} items - returning using streaming')
48-
return fastapi.responses.StreamingResponse(
49-
common.streaming_response_iterator(session, first_items, iterator, None),
50-
media_type="application/json"
51-
)
39+
data = [common.post_process_response_obj(obj, None) for obj in session.execute(sql, sql_params)]
40+
session.close()
41+
return data
42+
# iterator = (o for o in session.execute(sql, sql_params, execution_options={'stream_results': True}))
43+
# first_items = list(itertools.islice(iterator, QUERY_PAGE_SIZE + 1))
44+
# if len(first_items) <= QUERY_PAGE_SIZE:
45+
# common.debug_print(f'got {len(first_items)} items - returning without streaming')
46+
# data = [common.post_process_response_obj(obj, None) for obj in first_items]
47+
# session.close()
48+
# return data
49+
# else:
50+
# common.debug_print(f'got {len(first_items)} items - returning using streaming')
51+
# return fastapi.responses.StreamingResponse(
52+
# common.streaming_response_iterator(session, first_items, iterator, None),
53+
# media_type="application/json"
54+
# )
5255
except:
5356
session.close()
5457
raise

open_bus_stride_api/routers/common.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -81,23 +81,25 @@ def get_list(*args, convert_to_dict=None, **kwargs):
8181
return fastapi.Response(content=str(q_count), media_type="application/json")
8282
else:
8383
debug_print(f'Getting results for query: {q}')
84-
if not hasattr(q, '__q_limit') or not q.__q_limit or q.__q_limit > QUERY_PAGE_SIZE:
85-
debug_print(f'adding yield_per({QUERY_PAGE_SIZE}) to query')
86-
q = q.yield_per(QUERY_PAGE_SIZE)
87-
q_iterator = (obj for obj in q)
88-
first_items = list(itertools.islice(q_iterator, QUERY_PAGE_SIZE + 1))
89-
if len(first_items) <= QUERY_PAGE_SIZE:
90-
debug_print(f'got {len(first_items)} items - returning without streaming')
91-
data = [post_process_response_obj(obj, convert_to_dict) for obj in first_items]
92-
session.close()
93-
return data
94-
else:
95-
raise Exception("streaming responses disabled due to abuse, if you need this feature please contact us")
96-
debug_print(f'got {len(first_items)} items - returning using streaming')
97-
return fastapi.responses.StreamingResponse(
98-
streaming_response_iterator(session, first_items, q_iterator, convert_to_dict),
99-
media_type="application/json"
100-
)
84+
data = [post_process_response_obj(obj, convert_to_dict) for obj in q]
85+
session.close()
86+
return data
87+
# if not hasattr(q, '__q_limit') or not q.__q_limit or q.__q_limit > QUERY_PAGE_SIZE:
88+
# debug_print(f'adding yield_per({QUERY_PAGE_SIZE}) to query')
89+
# q = q.yield_per(QUERY_PAGE_SIZE)
90+
# q_iterator = (obj for obj in q)
91+
# first_items = list(itertools.islice(q_iterator, QUERY_PAGE_SIZE + 1))
92+
# if len(first_items) <= QUERY_PAGE_SIZE:
93+
# debug_print(f'got {len(first_items)} items - returning without streaming')
94+
# data = [post_process_response_obj(obj, convert_to_dict) for obj in first_items]
95+
# session.close()
96+
# return data
97+
# else:
98+
# debug_print(f'got {len(first_items)} items - returning using streaming')
99+
# return fastapi.responses.StreamingResponse(
100+
# streaming_response_iterator(session, first_items, q_iterator, convert_to_dict),
101+
# media_type="application/json"
102+
# )
101103
except:
102104
session.close()
103105
raise
@@ -166,6 +168,7 @@ def get_list_query(session, db_model, limit, offset, filters=None, default_limit
166168
limit = default_limit
167169
if limit:
168170
limit = int(limit)
171+
assert 0 < limit <= 1000, "due to abuse, maximum limit per request is 1000 items, contact us if you need more"
169172
if filters is None:
170173
filters = []
171174
if get_base_session_query_callback is None:

0 commit comments

Comments
 (0)