This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
2019-01-26 11:49:31 +00:00
|
|
|
from typing import Dict, List
|
|
|
|
|
|
|
|
from flask import Response, jsonify, request
|
2018-11-11 21:18:10 +00:00
|
|
|
from teal.query import NestedQueryFlaskParser
|
|
|
|
from webargs.flaskparser import FlaskParser
|
|
|
|
|
|
|
|
|
|
|
|
class SearchQueryParser(NestedQueryFlaskParser):
|
|
|
|
|
|
|
|
def parse_querystring(self, req, name, field):
|
|
|
|
if name == 'search':
|
|
|
|
v = FlaskParser.parse_querystring(self, req, name, field)
|
|
|
|
else:
|
|
|
|
v = super().parse_querystring(req, name, field)
|
|
|
|
return v
|
2019-01-26 11:49:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
def things_response(items: List[Dict],
|
|
|
|
page: int = None,
|
|
|
|
per_page: int = None,
|
|
|
|
total: int = None,
|
|
|
|
previous: int = None,
|
|
|
|
next: int = None,
|
|
|
|
url: str = None,
|
|
|
|
code: int = 200) -> Response:
|
|
|
|
"""Generates a Devicehub API list conformant response for multiple
|
|
|
|
things.
|
|
|
|
"""
|
|
|
|
response = jsonify({
|
|
|
|
'items': items,
|
|
|
|
# todo pagination should be in Header like github
|
|
|
|
# https://developer.github.com/v3/guides/traversing-with-pagination/
|
|
|
|
'pagination': {
|
|
|
|
'page': page,
|
|
|
|
'perPage': per_page,
|
|
|
|
'total': total,
|
|
|
|
'previous': previous,
|
|
|
|
'next': next
|
|
|
|
},
|
|
|
|
'url': url or request.path
|
|
|
|
})
|
|
|
|
response.status_code = code
|
|
|
|
return response
|