Refactor to use query string rather than path params

This commit is contained in:
Ken Tsang
2017-11-07 16:39:11 +00:00
parent ac15e7fd8b
commit dca331e3ea
4 changed files with 68 additions and 71 deletions

View File

@@ -1,35 +1,33 @@
import uuid
from flask import jsonify, request, url_for, current_app
from sqlalchemy.orm.exc import NoResultFound
from werkzeug.exceptions import abort
from notifications_utils.recipients import validate_and_format_phone_number
from notifications_utils.recipients import InvalidPhoneError
from app import authenticated_service
from app.dao import inbound_sms_dao
from app.schema_validation import validate
from app.v2.errors import BadRequestError
from app.v2.inbound_sms import v2_inbound_sms_blueprint
from app.v2.inbound_sms.inbound_sms_schemas import get_inbound_sms_request
@v2_inbound_sms_blueprint.route("/<user_number>", methods=['GET'])
def get_inbound_sms_by_number(user_number):
_data = request.args.to_dict(flat=False)
@v2_inbound_sms_blueprint.route("", methods=['GET'])
def get_inbound_sms():
data = validate(request.args.to_dict(), get_inbound_sms_request)
# flat=False makes everything a list, but we only ever allow one value for "older_than"
if 'older_than' in _data:
_data['older_than'] = _data['older_than'][0]
if data.get('user_number'):
try:
data['user_number'] = validate_and_format_phone_number(data.get('user_number'))
except InvalidPhoneError as e:
raise BadRequestError(message=str(e))
try:
user_number = validate_and_format_phone_number(user_number)
except InvalidPhoneError as e:
raise BadRequestError(message=str(e))
user_number = data.get('user_number', None)
older_than = data.get('older_than', None)
paginated_inbound_sms = inbound_sms_dao.dao_get_paginated_inbound_sms_for_service(
authenticated_service.id,
user_number=user_number,
older_than=_data.get('older_than'),
older_than=older_than,
page_size=current_app.config.get('API_PAGE_SIZE')
)
@@ -37,36 +35,14 @@ def get_inbound_sms_by_number(user_number):
received_text_messages=[i.serialize() for i in paginated_inbound_sms],
links=_build_links(
paginated_inbound_sms,
endpoint='get_inbound_sms_by_number',
user_number=user_number
)
user_number=user_number)
), 200
@v2_inbound_sms_blueprint.route("", methods=['GET'])
def get_all_inbound_sms():
_data = request.args.to_dict(flat=False)
# flat=False makes everything a list, but we only ever allow one value for "older_than"
if 'older_than' in _data:
_data['older_than'] = _data['older_than'][0]
paginated_inbound_sms = inbound_sms_dao.dao_get_paginated_inbound_sms_for_service(
authenticated_service.id,
older_than=_data.get('older_than'),
page_size=current_app.config.get('API_PAGE_SIZE')
)
return jsonify(
received_text_messages=[i.serialize() for i in paginated_inbound_sms],
links=_build_links(paginated_inbound_sms, endpoint='get_all_inbound_sms')
), 200
def _build_links(inbound_sms_list, endpoint, user_number=None):
def _build_links(inbound_sms_list, user_number=None):
_links = {
'current': url_for(
"v2_inbound_sms.{}".format(endpoint),
"v2_inbound_sms.get_inbound_sms",
user_number=user_number,
_external=True,
),
@@ -74,7 +50,7 @@ def _build_links(inbound_sms_list, endpoint, user_number=None):
if inbound_sms_list:
_links['next'] = url_for(
"v2_inbound_sms.{}".format(endpoint),
"v2_inbound_sms.get_inbound_sms",
user_number=user_number,
older_than=inbound_sms_list[-1].id,
_external=True,

View File

@@ -1,6 +1,18 @@
from app.schema_validation.definitions import uuid
get_inbound_sms_request = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "schema for query parameters allowed when getting list of received text messages",
"type": "object",
"properties": {
"older_than": uuid,
"user_number": {"type": "string"}
},
"additionalProperties": False,
}
get_inbound_sms_single_response = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "GET inbound sms schema response",
@@ -29,6 +41,7 @@ get_inbound_sms_single_response = {
"user_number", "created_at", "service_id",
"notify_number", "content"
],
"additionalProperties": False,
}
get_inbound_sms_response = {
@@ -60,5 +73,6 @@ get_inbound_sms_response = {
"required": ["received_text_messages", "links"],
"definitions": {
"inbound_sms": get_inbound_sms_single_response
}
},
"additionalProperties": False,
}