Skip to content
Snippets Groups Projects
Select Git revision
  • 05a07f7d3d9000f4c40257e3ec9bcce911701adc
  • 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 3.66 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, stringmapper
    from save import insert_mdb_concepts, insert_mdb_columns_concepts
    
    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('/api/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(stringmapper(unit),offset)
            return jsonify(res), 200
        except Exception as e:
            print(e)
            res = {"success": False, "message": str(e)}
            return jsonify(res), 500
    
    @app.route('/api/units/validate/<unit>', methods=["GET"], endpoint='validate')
    @swag_from('validate.yml')
    def valitate(unit):
        try:
            res = validator(unit)
            return str(res), 200
        except Exception as e:
            print(e)
            res = {"success": False, "message": str(e)}
            return jsonify(res)
    
    @app.route('/api/units/uri/<uname>', methods=["GET"], endpoint='uri')
    @swag_from('geturi.yml')
    def geturi(uname):
        try:
            res = get_uri(uname)
            return jsonify(res), 200
        except Exception as e:
            print(e)
            res = {"success": False, "message": str(e)}
            return jsonify(res), 500
    
    @app.route('/api/units/saveconcept', methods=["POST"], endpoint='saveconcept')
    @swag_from('saveconcept.yml')
    def saveconcept():
        input_json = request.get_json()
        try:
            uri = str(input_json['uri'])
            c_name = str(input_json['name'])
            if insert_mdb_concepts(uri, c_name) > 0:
                return jsonify({'uri': uri}), 201
            else:
                return jsonify({'status': 'error'}), 400
        except Exception as e:
            print(e)
            res = {"success": False, "message": str(e)}
            return jsonify(res), 500
    
    @app.route('/api/units/savecolumnsconcept', methods=["POST"], endpoint='savecolumnsconcept')
    @swag_from('savecolumnsconcept.yml')
    def saveconcept():
        input_json = request.get_json()
        try:
            uri = str(input_json['uri'])
            cid = int(input_json['cid'])
            tid = int(input_json['tid'])
            cdbid = int(input_json['cdbid'])
            if insert_mdb_columns_concepts(cdbid, tid, cid, uri)>0:
                return jsonify({'uri': uri}), 201
            else:
                return jsonify({'status': 'error'}), 400
        except Exception as e:
            print(e)
            res = {"success": False, "message": str(e)}
            return jsonify(res), 500
    
    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()