completely refactored with proper models and a swagger ui
This commit is contained in:
parent
803f0483dc
commit
d9237b2891
101
app.py
101
app.py
@ -1,65 +1,84 @@
|
|||||||
from flask import Flask, request, jsonify
|
from flask import Flask
|
||||||
|
from flask_restx import Api, Resource, fields
|
||||||
import random
|
import random
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
app.config.SWAGGER_UI_JSONEDITOR = True
|
||||||
|
app.config['SWAGGER_UI_JSONEDITOR'] = True
|
||||||
|
api = Api(app, version='1.0', title='Gamma World Dice', description='Rolled Dice As A Service')
|
||||||
|
|
||||||
|
dice = api.namespace('dice', description='Dice operations')
|
||||||
|
ability = api.namespace('ability', description='Ability operations')
|
||||||
|
hp = api.namespace('hp', description='HP operations')
|
||||||
|
|
||||||
|
dice_model = dice.model('Dice', {
|
||||||
|
'quantity': fields.Integer(required=True, description='The number of dice to roll'),
|
||||||
|
'geometry': fields.Integer(required=True, description='The number of sides on each die'),
|
||||||
|
'discard_lowest': fields.Boolean(required=False, default=False, description='Drop the lowest score')
|
||||||
|
})
|
||||||
|
|
||||||
|
ability_model = ability.model('Ability', {
|
||||||
|
'chartype': fields.String(required=False, default="human", description='Character type')
|
||||||
|
})
|
||||||
|
|
||||||
|
hp_model = hp.model('HP', {
|
||||||
|
'chartype': fields.String(required=False, default="human", description='Character type'),
|
||||||
|
'conscore': fields.Integer(required=True, description='Conscore')
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
@app.route('/')
|
@api.route('/roll/dice', methods=['POST'])
|
||||||
def hello_world(): # put application's code here
|
class RollDice(Resource):
|
||||||
return 'Hello World!'
|
@dice.expect(dice_model)
|
||||||
|
def post(self):
|
||||||
|
data = api.payload
|
||||||
|
quantity = data.get('quantity')
|
||||||
|
geometry = data.get('geometry')
|
||||||
|
discard_lowest = data.get('discard_lowest')
|
||||||
|
if quantity is None or geometry is None:
|
||||||
|
return {"message": "Required dice data not provided"}, 400
|
||||||
|
return roll_dices(quantity, geometry, discard_lowest), 200
|
||||||
|
|
||||||
|
|
||||||
@app.route('/roll/chance', methods=['GET'])
|
@api.route('/roll/ability', methods=['POST'])
|
||||||
def roll_chance():
|
class RollAbility(Resource):
|
||||||
result = roll_dices(1, 100, False)
|
@ability.expect(ability_model)
|
||||||
return jsonify(result["sum"])
|
def post(self):
|
||||||
|
data = api.payload
|
||||||
|
chartype = data.get('chartype')
|
||||||
@app.route('/roll/tohit', methods=['GET'])
|
|
||||||
def roll_tohit():
|
|
||||||
result = roll_dices(1, 20, False)
|
|
||||||
return jsonify(result["sum"])
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/roll/ability', methods=['POST', 'GET'])
|
|
||||||
def roll_ability():
|
|
||||||
if request.json.get('chartype') is None:
|
|
||||||
chartype = 'human'
|
|
||||||
else:
|
|
||||||
chartype = request.json.get('chartype')
|
|
||||||
|
|
||||||
if chartype == 'human':
|
if chartype == 'human':
|
||||||
geometry = 8
|
geometry = 8
|
||||||
else:
|
else:
|
||||||
geometry = 6
|
geometry = 6
|
||||||
results = roll_dices(4, geometry, True)
|
return roll_dices(4, geometry, True), 200
|
||||||
|
|
||||||
return jsonify(results)
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/roll/hp', methods=['POST', 'GET'])
|
@api.route('/roll/hp', methods=['POST'])
|
||||||
def roll_sum():
|
class RollHP(Resource):
|
||||||
chartype = request.json.get('quantity')
|
@hp.expect(hp_model)
|
||||||
if chartype is None:
|
def post(self):
|
||||||
chartype = 'human'
|
data = api.payload
|
||||||
conscore = request.json.get('conscore')
|
chartype = data.get('chartype')
|
||||||
|
conscore = data.get('conscore')
|
||||||
if conscore is None:
|
if conscore is None:
|
||||||
return jsonify({"ERROR": "NO CONSTITUTION SCORE SUPPLIED"})
|
return {"message": "A constitution score is required"}, 400
|
||||||
if chartype == 'human':
|
if chartype == 'human':
|
||||||
geometry = 8
|
geometry = 8
|
||||||
else:
|
else:
|
||||||
geometry = 6
|
geometry = 6
|
||||||
|
return roll_dices(conscore, geometry, False), 200
|
||||||
return jsonify(roll_dices(conscore, geometry, False))
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/roll/dice', methods=['POST'])
|
@api.route('/roll/chance', methods=['GET'])
|
||||||
def rolldie():
|
class RollChance(Resource):
|
||||||
quantity = request.json.get('quantity')
|
def get(self):
|
||||||
geometry = request.json.get('geometry')
|
return roll_dices(1, 100, False), 200
|
||||||
discard_lowest = request.json.get('discard_lowest', False)
|
|
||||||
|
|
||||||
return jsonify(roll_dices(quantity, geometry, discard_lowest))
|
|
||||||
|
@api.route('/roll/tohit', methods=['GET'])
|
||||||
|
class RollToHit(Resource):
|
||||||
|
def get(self):
|
||||||
|
return roll_dices(1, 20, False), 200
|
||||||
|
|
||||||
|
|
||||||
def roll_dices(dice_count, dice_sides, discard_lowest):
|
def roll_dices(dice_count, dice_sides, discard_lowest):
|
||||||
|
Loading…
Reference in New Issue
Block a user