ucloud-pay/ucloud-pay.py

45 lines
1.4 KiB
Python
Raw Normal View History

2019-09-13 05:02:23 +00:00
import json
from flask import Flask, request
from flask_restful import Resource, Api
from config import etcd_client as client, logging
app = Flask(__name__)
api = Api(app)
class ListProducts(Resource):
@staticmethod
def get():
products = client.get_prefix("/v1/products/", value_in_json=False)
prod_dict = {}
for p in products:
prod_dict[p.key] = json.loads(p.value)
logging.debug(f"Products = {prod_dict}")
return prod_dict, 200
class AddProduct(Resource):
@staticmethod
def post():
data = request.json
logging.debug(f"Got data: {str(data)}")
# TODO: Add validation
product_key = "/v1/products/"
product_value = {
"name": data["product_name"],
"description": data["product_description"],
"type": data["product_type"],
"price": data["product_price"],
"recurring_duration": data["product_recurring_duration"],
"recurring_duration_units": data["product_recurring_duration_units"]
}
logging.debug(f"Adding product data: {str(product_value)}")
client.put(product_key, product_value, value_in_json=True)
return {"message": f"Product {data['product_name']} created"}, 200
api.add_resource(ListProducts, "/product/list")
api.add_resource(AddProduct, "/product/add")
if __name__ == '__main__':
app.run(host="::", debug=True)