diff --git a/README.md b/README.md index a85dc35..4506e9a 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ List of available configuration options: | `API_KEYS` | A list of api keys that are used to authenticate to the API. If this list is empty authentication will be disabled. | | `ESALES_URL` | URL to the eSales server. If its a cloud cluster it has the format `username:password` | | `ESALES_IMPORT_DIR` | XML-files that should be imported to eSales are temporarily stored in this directory. e.g: `/tmp/esales` +| `BERNARD_QUEUE_URL` | URL to Bernard queue (used for running Photowall background tasks) ## Getting started diff --git a/api/core.py b/api/core.py index 4184663..88e6b94 100644 --- a/api/core.py +++ b/api/core.py @@ -1,5 +1,5 @@ from flask import Flask, jsonify, Response, request -from api.extensions import db, esales +from api.extensions import db, esales, bernard from api.validators import ValidationError @@ -9,6 +9,7 @@ def create_app(config_name="config"): db.init_app(app) esales.init_app(app) + bernard.init_app(app) # register errors handlers @app.errorhandler(404) diff --git a/api/extensions.py b/api/extensions.py index f941f76..28006f3 100644 --- a/api/extensions.py +++ b/api/extensions.py @@ -1,5 +1,8 @@ from flask_sqlalchemy import SQLAlchemy from esales.flask_esales import Esales +from api.lib.bernard import Bernard + db = SQLAlchemy() esales = Esales() +bernard = Bernard() diff --git a/api/lib/bernard.py b/api/lib/bernard.py new file mode 100644 index 0000000..fdaf5ac --- /dev/null +++ b/api/lib/bernard.py @@ -0,0 +1,37 @@ +import boto3 +import time +import json + + +class Bernard(object): + def __init__(self, app=None): + self.sqs = boto3.client("sqs") + if app: + self.init_app(app) + + def init_app(self, app): + if not "BERNARD_QUEUE_URL" in app.config: + raise Exception("BERNARD_QUEUE_URL missing in config") + self.queue_url = app.config.get("BERNARD_QUEUE_URL") + + def produce(self, task, args={}, retries=0): + args["name"] = task + + if retries > 0: + args["retries"] = retries + + message = { + "args": args, + "class": "Bernard:Message:DefaultMessage", + "timestamp": time.time(), + } + + response = self.sqs.send_message( + QueueUrl=self.queue_url, MessageBody=json.dumps(message) + ) + + # if the response contains a MD5OfMessageBody we assume the message was successfully delivered to the queue + if "MD5OfMessageBody" in response: + return True + + return False diff --git a/api/resources/order_rows.py b/api/resources/order_rows.py index e0e6120..d03c9e0 100644 --- a/api/resources/order_rows.py +++ b/api/resources/order_rows.py @@ -1,5 +1,5 @@ from flask import Blueprint, jsonify, abort, request -from api import db +from api import db, bernard from api.helpers import check_api_key from api.models import OrderRow, OrderRowField, OrderRowDetail @@ -22,3 +22,14 @@ def save_pwinty_image_id(row_id): db.session.commit() return jsonify(detail.to_json()) + + +@mod.route("//send", methods=["POST"]) +def send_order_row(row_id): + success = bernard.produce("SendOrderRow", {"row_id": row_id}) + if success: + return jsonify({"status": "ok"}) + else: + return jsonify( + {"status": "err", "message": "Message not delivered to Bernard queue"} + ) diff --git a/config.sample.py b/config.sample.py index dff50c9..ddcd4ed 100644 --- a/config.sample.py +++ b/config.sample.py @@ -3,4 +3,5 @@ SQLALCHEMY_TRACK_MODIFICATIONS = True DEBUG = True API_KEYS = ['secretkey'] ESALES_URL = 'username:password' -ESALES_IMPORT_DIR = '/tmp/esales' \ No newline at end of file +ESALES_IMPORT_DIR = '/tmp/esales' +BERNARD_QUEUE_URL = 'https://sqs.eu-west-1.amazonaws.com//' diff --git a/docs/orders.md b/docs/orders.md index d4716e9..2d6fe7a 100644 --- a/docs/orders.md +++ b/docs/orders.md @@ -11,3 +11,9 @@ curl -X POST https://api2.photowall.com/orders/1/pwinty_id -H "Content-Type: app ``` curl -X POST https://api2.photowall.com/order-rows/1/pwinty_image_id -H "Content-Type: application/json" -d '{"value": 123}' ``` + +## Send a notification that the order-row has been delivered + +``` +curl -X POST https://api2.photowall.com/order-rows/1/send' +``` diff --git a/requirements.txt b/requirements.txt index f8b3a49..78782dc 100644 --- a/requirements.txt +++ b/requirements.txt @@ -14,3 +14,11 @@ factory-boy==2.7.0 jsonschema==2.5.1 mock==2.0.0 Faker==0.9.0 +boto3==1.12.1 +botocore==1.15.1 +docutils==0.15.2 +jmespath==0.9.4 +python-dateutil==2.8.1 +s3transfer==0.3.3 +six==1.14.0 +urllib3==1.25.8 diff --git a/tests/config.py b/tests/config.py index 3f31e9c..d4bc713 100644 --- a/tests/config.py +++ b/tests/config.py @@ -7,3 +7,4 @@ TESTING = True DEBUG = True SQLALCHEMY_ECHO = False API_KEYS = None # api keys are disabled in test +BERNARD_QUEUE_URL = ""