Skip to content

Commit

Permalink
Added functions to get API version
Browse files Browse the repository at this point in the history
  • Loading branch information
dsbilling committed Nov 27, 2023
1 parent d60fb87 commit 1d73039
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 1 deletion.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@

flask>=2.3.0,>=3.0.0
61 changes: 61 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from flask import Flask

from tunsberg.api import get_api_version, get_api_version_from_flask_url


app = Flask(__name__)


def test_get_api_version_from_flask_url_default():
""" Test that the default pattern matches """
with app.test_request_context('/api/v1/resource'):
assert get_api_version_from_flask_url() == 'v1'


def test_get_api_version_from_flask_url_custom_pattern():
""" Test that the custom pattern matches """
with app.test_request_context('/api/v2_1/resource'):
assert get_api_version_from_flask_url(pattern=r'v\d+_\d+') == 'v2_1'


def test_get_api_version_from_flask_url_no_version():
""" Test that the default return value is empty string """
with app.test_request_context('/api/resource'):
assert get_api_version_from_flask_url() == ''


def test_get_api_version_from_flask_url_default_return():
""" Test that the default return value is 'v0' """
with app.test_request_context('/api/resource'):
assert get_api_version_from_flask_url(default='v0') == 'v0'


def test_get_api_version_from_flask_url_no_default_return():
""" Test that the default return value is empty string """
with app.test_request_context('/api/resource'):
assert get_api_version_from_flask_url(default='') == ''


def test_get_api_version_standard():
""" Test that the standard pattern matches """
assert get_api_version('app.api.v1') == 'v1'
assert get_api_version('module.v2.submodule') == 'v2'
assert get_api_version('service.v3_1.beta') == 'v3_1'


def test_get_api_version_no_version():
""" Test that the default return value is empty string """
assert get_api_version('app.api') == ''
assert get_api_version('module.submodule') == ''


def test_get_api_version_custom_pattern():
""" Test that the custom pattern matches """
assert get_api_version('app.api.v2021_04', pattern=r'v\d{4}_\d{2}') == 'v2021_04'
assert get_api_version('service.v2021.beta', pattern=r'v\d{4}') == 'v2021'


def test_get_api_version_custom_default():
""" Test that the custom default value is returned """
assert get_api_version('app.api', default='no_version') == 'no_version'
assert get_api_version('module.submodule', default='undefined') == 'undefined'
52 changes: 52 additions & 0 deletions tunsberg/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import re

from flask import request


def get_api_version(folder_name: str, pattern: str = r'v\d+(_\d+)?', default: str = '') -> str:
r"""
Get API version from folder name.
:param folder_name: Folder name
:type folder_name: str
:param pattern: Regular expression pattern to match version. Default is 'v\\d+(_\\d+)?',
which matches 'v<number>' or 'v<number>_<number>'.
:type pattern: str
:param default: Default return value if no version is found. Defaults to ''.
:type default: str
:return: API version as string, or the default value if no version is found
:rtype: str
"""
if folder_name == 'app.api':
return default

version_parts = folder_name.split('.')
version_regex = re.compile(pattern)

for part in version_parts:
if version_regex.match(part):
return part

return default


def get_api_version_from_flask_url(pattern: str = r'v\d+(_\d+)?', default: str = '') -> str:
r"""
Get the API version from a Flask URL.
:param pattern: Regular expression pattern to match version. Default is 'v\\d+(_\\d+)?',
which matches 'v<number>' or 'v<number>_<number>'.
:type pattern: str
:param default: Default return value if no version is found. Defaults to ''.
:type default: str
:return: API version as string, or the default value if no version is found
:rtype: str
"""
url_list = request.url.split('/')
version_regex = re.compile(pattern)

for item in url_list:
if version_regex.match(item):
return item

return default

0 comments on commit 1d73039

Please sign in to comment.