Skip to content

Commit dc3fb28

Browse files
committed
Split swagger conf in other class
1 parent 7a25a7f commit dc3fb28

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

pyms/constants.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
CONFIGMAP_FILE_ENVIRONMENT = "CONFIGMAP_FILE"
22

3-
LOGGER_NAME = "pyms"
3+
LOGGER_NAME = "pyms"
4+
5+
SWAGGER_PATH = "swagger"
6+
SWAGGER_FILE = "swagger.yaml"

pyms/flask/app/__init__.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from flask_opentracing import FlaskTracer
77

88
from pyms.config.conf import get_conf
9+
from pyms.flask.app.swagger import Swagger
910
from pyms.flask.healthcheck import healthcheck_blueprint
1011
from pyms.logger import CustomJsonFormatter
1112
from pyms.tracer.main import init_jaeger_tracer
@@ -18,6 +19,8 @@ class Microservice:
1819
def __init__(self, service: Text, path=__file__):
1920
self.service = service
2021
self.path = os.path.dirname(path)
22+
self.swagger = Swagger()
23+
self.config = get_conf(service=self.service)
2124

2225
def init_libs(self):
2326
return self.application
@@ -29,16 +32,16 @@ def create_app(self):
2932
return the app and the database objects.
3033
:return:
3134
"""
32-
config = get_conf(service=self.service)
33-
app = connexion.App(__name__, specification_dir=os.path.join(self.path, 'swagger'))
34-
app.add_api('swagger.yaml',
35-
arguments={'title': config.APP_NAME},
36-
base_path=config.APPLICATION_ROOT
35+
36+
app = connexion.App(__name__, specification_dir=os.path.join(self.path, self.swagger.path))
37+
app.add_api(self.swagger.file,
38+
arguments={'title': self.config.APP_NAME},
39+
base_path=self.config.APPLICATION_ROOT
3740
)
3841

3942
self.application = app.app
4043
self.application._connexion_app = app
41-
self.application.config.from_object(config)
44+
self.application.config.from_object(self.config)
4245
self.application.tracer = None
4346

4447
# Initialize Blueprints
@@ -74,5 +77,4 @@ def add_error_handler(self, code_or_exception, handler):
7477
:param code_or_exception: HTTP error code or exception
7578
:param handler: callback for error handler
7679
"""
77-
7880
self.application._connexion_app.add_error_handler(code_or_exception, handler)

pyms/flask/app/swagger.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from pyms.exceptions import AttrDoesNotExistException
2+
3+
from pyms.config.conf import get_conf
4+
from pyms.constants import SWAGGER_PATH, SWAGGER_FILE
5+
6+
7+
class Swagger:
8+
service = "swagger"
9+
10+
def __init__(self):
11+
self.config = get_conf(service=self.service, empty_init=True)
12+
13+
@property
14+
def path(self):
15+
path = self.config.path
16+
return path if path else SWAGGER_PATH
17+
18+
@property
19+
def file(self):
20+
swagger_file = self.config.file
21+
return swagger_file if swagger_file else SWAGGER_FILE

0 commit comments

Comments
 (0)