Skip to content
Snippets Groups Projects
Select Git revision
  • 4cd1b6e57c4ef836af7a12213c6f73982a44a5c0
  • master default protected
  • replication_test
  • release-1.10 protected
  • dev protected
  • 556-usage-statistics
  • 553-semantic-recommendation-2
  • 553-semantic-recommendation
  • release-1.9 protected
  • 551-init-broker-service-permissions
  • 549-test-oai-pmh
  • 545-saving-multiple-times-breaks-pid-metadata
  • 499-standalone-compute-service-2
  • 539-load-tests
  • hotfix/helm-chart
  • luca_ba_new_interface
  • 534-bug-when-adding-access-to-user-that-is-not-registered-at-dashboard-service
  • release-1.8 protected
  • 533-integrate-semantic-recommendation
  • feature/openshift
  • 518-spark-doesn-t-map-the-headers-correct
  • v1.10.4 protected
  • v1.10.3 protected
  • v1.10.2 protected
  • v1.10.1 protected
  • v1.10.0-rc13 protected
  • v1.10.0-rc12 protected
  • v1.10.0-rc11 protected
  • v1.10.0-rc10 protected
  • v1.10.0-rc9 protected
  • v1.10.0-rc8 protected
  • v1.10.0-rc7 protected
  • v1.10.0-rc6 protected
  • v1.10.0-rc5 protected
  • v1.10.0-rc4 protected
  • v1.10.0-rc3 protected
  • v1.10.0-rc2 protected
  • v1.10.0rc1 protected
  • v1.10.0rc0 protected
  • v1.10.0 protected
  • v1.9.3 protected
41 results

app.py

Blame
  • app.py 2.18 KiB
    import os
    from flask import Flask, flash, request, redirect, url_for, Response, abort, jsonify
    import logging
    import py_eureka_client.eureka_client as eureka_client
    import json
    from flasgger import Swagger
    from flasgger.utils import swag_from
    from flasgger import LazyString, LazyJSONEncoder
    from list import list_units, get_uri
    from validate import validator
    
    app = Flask(__name__)
    app.config["SWAGGER"] = {"title": "FDA-Units-Service", "uiversion": 3}
    
    swagger_config = {
        "headers": [],
        "specs": [
            {
                "title": "units",
                "endpoint": "api-units",
                "route": "/api-units.json"
            }
        ],
        "static_url_path": "/flasgger_static",
        "swagger_ui": True,
        "specs_route": "/swagger-ui/",
    }
    
    template = dict(
        swaggerUiPrefix=LazyString(lambda: request.environ.get("HTTP_X_SCRIPT_NAME", ""))
    )
    
    app.json_encoder = LazyJSONEncoder
    swagger = Swagger(app, config=swagger_config, template=template)
    
    @app.route('/units/suggest', methods=["POST"], endpoint='suggest')
    @swag_from('suggest.yml')
    def suggest():
        input_json = request.get_json()
        try:
            unit = str(input_json['ustring'])
            offset = int(input_json['offset'])
            res = list_units(unit,offset)
            return jsonify(res),200
        except Exception as e:
            print(e)
            #str(print_exc())
            res = {"success": False, "message": "Unknown error"+str(e)+unit}
            return jsonify(res)
    
    @app.route('/units/validate', methods=["POST"], endpoint='validate')
    @swag_from('validate.yml')
    def suggest():
        input_json = request.get_json()
        try:
            unit = str(input_json['ustring'])
            res = validator(unit)
        except Exception as e:
            print(e)
            res = {"success": False, "message": "Unknown error"+str(e)+unit}
        return jsonify(res)
    
    rest_server_port = 5010
    eureka_client.init(eureka_server=os.getenv('EUREKA_SERVER', 'http://localhost:9090/eureka/'),
                       app_name="fda-units-service",
                       instance_ip="fda-units-service",
                       instance_host="fda-units-service",
                       instance_port=rest_server_port)
    
    if __name__ == '__main__':
        http_server = WSGIServer(('', 5010), app)
        http_server.serve_forever()