2021-03-10 13:55:06 +00:00
|
|
|
from flask import current_app, jsonify, request, url_for
|
2017-11-03 16:35:22 +00:00
|
|
|
|
|
|
|
|
from app import authenticated_service
|
|
|
|
|
from app.dao import inbound_sms_dao
|
2017-11-07 16:39:11 +00:00
|
|
|
from app.schema_validation import validate
|
2017-11-03 16:35:22 +00:00
|
|
|
from app.v2.inbound_sms import v2_inbound_sms_blueprint
|
2017-11-07 16:39:11 +00:00
|
|
|
from app.v2.inbound_sms.inbound_sms_schemas import get_inbound_sms_request
|
2017-11-03 16:35:22 +00:00
|
|
|
|
|
|
|
|
|
2017-11-07 16:39:11 +00:00
|
|
|
@v2_inbound_sms_blueprint.route("", methods=['GET'])
|
|
|
|
|
def get_inbound_sms():
|
|
|
|
|
data = validate(request.args.to_dict(), get_inbound_sms_request)
|
2017-11-06 18:22:18 +00:00
|
|
|
|
2018-03-22 12:41:17 +00:00
|
|
|
paginated_inbound_sms = inbound_sms_dao.dao_get_paginated_inbound_sms_for_service_for_public_api(
|
2017-11-06 18:22:18 +00:00
|
|
|
authenticated_service.id,
|
2017-11-08 10:57:16 +00:00
|
|
|
older_than=data.get('older_than', None),
|
2017-11-06 18:22:18 +00:00
|
|
|
page_size=current_app.config.get('API_PAGE_SIZE')
|
2017-11-03 16:35:22 +00:00
|
|
|
)
|
|
|
|
|
|
2017-11-06 18:22:18 +00:00
|
|
|
return jsonify(
|
2017-11-07 12:19:44 +00:00
|
|
|
received_text_messages=[i.serialize() for i in paginated_inbound_sms],
|
2017-11-08 10:57:16 +00:00
|
|
|
links=_build_links(paginated_inbound_sms)
|
2017-11-06 18:22:18 +00:00
|
|
|
), 200
|
|
|
|
|
|
|
|
|
|
|
2017-11-08 10:57:16 +00:00
|
|
|
def _build_links(inbound_sms_list):
|
2017-11-06 18:22:18 +00:00
|
|
|
_links = {
|
|
|
|
|
'current': url_for(
|
2017-11-07 16:39:11 +00:00
|
|
|
"v2_inbound_sms.get_inbound_sms",
|
2017-11-06 18:22:18 +00:00
|
|
|
_external=True,
|
|
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if inbound_sms_list:
|
|
|
|
|
_links['next'] = url_for(
|
2017-11-07 16:39:11 +00:00
|
|
|
"v2_inbound_sms.get_inbound_sms",
|
2017-11-06 18:22:18 +00:00
|
|
|
older_than=inbound_sms_list[-1].id,
|
|
|
|
|
_external=True,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return _links
|