Files
robot-shop/payment/payment.py

81 lines
2.3 KiB
Python
Raw Normal View History

2018-01-25 17:42:38 +00:00
import os
import sys
2018-01-26 16:01:39 +00:00
import time
2018-01-25 17:42:38 +00:00
import logging
2018-01-26 16:01:39 +00:00
import uuid
2018-02-12 13:06:35 +00:00
import json
2018-01-25 17:42:38 +00:00
import requests
2018-01-31 17:27:17 +00:00
import opentracing as ot
import opentracing.ext.tags as tags
2018-01-25 17:42:38 +00:00
from flask import Flask
from flask import request
2018-01-26 16:01:39 +00:00
from flask import jsonify
from rabbitmq import Publisher
2018-01-25 17:42:38 +00:00
app = Flask(__name__)
@app.route('/health', methods=['GET'])
def health():
return 'OK'
@app.route('/pay/<id>', methods=['POST'])
def pay(id):
app.logger.info('payment for {}'.format(id))
cart = request.get_json()
app.logger.info(cart)
# dummy call to Paypal, hope they dont object
req = requests.get('https://paypal.com/')
app.logger.info('paypal returned {}'.format(req.status_code))
2018-01-26 16:01:39 +00:00
# Generate order id
orderid = str(uuid.uuid4())
2018-01-30 09:16:51 +00:00
queueOrder({ 'orderid': orderid, 'user': id, 'cart': cart })
2018-01-26 16:01:39 +00:00
2018-02-12 13:06:35 +00:00
# add to history
req = requests.post('http://user:8080/order/' + id,
data=json.dumps({'orderid': orderid, 'cart': cart}),
headers={'Content-Type': 'application/json'})
app.logger.info('order history returned {}'.format(req.status_code))
2018-02-14 09:29:47 +00:00
# delete cart
req = requests.delete('http://cart:8080/cart/' + id);
app.logger.info('cart delete returned {}'.format(req.status_code))
2018-01-25 17:42:38 +00:00
2018-01-30 09:16:51 +00:00
return jsonify({ 'orderid': orderid })
2018-01-26 16:01:39 +00:00
def queueOrder(order):
app.logger.info('queue order')
2018-01-31 17:27:17 +00:00
# RabbitMQ is not currently traced automatically
# opentracing tracer is automatically set to Instana tracer
# start a span
context = ot.tracer.current_context()
span = ot.tracer.start_span(operation_name='queue-order',
child_of=ot.tracer.current_context(),
tags={
tags.SPAN_KIND: 'producer',
tags.COMPONENT: 'payment',
'message_bus.destination': 'orders'
}
)
headers = {}
ot.tracer.inject(span.context, ot.Format.HTTP_HEADERS, headers)
app.logger.info('msg headers {}'.format(headers))
publisher.publish(order, headers)
span.finish()
2018-01-26 16:01:39 +00:00
# RabbitMQ
publisher = Publisher(app.logger)
2018-01-25 17:42:38 +00:00
if __name__ == "__main__":
sh = logging.StreamHandler(sys.stdout)
sh.setLevel(logging.INFO)
app.logger.addHandler(sh)
app.logger.setLevel(logging.INFO)
2018-01-31 10:11:58 +00:00
port = int(os.getenv("SHOP_PAYMENT_PORT", "8080"))
2018-01-25 17:42:38 +00:00
app.run(host='0.0.0.0', port=port)