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)

[((7, 7, 7, 34), 'flask.Blueprint', 'Blueprint', ({(7, 17, 7, 23): '"""main"""', (7, 25, 7, 33): '__name__'}, {}), "('main', __name__)", False, 'from flask import request, jsonify, Blueprint\n'), ((21, 11, 21, 35), 'flask.request.args.get', 'request.args.get', ({(21, 28, 21, 34): '"""text"""'}, {}), "('text')", False, 'from flask import request, jsonify, Blueprint\n'), ((29, 11, 29, 28), 'flask.jsonify', 'jsonify', ({(29, 19, 29, 27): 'response'}, {}), '(response)', False, 'from flask import request, jsonify, Blueprint\n')]