~kf5jwc/sms-printer

3f7ab6705fecb956050013cfa685da2d6fef8634 — Kyle Jones 1 year, 4 months ago 5d0e381
Strip out the web server
5 files changed, 0 insertions(+), 61 deletions(-)

M Pipfile
D Procfile
M pyproject.toml
M sms_printer/__init__.py
D sms_printer/application.py
M Pipfile => Pipfile +0 -2
@@ 4,8 4,6 @@ url = "https://pypi.python.org/simple"
name = "pypi"

[packages]
gunicorn = "*"
flask = "*"
pyxdg = "*"

[dev-packages]

D Procfile => Procfile +0 -1
@@ 1,1 0,0 @@
web: gunicorn sms_printer

M pyproject.toml => pyproject.toml +0 -2
@@ 8,8 8,6 @@ author = "Kyle Jones"
author-email = "kyle@kf5jwc.us"
requires-python = ">=3.7"
requires = [
	"gunicorn",
	"flask",
	"pyxdg",
	"setuptools", # for pkg_resources
]

M sms_printer/__init__.py => sms_printer/__init__.py +0 -2
@@ 4,5 4,3 @@ This prints SMS messages (on a real printer) using CUPS.
This is a web server which accepts JSON-formatted SMS messages
at /sms-printer from brokers (such as bandwidth.com) for printing.
"""

from .application import APP as application

D sms_printer/application.py => sms_printer/application.py +0 -54
@@ 1,54 0,0 @@
import logging

from flask import Flask, Response, json, request

from sms_broker_parsers import get_mimetype_parser
from sms_broker_parsers.errors import NoMatchingSchema, NoParserForMimeType
from .utils import allowed_sender, log_request, print_message

APP = Flask(__name__)
logging.basicConfig(level=logging.DEBUG)


@APP.route("/", methods=["GET"])
def main() -> Response:
    return Response(status=204)


@APP.route("/sms-printer", methods=["POST"])
def sms_printer() -> Response:
    # If this throws, flask takes care of it
    sms_input = request.get_json(force=True)

    try:
        parser = get_mimetype_parser(request.mimetype)

    except NoParserForMimeType:
        logging.warning("No parsers available for the given mimetype!")
        logging.debug("mimetype: {}".format(request.mimetype))
        logging.debug("header: {}".format(request.headers["Content-Type"]))
        return Response(status=415)

    try:
        sms_list = parser(sms_input)
        log_request(sms_input)

    except NoMatchingSchema:
        logging.warning("Unrecognized message format!")
        logging.debug(json.dumps(sms_input))
        return Response(status=415)

    for sms in sms_list:

        logging.info("Recieved sms from %s", sms.sender)
        if not allowed_sender(sms.sender):
            logging.warning("Unauthorized sender %s", sms.sender)
            return Response(status=401)

        logging.info("Printing sms from %s", sms.sender)
        logging.debug("Text: %s", sms.text)
        if not print_message(sms.text):
            logging.error("Error printing sms")
            return Response(status=500)

        return Response(status=200)