Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #27: Add Swagger Documentation to finesse-backend #30

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions app/app_creator.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
from flask import Flask
from flask_restx import Api
from flask_cors import CORS

from .config import Config


def create_app(config: Config):
app = Flask(__name__)
CORS(app)
app.config.from_object(config)

from .blueprints.monitor import monitor_blueprint
from .blueprints.search import search_blueprint
api = Api(path = "")
api.init_app(app)

app.register_blueprint(
monitor_blueprint, url_prefix="/health", strict_slashes=False
)
app.register_blueprint(search_blueprint, url_prefix="/search", strict_slashes=False)
from .namespaces.search import search_namespace
from .namespaces.monitor import monitor_namespace

api.add_namespace(search_namespace)
api.add_namespace(monitor_namespace)
return app
8 changes: 0 additions & 8 deletions app/blueprints/monitor.py

This file was deleted.

65 changes: 0 additions & 65 deletions app/blueprints/search.py

This file was deleted.

File renamed without changes.
9 changes: 9 additions & 0 deletions app/namespaces/monitor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from flask_restx import Namespace, Resource

monitor_namespace = Namespace("monitor", description="Monitoring operations")


@monitor_namespace.route("")
class Health(Resource):
def get(self):
return "ok", 200
75 changes: 75 additions & 0 deletions app/namespaces/search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from functools import wraps

from flask import current_app, jsonify, request
from flask_restx import Namespace, Resource, fields
from index_search import AzureIndexSearchQueryError, search

from app.ailab_db import DBError, ailab_db_search
from app.finesse_data import FinesseDataFetchException, fetch_data
from app.utils import sanitize

search_namespace = Namespace("search",description="Search operations")

search_model = search_namespace.model('SearchInput', {
'query': fields.String,
})

def require_non_empty_query(f):
@wraps(f)
def decorated_function(*args, **kwargs):
query = request.json.get("query")
if not query:
return jsonify({"message": current_app.config["ERROR_EMPTY_QUERY"]}), 400
return f(*args, **kwargs)

return decorated_function


@search_namespace.route("/azure")
class Azure(Resource):
@require_non_empty_query
@search_namespace.expect(search_model)
def post(self):
query = request.json["query"]
query = sanitize(query, current_app.config["SANITIZE_PATTERN"])
try:
results = search(query, current_app.config["AZURE_CONFIG"])
return jsonify(results)
except AzureIndexSearchQueryError:
return jsonify({"error": current_app.config["ERROR_AZURE_FAILED"]}), 500
except Exception:
return jsonify({"error": current_app.config["ERROR_UNEXPECTED"]}), 500


@search_namespace.route("/static")
class Static(Resource):
@require_non_empty_query
@search_namespace.expect(search_model)
def post(self):
finesse_data_url = current_app.config["FINESSE_DATA_URL"]
query = request.json["query"]
query = sanitize(query, current_app.config["SANITIZE_PATTERN"])
match_threshold = current_app.config["FUZZY_MATCH_THRESHOLD"]
try:
data = fetch_data(finesse_data_url, query, match_threshold)
return jsonify(data)
except FinesseDataFetchException:
return jsonify({"error": current_app.config["ERROR_FINESSE_DATA_FAILED"]}), 500
except Exception:
return jsonify({"error": current_app.config["ERROR_UNEXPECTED"]}), 500


@search_namespace.route("/ailab")
class AilabDB(Resource):
@require_non_empty_query
@search_namespace.expect(search_model)
def post(self):
query = request.json["query"]
query = sanitize(query, current_app.config["SANITIZE_PATTERN"])
try:
results = ailab_db_search(query)
return jsonify(results)
except DBError:
return jsonify({"error": current_app.config["ERROR_AILAB_FAILED"]}), 500
except Exception:
return jsonify({"error": current_app.config["ERROR_UNEXPECTED"]}), 500
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ git+https://github.com/ai-cfia/azure-db.git@main#subdirectory=azure-ai-search
fuzzywuzzy
python-Levenshtein
git+https://github.com/ai-cfia/ailab-db@main
flask_restx
Loading