Skip to content
Snippets Groups Projects
Select Git revision
  • ac15e2ca41642bb60523041b74f3ab99258895f7
  • 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.69 KiB
    import json
    import os
    import logging
    
    from flasgger import LazyJSONEncoder, Swagger
    from flask import Flask, request, Response
    from flasgger.utils import swag_from
    from clients.s3_client import S3Client
    from prometheus_flask_exporter import PrometheusMetrics
    
    logging.basicConfig(level=logging.DEBUG)
    
    from logging.config import dictConfig
    
    # logging configuration
    dictConfig({
        'version': 1,
        'formatters': {
            'default': {
                'format': '[%(asctime)s] %(levelname)s in %(module)s: %(message)s',
            },
            'simple': {
                'format': '[%(asctime)s] %(levelname)s: %(message)s',
            },
        },
        'handlers': {'wsgi': {
            'class': 'logging.StreamHandler',
            'stream': 'ext://flask.logging.wsgi_errors_stream',
            'formatter': 'simple'  # default
        }},
        'root': {
            'level': 'DEBUG',
            'handlers': ['wsgi']
        }
    })
    
    # create app object
    app = Flask(__name__)
    
    metrics = PrometheusMetrics(app)
    metrics.info("app_info", "Application info", version="0.0.1")
    app.config["SWAGGER"] = {"openapi": "3.0.1", "title": "Swagger UI", "uiversion": 3}
    
    swagger_config = {
        "headers": [],
        "specs": [
            {
                "endpoint": "api-sidecar",
                "route": "/api-sidecar.json",
                "rule_filter": lambda rule: rule.endpoint.startswith('actuator') or rule.endpoint.startswith('sidecar'),
                "model_filter": lambda tag: True,  # all in
            }
        ],
        "static_url_path": "/flasgger_static",
        "swagger_ui": True,
        "specs_route": "/swagger-ui/",
    }
    
    template = {
        "openapi": "3.0.0",
        "info": {
            "title": "Database Repository Data Database sidecar API",
            "description": "Sidecar that downloads the import .csv file",
            "version": "$TAG",
            "contact": {
                "name": "Prof. Andreas Rauber",
                "email": "andreas.rauber@tuwien.ac.at"
            },
            "license": {
                "name": "Apache 2.0",
                "url": "https://www.apache.org/licenses/LICENSE-2.0"
            },
        },
        "externalDocs": {
            "description": "Sourcecode Documentation",
            "url": "https://gitlab.phaidra.org/fair-data-austria-db-repository/fda-services"
        },
        "servers": [
            {
                "url": "http://localhost:5000",
                "description": "Generated server url"
            },
            {
                "url": "https://test.dbrepo.tuwien.ac.at",
                "description": "Sandbox"
            }
        ]
    }
    
    swagger = Swagger(app, config=swagger_config, template=template)
    # https://flask-jwt-extended.readthedocs.io/en/stable/options/
    app.config["JWT_ALGORITHM"] = "HS256"
    app.config["JWT_DECODE_ISSUER"] = os.getenv("JWT_ISSUER")
    app.config["JWT_PUBLIC_KEY"] = os.getenv("JWT_PUBKEY")
    
    app.json_encoder = LazyJSONEncoder
    
    
    @app.route("/health", methods=["GET"], endpoint="actuator_health")
    @swag_from("ds-yml/health.yml")
    def health():
        return Response(json.dumps({"status": "UP"}), mimetype="application/json"), 200
    
    
    @app.route("/sidecar/import/<string:filename>", methods=["POST"], endpoint="sidecar_import")
    @swag_from("ds-yml/import.yml")
    def import_csv(filename):
        logging.debug('endpoint import csv, filename=%s, body=%s', filename, request)
        s3_client = S3Client()
        response = s3_client.download_file(filename)
        if response is False:
            return Response(), 400
        return Response(json.dumps(response)), 202
    
    
    @app.route("/sidecar/export/<string:filename>", methods=["POST"], endpoint="sidecar_export")
    @swag_from("ds-yml/export.yml")
    def import_csv(filename):
        logging.debug('endpoint export csv, filename=%s, body=%s', filename, request)
        s3_client = S3Client()
        response = s3_client.upload_file(filename)
        if response is False:
            return Response(), 400
        return Response(), 202