Authentication and Odata compliance
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
def parse_odata_query(request_args):
|
||||
"""
|
||||
Translates standard OData query parameters ($top, $skip, $orderby, $filter)
|
||||
into MongoDB query parameters.
|
||||
"""
|
||||
limit = int(request_args.get('$top', 50))
|
||||
skip = int(request_args.get('$skip', 0))
|
||||
|
||||
# Parse $orderby (e.g., $orderby=received_at desc)
|
||||
sort = []
|
||||
orderby_str = request_args.get('$orderby')
|
||||
if orderby_str:
|
||||
parts = orderby_str.split()
|
||||
field = parts[0]
|
||||
direction = -1 if len(parts) > 1 and parts[1].lower() == 'desc' else 1
|
||||
sort.append((field, direction))
|
||||
|
||||
# Parse basic $filter (e.g., $filter=device_id eq '1')
|
||||
mongo_filter = {}
|
||||
filter_str = request_args.get('$filter')
|
||||
if filter_str and ' eq ' in filter_str:
|
||||
field, val = filter_str.split(' eq ')
|
||||
field = field.strip()
|
||||
val = val.strip().strip("'").strip('"')
|
||||
|
||||
# Cast data types
|
||||
if val.isdigit():
|
||||
val = int(val)
|
||||
elif val.lower() == 'true':
|
||||
val = True
|
||||
elif val.lower() == 'false':
|
||||
val = False
|
||||
|
||||
mongo_filter[field] = val
|
||||
|
||||
return {
|
||||
"filter": mongo_filter,
|
||||
"limit": limit,
|
||||
"skip": skip,
|
||||
"sort": sort or [("_id", -1)]
|
||||
}
|
||||
Reference in New Issue
Block a user