diff --git a/ucloud-pay.py b/ucloud-pay.py index ad6ca73..b65026d 100644 --- a/ucloud-pay.py +++ b/ucloud-pay.py @@ -53,22 +53,30 @@ class AddProduct(Resource): def post(): data = request.json logging.debug("Got data: {}".format(str(data))) - # TODO: Add validation + otp_response = check_otp(data["name"], data["realm"], + data["token"]) + if otp_response != 200: + return {"message": "Wrong Credentials"}, 403 - 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("Adding product data: {}".format(str(product_value))) - client.put(product_key, product_value, value_in_json=True) - return {"message": - "Product {} created".format(data['product_name'])}, 200 + try: + 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("Adding product data: {}".format(str(product_value))) + client.put(product_key, product_value, value_in_json=True) + return {"message": + "Product {} created".format(data['product_name'])}, 200 + except KeyError as ke: + logging.error("KeyError occurred. details = {}".format(str(ke))) + return {"message": + "Missing or wrong parameters"}, 400 class UserRegisterPayment(Resource):