P5-4697 add bernard integration and send order row endpoint

This commit is contained in:
Martin Carlsson
2020-02-24 14:14:13 +01:00
committed by GitHub
parent fcf79ca75a
commit 20fa6b0322
9 changed files with 72 additions and 3 deletions
+1
View File
@@ -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. | | `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_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` | `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 ## Getting started
+2 -1
View File
@@ -1,5 +1,5 @@
from flask import Flask, jsonify, Response, request 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 from api.validators import ValidationError
@@ -9,6 +9,7 @@ def create_app(config_name="config"):
db.init_app(app) db.init_app(app)
esales.init_app(app) esales.init_app(app)
bernard.init_app(app)
# register errors handlers # register errors handlers
@app.errorhandler(404) @app.errorhandler(404)
+3
View File
@@ -1,5 +1,8 @@
from flask_sqlalchemy import SQLAlchemy from flask_sqlalchemy import SQLAlchemy
from esales.flask_esales import Esales from esales.flask_esales import Esales
from api.lib.bernard import Bernard
db = SQLAlchemy() db = SQLAlchemy()
esales = Esales() esales = Esales()
bernard = Bernard()
+37
View File
@@ -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
+12 -1
View File
@@ -1,5 +1,5 @@
from flask import Blueprint, jsonify, abort, request 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.helpers import check_api_key
from api.models import OrderRow, OrderRowField, OrderRowDetail from api.models import OrderRow, OrderRowField, OrderRowDetail
@@ -22,3 +22,14 @@ def save_pwinty_image_id(row_id):
db.session.commit() db.session.commit()
return jsonify(detail.to_json()) return jsonify(detail.to_json())
@mod.route("/<int:row_id>/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"}
)
+1
View File
@@ -4,3 +4,4 @@ DEBUG = True
API_KEYS = ['secretkey'] API_KEYS = ['secretkey']
ESALES_URL = 'username:password' ESALES_URL = 'username:password'
ESALES_IMPORT_DIR = '/tmp/esales' ESALES_IMPORT_DIR = '/tmp/esales'
BERNARD_QUEUE_URL = 'https://sqs.eu-west-1.amazonaws.com/<queue_id>/<queue_name>'
+6
View File
@@ -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}' 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'
```
+8
View File
@@ -14,3 +14,11 @@ factory-boy==2.7.0
jsonschema==2.5.1 jsonschema==2.5.1
mock==2.0.0 mock==2.0.0
Faker==0.9.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
+1
View File
@@ -7,3 +7,4 @@ TESTING = True
DEBUG = True DEBUG = True
SQLALCHEMY_ECHO = False SQLALCHEMY_ECHO = False
API_KEYS = None # api keys are disabled in test API_KEYS = None # api keys are disabled in test
BERNARD_QUEUE_URL = ""