2020-09-15 11:22:28 +00:00
|
|
|
import flask
|
2020-09-15 15:44:12 +00:00
|
|
|
import json
|
2020-09-17 19:09:22 +00:00
|
|
|
import requests
|
2020-09-15 11:22:28 +00:00
|
|
|
import teal.marshmallow
|
|
|
|
|
|
|
|
from typing import Callable, Iterable, Tuple
|
2020-09-17 19:09:22 +00:00
|
|
|
from urllib.parse import urlparse
|
2020-09-15 11:22:28 +00:00
|
|
|
from flask import make_response, g
|
2020-10-09 09:00:49 +00:00
|
|
|
from flask.json import jsonify
|
2020-09-15 15:44:12 +00:00
|
|
|
from teal.resource import Resource, View
|
|
|
|
|
2020-09-17 19:09:22 +00:00
|
|
|
from ereuse_devicehub.resources.inventory.model import Inventory
|
2020-09-22 09:57:39 +00:00
|
|
|
from ereuse_devicehub import __version__
|
2020-09-17 19:09:22 +00:00
|
|
|
|
|
|
|
|
2020-09-18 09:05:42 +00:00
|
|
|
def get_tag_version(app):
|
2020-09-17 19:09:22 +00:00
|
|
|
"""Get version of microservice ereuse-tag."""
|
2020-09-18 09:05:42 +00:00
|
|
|
|
2020-09-17 19:09:22 +00:00
|
|
|
path = "/versions/version/"
|
|
|
|
url = urlparse(Inventory.current.tag_provider.to_text())._replace(path=path)
|
2020-09-18 09:05:42 +00:00
|
|
|
try:
|
|
|
|
res = requests.get(url.geturl())
|
|
|
|
except requests.exceptions.ConnectionError:
|
|
|
|
app.logger.error("The microservice Tags is down!!")
|
|
|
|
return {}
|
|
|
|
|
2020-09-17 19:09:22 +00:00
|
|
|
if res.status_code == 200:
|
|
|
|
return json.loads(res.content)
|
|
|
|
else:
|
|
|
|
return {}
|
2020-09-15 15:44:12 +00:00
|
|
|
|
|
|
|
class VersionView(View):
|
|
|
|
def get(self, *args, **kwargs):
|
2020-09-17 19:09:22 +00:00
|
|
|
"""Get version of DeviceHub and ereuse-tag."""
|
2020-09-22 09:57:39 +00:00
|
|
|
|
2020-09-18 09:05:42 +00:00
|
|
|
tag_version = get_tag_version(self.resource_def.app)
|
2020-09-22 09:57:39 +00:00
|
|
|
versions = {'devicehub': __version__, "ereuse_tag": "0.0.0"}
|
2020-09-17 19:09:22 +00:00
|
|
|
versions.update(tag_version)
|
2020-10-09 09:00:49 +00:00
|
|
|
|
|
|
|
ret = jsonify(versions)
|
|
|
|
ret.status_code = 200
|
2020-10-09 09:35:16 +00:00
|
|
|
return ret
|
2020-09-15 11:22:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
class VersionDef(Resource):
|
|
|
|
__type__ = 'Version'
|
|
|
|
SCHEMA = None
|
|
|
|
VIEW = None # We do not want to create default / documents endpoint
|
|
|
|
AUTH = False
|
|
|
|
|
|
|
|
def __init__(self, app,
|
|
|
|
import_name=__name__,
|
2020-09-18 09:05:42 +00:00
|
|
|
static_folder=None,
|
2020-09-15 11:22:28 +00:00
|
|
|
static_url_path=None,
|
2020-09-18 09:05:42 +00:00
|
|
|
template_folder=None,
|
2020-09-15 11:22:28 +00:00
|
|
|
url_prefix=None,
|
|
|
|
subdomain=None,
|
|
|
|
url_defaults=None,
|
|
|
|
root_path=None,
|
|
|
|
cli_commands: Iterable[Tuple[Callable, str or None]] = tuple()):
|
2020-09-15 15:44:12 +00:00
|
|
|
super().__init__(app, import_name, static_folder, static_url_path, template_folder,
|
2020-09-15 11:22:28 +00:00
|
|
|
url_prefix, subdomain, url_defaults, root_path, cli_commands)
|
|
|
|
|
2020-09-22 09:57:39 +00:00
|
|
|
d = {'devicehub': __version__, "ereuse_tag": "0.0.0"}
|
2020-09-15 15:44:12 +00:00
|
|
|
get = {'GET'}
|
2020-09-15 11:22:28 +00:00
|
|
|
|
2020-09-16 15:40:16 +00:00
|
|
|
version_view = VersionView.as_view('VersionView', definition=self)
|
2020-09-15 15:44:12 +00:00
|
|
|
self.add_url_rule('/', defaults=d, view_func=version_view, methods=get)
|