Skip to content

Support more parameters for Connexion #222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions pyms/flask/services/service_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from pyms.constants import LOGGER_NAME
from pyms.flask.services.driver import DriverService
from pyms.utils import import_from
from pyms.utils.utils import import_class

logger = logging.getLogger(LOGGER_NAME)

Expand Down Expand Up @@ -65,9 +65,7 @@ def get_client(self) -> ServiceDiscoveryBase:
if self.service == CONSUL_SERVICE_DISCOVERY:
client = ServiceDiscoveryConsul(self)
else:
service_paths = self.service.split(".")
package = ".".join(service_paths[:-1])
client = import_from(package, service_paths[-1])(self)
client = import_class(self.service)(self)

logger.debug("Init %s as service discovery", client)
return client
22 changes: 18 additions & 4 deletions pyms/flask/services/swagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from pyms.exceptions import AttrDoesNotExistException
from pyms.flask.services.driver import DriverService
from pyms.utils import check_package_exists
from pyms.utils.utils import check_package_exists, import_class

SWAGGER_PATH = "swagger"
SWAGGER_FILE = "swagger.yaml"
Expand Down Expand Up @@ -62,7 +62,14 @@ class Service(DriverService):
"""

config_resource = "swagger"
default_values = {"path": SWAGGER_PATH, "file": SWAGGER_FILE, "url": SWAGGER_URL, "project_dir": PROJECT_DIR}
default_values = {
"path": SWAGGER_PATH,
"file": SWAGGER_FILE,
"url": SWAGGER_URL,
"project_dir": PROJECT_DIR,
"validator_map": {},
"validate_responses": True,
}

@staticmethod
def _get_application_root(config) -> str:
Expand Down Expand Up @@ -97,27 +104,34 @@ def init_app(self, config, path: Path) -> Flask:
:return: Flask
"""
check_package_exists("connexion")

# Set paths
specification_dir = self.path
application_root = self._get_application_root(config)
if not os.path.isabs(self.path):
specification_dir = os.path.join(path, self.path)

app = connexion.App(__name__, specification_dir=specification_dir, resolver=RestyResolver(self.project_dir))

# Prepare params
validator_map = {k: import_class(v) for k, v in self.validator_map.items()}
params = {
"specification": get_bundled_specs(Path(os.path.join(specification_dir, self.file)))
if prance
else self.file,
"arguments": {"title": config.APP_NAME},
"base_path": application_root,
"options": {"swagger_url": self.url},
"validator_map": validator_map,
"validate_responses": self.validate_responses,
}

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

# Initialize connexion
app = connexion.App(__name__, specification_dir=specification_dir, resolver=RestyResolver(self.project_dir))
app.add_api(**params)

# Invert the objects, instead connexion with a Flask object, a Flask object with
application = app.app
application.connexion_app = app
Expand Down
7 changes: 7 additions & 0 deletions pyms/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ def import_package(package: Text):
return importlib.import_module(package)


def import_class(module_class_text: Text) -> type:
module_class_paths = module_class_text.split(".")
package = ".".join(module_class_paths[:-1])
module_class = import_from(package, module_class_paths[-1])
return module_class


def check_package_exists(package_name: Text) -> Union[Exception, bool]:
spec = importlib.util.find_spec(package_name)
if spec is None:
Expand Down