Skip to content

Commit

Permalink
issue #21: create version endpoint + test
Browse files Browse the repository at this point in the history
  • Loading branch information
k-allagbe committed Dec 22, 2023
1 parent d9f48b3 commit 0e1cdb8
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 12 deletions.
9 changes: 9 additions & 0 deletions app/api/common/api_version/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from dataclasses import dataclass


@dataclass
class ApiVersion:
full: str
release_date: str
deprecated: bool
supported_until: str | None = None
10 changes: 5 additions & 5 deletions app/api/v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

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

VERSION = "1"
from .blueprints.version import VERSION, version_blueprint


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)
bp = Blueprint(VERSION.full, __name__)
bp.register_blueprint(monitor_blueprint, url_prefix="/health")
bp.register_blueprint(search_blueprint, url_prefix="/search")
bp.register_blueprint(version_blueprint, url_prefix="/version")
return bp
13 changes: 13 additions & 0 deletions app/api/v1/blueprints/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from dataclasses import asdict

from flask import Blueprint, jsonify

from app.api.common.api_version import ApiVersion

VERSION = ApiVersion(full="1", release_date="2023-12", deprecated=False)
version_blueprint = Blueprint("version", __name__)


@version_blueprint.route("", methods=["GET"])
def version():
return jsonify({"version": asdict(VERSION)})
10 changes: 5 additions & 5 deletions app/api/v2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

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

VERSION = "2"
from .blueprints.version import VERSION, version_blueprint


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)
bp = Blueprint(VERSION.full, __name__)
bp.register_blueprint(monitor_blueprint, url_prefix="/health")
bp.register_blueprint(search_blueprint, url_prefix="/search")
bp.register_blueprint(version_blueprint, url_prefix="/version")
return bp
13 changes: 13 additions & 0 deletions app/api/v2/blueprints/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from dataclasses import asdict

from flask import Blueprint, jsonify

from app.api.common.api_version import ApiVersion

VERSION = ApiVersion(full="2", release_date="2024-01", deprecated=False)
version_blueprint = Blueprint("version", __name__)


@version_blueprint.route("", methods=["GET"])
def version():
return jsonify({"version": asdict(VERSION)})
4 changes: 2 additions & 2 deletions app/app_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ def create_app(config: Config):
from .api import v1, v2

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

return app
17 changes: 17 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def setUp(self):
self.azure_search_url = "/api/v1/search/azure"
self.ailab_search_url = "/api/v1/search/ailab"
self.health_check_url = "/api/v1/health"
self.version_url = "/api/v1/version"

### Static ###
def test_search_static_success(self):
Expand Down Expand Up @@ -96,3 +97,19 @@ def test_health_route(self):
response = self.client.get(self.health_check_url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data.decode(), "ok")

### Version ###
def test_version_route(self):
response = self.client.get(self.version_url)
self.assertEqual(response.status_code, 200)
self.assertDictEqual(
response.json,
{
"version": {
"deprecated": False,
"full": "1",
"release_date": "2023-12",
"supported_until": None,
}
},
)

0 comments on commit 0e1cdb8

Please sign in to comment.