Skip to content

Commit

Permalink
issue #21: implement versioning logic
Browse files Browse the repository at this point in the history
  • Loading branch information
k-allagbe committed Dec 22, 2023
1 parent e44e828 commit f9742f0
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 6 deletions.
File renamed without changes.
Empty file added app/api/common/__init__.py
Empty file.
File renamed without changes.
File renamed without changes.
13 changes: 13 additions & 0 deletions app/api/v1/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from flask import Blueprint

from .blueprints.monitor import monitor_blueprint
from .blueprints.search import search_blueprint

VERSION = "1"


def get_blueprint():
bp = Blueprint(VERSION, __name__)
bp.register_blueprint(monitor_blueprint, url_prefix="/health", strict_slashes=False)
bp.register_blueprint(search_blueprint, url_prefix="/search", strict_slashes=False)
return bp
Empty file.
File renamed without changes.
4 changes: 2 additions & 2 deletions app/blueprints/search.py → app/api/v1/blueprints/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from flask import Blueprint, current_app, jsonify, request
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.api.common.ailab_db import DBError, ailab_db_search
from app.api.common.finesse_data import FinesseDataFetchException, fetch_data
from app.utils import sanitize

search_blueprint = Blueprint("finesse", __name__)
Expand Down
13 changes: 13 additions & 0 deletions app/api/v2/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from flask import Blueprint

from .blueprints.monitor import monitor_blueprint
from .blueprints.search import search_blueprint

VERSION = "2"


def get_blueprint():
bp = Blueprint(VERSION, __name__)
bp.register_blueprint(monitor_blueprint, url_prefix="/health", strict_slashes=False)
bp.register_blueprint(search_blueprint, url_prefix="/search", strict_slashes=False)
return bp
Empty file.
8 changes: 8 additions & 0 deletions app/api/v2/blueprints/monitor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from flask import Blueprint

monitor_blueprint = Blueprint("monitor", __name__)


@monitor_blueprint.route("", methods=["GET"])
def health():
return "ok", 200
65 changes: 65 additions & 0 deletions app/api/v2/blueprints/search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from functools import wraps

from flask import Blueprint, current_app, jsonify, request
from index_search import AzureIndexSearchQueryError, search

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

search_blueprint = Blueprint("finesse", __name__)


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_blueprint.route("/azure", methods=["POST"])
@require_non_empty_query
def search_azure():
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_blueprint.route("/static", methods=["POST"])
@require_non_empty_query
def search_static():
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_blueprint.route("/ailab", methods=["POST"])
@require_non_empty_query
def search_ailab_db():
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
9 changes: 5 additions & 4 deletions app/app_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ def create_app(config: Config):
CORS(app)
app.config.from_object(config)

from .blueprints.monitor import monitor_blueprint
from .blueprints.search import search_blueprint
from .api import v1, v2

app.register_blueprint(
monitor_blueprint, url_prefix="/health", strict_slashes=False
v1.get_blueprint(), url_prefix=f"/api/v{v1.VERSION}", strict_slashes=False
)
app.register_blueprint(
v2.get_blueprint(), url_prefix=f"/api/v{v2.VERSION}", strict_slashes=False
)
app.register_blueprint(search_blueprint, url_prefix="/search", strict_slashes=False)

return app

0 comments on commit f9742f0

Please sign in to comment.