8000 Support more parameters for Connexion (#222) · python-microservices/pyms@95b3048 · GitHub
[go: up one dir, main page]

Skip to content

Commit 95b3048

Browse files
authored
Support more parameters for Connexion (#222)
1 parent 6cf7e0e commit 95b3048

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

pyms/flask/services/service_discovery.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from pyms.constants import LOGGER_NAME
99
from pyms.flask.services.driver import DriverService
10-
from pyms.utils import import_from
10+
from pyms.utils.utils import import_class
1111

1212
logger = logging.getLogger(LOGGER_NAME)
1313

@@ -65,9 +65,7 @@ def get_client(self) -> ServiceDiscoveryBase:
6565
if self.service == CONSUL_SERVICE_DISCOVERY:
6666
client = ServiceDiscoveryConsul(self)
6767
else:
68-
service_paths = self.service.split(".")
69-
package = ".".join(service_paths[:-1])
70-
client = import_from(package, service_paths[-1])(self)
68+
client = import_class(self.service)(self)
7169

7270
logger.debug("Init %s as service discovery", client)
7371
return client

pyms/flask/services/swagger.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
from pyms.exceptions import AttrDoesNotExistException
1616
from pyms.flask.services.driver import DriverService
17-
from pyms.utils import check_package_exists
17+
from pyms.utils.utils import check_package_exists, import_class
1818

1919
SWAGGER_PATH = "swagger"
2020
SWAGGER_FILE = "swagger.yaml"
@@ -62,7 +62,14 @@ class Service(DriverService):
6262
"""
6363

6464
config_resource = "swagger"
65-
default_values = {"path": SWAGGER_PATH, "file": SWAGGER_FILE, "url": SWAGGER_URL, "project_dir": PROJECT_DIR}
65+
default_values = {
66+
"path": SWAGGER_PATH,
67+
"file": SWAGGER_FILE,
68+
"url": SWAGGER_URL,
69+
"project_dir": PROJECT_DIR,
70+
"validator_map": {},
71+
"validate_responses": True,
72+
}
6673

6774
@staticmethod
6875
def _get_application_root(config) -> str:
@@ -97,27 +104,34 @@ def init_app(self, config, path: Path) -> Flask:
97104
:return: Flask
98105
"""
99106
check_package_exists("connexion")
107+
108+
# Set paths
100109
specification_dir = self.path
101110
application_root = self._get_application_root(config)
102111
if not os.path.isabs(self.path):
103112
specification_dir = os.path.join(path, self.path)
104113

105-
app = connexion.App(__name__, specification_dir=specification_dir, resolver=RestyResolver(self.project_dir))
106-
114+
# Prepare params
115+
validator_map = {k: import_class(v) for k, v in self.validator_map.items()}
107116
params = {
108117
"specification": get_bundled_specs(Path(os.path.join(specification_dir, self.file)))
109118
if prance
110119
else self.file,
111120
"arguments": {"title": config.APP_NAME},
112121
"base_path": application_root,
113122
"options": {"swagger_url": self.url},
123+
"validator_map": validator_map,
124+
"validate_responses": self.validate_responses,
114125
}
115126

116127
# Fix Connexion issue https://github.com/zalando/connexion/issues/1135
117128
if application_root == "/":
118129
del params["base_path"]
119130

131+
# Initialize connexion
132+
app = connexion.App(__name__, specification_dir=specification_dir, resolver=RestyResolver(self.project_dir))
120133
app.add_api(**params)
134+
121135
# Invert the objects, instead connexion with a Flask object, a Flask object with
122136
application = app.app
123137
application.connexion_app = app

pyms/utils/utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ def import_package(package: Text):
2727
return importlib.import_module(package)
2828

2929

30+
def import_class(module_class_text: Text) -> type:
31+
module_class_paths = module_class_text.split(".")
32+
package = ".".join(module_class_paths[:-1])
33+
module_class = import_from(package, module_class_paths[-1])
34+
return module_class
35+
36+
3037
def check_package_exists(package_name: Text) -> Union[Exception, bool]:
3138
spec = importlib.util.find_spec(package_name)
3239
if spec is None:

0 commit comments

Comments
 (0)
0