rest_api.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. from os import environ
  2. from flask import Flask, Response, jsonify, request
  3. from transliterator.tables import list_tables, load_table
  4. from transliterator.trans import transliterate
  5. def create_app():
  6. app = Flask(__name__)
  7. app.config.update({
  8. "ENV": environ.get("TXL_APP_MODE", "production"),
  9. "SECRET_KEY": environ["TXL_FLASK_SECRET"],
  10. "USE_X_SENDFILE": True,
  11. "JSON_AS_ASCII": False,
  12. "JSONIFY_PRETTYPRINT_REGULAR": True,
  13. })
  14. return app
  15. app = create_app()
  16. @app.route("/health", methods=["GET"])
  17. def health_check():
  18. return "I'm alive!\n"
  19. @app.route("/languages", methods=["GET"])
  20. def list_languages():
  21. return jsonify(list_tables())
  22. @app.route("/table/<lang>")
  23. def dump_table(lang):
  24. """
  25. Dump parsed transliteration table for a language.
  26. """
  27. return jsonify(load_table(lang))
  28. @app.route("/trans/<lang>/r2s", methods=["POST"], defaults={"s2r": False})
  29. @app.route("/trans/<lang>", methods=["POST"])
  30. def transliterate_req(lang, s2r=True):
  31. in_txt = request.form["text"]
  32. if not len(in_txt):
  33. return ("No input text provided! ", 400)
  34. return Response(
  35. transliterate(in_txt, lang, s2r),
  36. content_type="text/plain")