mirror of
https://github.com/GSA/notifications-api.git
synced 2026-07-18 13:40:31 -04:00
Merge pull request #1373 from alphagov/ken-get-inbound_sms-api
Get inbound SMS API
This commit is contained in:
@@ -174,6 +174,7 @@ def register_blueprint(application):
|
||||
|
||||
|
||||
def register_v2_blueprints(application):
|
||||
from app.v2.inbound_sms.get_inbound_sms import v2_inbound_sms_blueprint as get_inbound_sms
|
||||
from app.v2.notifications.post_notifications import v2_notification_blueprint as post_notifications
|
||||
from app.v2.notifications.get_notifications import v2_notification_blueprint as get_notifications
|
||||
from app.v2.template.get_template import v2_template_blueprint as get_template
|
||||
@@ -196,6 +197,9 @@ def register_v2_blueprints(application):
|
||||
post_template.before_request(requires_auth)
|
||||
application.register_blueprint(post_template)
|
||||
|
||||
get_inbound_sms.before_request(requires_auth)
|
||||
application.register_blueprint(get_inbound_sms)
|
||||
|
||||
|
||||
def init_app(app):
|
||||
@app.before_request
|
||||
|
||||
@@ -2,7 +2,8 @@ from datetime import (
|
||||
timedelta,
|
||||
datetime
|
||||
)
|
||||
|
||||
from flask import current_app
|
||||
from sqlalchemy import desc
|
||||
|
||||
from app import db
|
||||
from app.dao.dao_utils import transactional
|
||||
@@ -31,6 +32,28 @@ def dao_get_inbound_sms_for_service(service_id, limit=None, user_number=None):
|
||||
return q.all()
|
||||
|
||||
|
||||
def dao_get_paginated_inbound_sms_for_service(
|
||||
service_id,
|
||||
older_than=None,
|
||||
page_size=None
|
||||
):
|
||||
if page_size is None:
|
||||
page_size = current_app.config['PAGE_SIZE']
|
||||
|
||||
filters = [InboundSms.service_id == service_id]
|
||||
|
||||
if older_than:
|
||||
older_than_created_at = db.session.query(
|
||||
InboundSms.created_at).filter(InboundSms.id == older_than).as_scalar()
|
||||
filters.append(InboundSms.created_at < older_than_created_at)
|
||||
|
||||
query = InboundSms.query.filter(*filters)
|
||||
|
||||
return query.order_by(desc(InboundSms.created_at)).paginate(
|
||||
per_page=page_size
|
||||
).items
|
||||
|
||||
|
||||
def dao_count_inbound_sms_for_service(service_id):
|
||||
return InboundSms.query.filter(
|
||||
InboundSms.service_id == service_id
|
||||
|
||||
@@ -1405,13 +1405,11 @@ class InboundSms(db.Model):
|
||||
def serialize(self):
|
||||
return {
|
||||
'id': str(self.id),
|
||||
'created_at': self.created_at.isoformat(),
|
||||
'created_at': self.created_at.strftime(DATETIME_FORMAT),
|
||||
'service_id': str(self.service_id),
|
||||
'notify_number': self.notify_number,
|
||||
'user_number': self.user_number,
|
||||
'content': self.content,
|
||||
'provider_date': self.provider_date and self.provider_date.isoformat(),
|
||||
'provider_reference': self.provider_reference
|
||||
}
|
||||
|
||||
|
||||
|
||||
6
app/v2/inbound_sms/__init__.py
Normal file
6
app/v2/inbound_sms/__init__.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from flask import Blueprint
|
||||
from app.v2.errors import register_errors
|
||||
|
||||
v2_inbound_sms_blueprint = Blueprint("v2_inbound_sms", __name__, url_prefix='/v2/received-text-messages')
|
||||
|
||||
register_errors(v2_inbound_sms_blueprint)
|
||||
44
app/v2/inbound_sms/get_inbound_sms.py
Normal file
44
app/v2/inbound_sms/get_inbound_sms.py
Normal file
@@ -0,0 +1,44 @@
|
||||
from flask import jsonify, request, url_for, current_app
|
||||
|
||||
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.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("", methods=['GET'])
|
||||
def get_inbound_sms():
|
||||
data = validate(request.args.to_dict(), get_inbound_sms_request)
|
||||
|
||||
paginated_inbound_sms = inbound_sms_dao.dao_get_paginated_inbound_sms_for_service(
|
||||
authenticated_service.id,
|
||||
older_than=data.get('older_than', None),
|
||||
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)
|
||||
), 200
|
||||
|
||||
|
||||
def _build_links(inbound_sms_list):
|
||||
_links = {
|
||||
'current': url_for(
|
||||
"v2_inbound_sms.get_inbound_sms",
|
||||
_external=True,
|
||||
),
|
||||
}
|
||||
|
||||
if inbound_sms_list:
|
||||
_links['next'] = url_for(
|
||||
"v2_inbound_sms.get_inbound_sms",
|
||||
older_than=inbound_sms_list[-1].id,
|
||||
_external=True,
|
||||
)
|
||||
|
||||
return _links
|
||||
70
app/v2/inbound_sms/inbound_sms_schemas.py
Normal file
70
app/v2/inbound_sms/inbound_sms_schemas.py
Normal file
@@ -0,0 +1,70 @@
|
||||
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,
|
||||
},
|
||||
"additionalProperties": False,
|
||||
}
|
||||
|
||||
|
||||
get_inbound_sms_single_response = {
|
||||
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||
"description": "GET inbound sms schema response",
|
||||
"type": "object",
|
||||
"title": "GET response v2/inbound_sms",
|
||||
"properties": {
|
||||
"user_number": {"type": "string"},
|
||||
"created_at": {
|
||||
"format": "date-time",
|
||||
"type": "string",
|
||||
"description": "Date+time created at"
|
||||
},
|
||||
"service_id": uuid,
|
||||
"id": uuid,
|
||||
"notify_number": {"type": "string"},
|
||||
"content": {"type": "string"},
|
||||
},
|
||||
"required": [
|
||||
"id", "user_number", "created_at", "service_id",
|
||||
"notify_number", "content"
|
||||
],
|
||||
"additionalProperties": False,
|
||||
}
|
||||
|
||||
get_inbound_sms_response = {
|
||||
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||
"description": "GET list of inbound sms response schema",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"received_text_messages": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"$ref": "#/definitions/inbound_sms"
|
||||
}
|
||||
},
|
||||
"links": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"current": {
|
||||
"type": "string"
|
||||
},
|
||||
"next": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"additionalProperties": False,
|
||||
"required": ["current"]
|
||||
}
|
||||
},
|
||||
"required": ["received_text_messages", "links"],
|
||||
"definitions": {
|
||||
"inbound_sms": get_inbound_sms_single_response
|
||||
},
|
||||
"additionalProperties": False,
|
||||
}
|
||||
Reference in New Issue
Block a user