37 lines
845 B
Python
37 lines
845 B
Python
import os
|
|
from flask import Flask, jsonify, request
|
|
|
|
app = Flask(__name__)
|
|
VERSION = "v2"
|
|
UNIT_PRICE = 12
|
|
COMMIT = os.environ.get("APP_COMMIT", "development")
|
|
|
|
|
|
QUANTITY_ERROR = "quantity must be a whole number between 1 and 1000"
|
|
|
|
|
|
def total_price(quantity):
|
|
if quantity < 1 or quantity > 1000:
|
|
raise ValueError(QUANTITY_ERROR)
|
|
return UNIT_PRICE * quantity
|
|
|
|
|
|
@app.get("/")
|
|
def index():
|
|
return jsonify(service="Startup Pricing", version=VERSION, commit=COMMIT)
|
|
|
|
|
|
@app.get("/health")
|
|
def health():
|
|
return jsonify(status="ok", version=VERSION)
|
|
|
|
|
|
@app.get("/price")
|
|
def price():
|
|
try:
|
|
quantity = int(request.args.get("quantity", "1"))
|
|
total = total_price(quantity)
|
|
except ValueError:
|
|
return jsonify(error=QUANTITY_ERROR), 400
|
|
return jsonify(quantity=quantity, total=total, currency="USD")
|