from flask import request, jsonify, Blueprint


from .. import piglatin


main = Blueprint('main', __name__)


@main.route('/', methods=['GET', 'POST'])
def index():
    response = """
        Please use the endpoint /translate to access this api.
        Usage: "{}translate?text=Translate+this+text+into+Piglatin."
    """.format(request.url)
    return response


@main.route('/translate', methods=['GET'])
def translate():
    text = request.args.get('text')
    if not text:
        message = 'Invalid parameter text={}'.format(text)
        return jsonify(error=500, text=str(message)), 500

    pig_text = piglatin.translate(text)

    response = {'text': pig_text}
    return jsonify(response)

[((81, 108), 'flask.Blueprint', 'Blueprint', (['"""main"""', '__name__'], {}), "('main', __name__)\n", (90, 108), False, 'from flask import request, jsonify, Blueprint\n'), ((438, 462), 'flask.request.args.get', 'request.args.get', (['"""text"""'], {}), "('text')\n", (454, 462), False, 'from flask import request, jsonify, Blueprint\n'), ((684, 701), 'flask.jsonify', 'jsonify', (['response'], {}), '(response)\n', (691, 701), False, 'from flask import request, jsonify, Blueprint\n')]