Package: connexion-plus
Connexionwith benefits for microservices.Connexion PlusIf you want to useConnexionfor your microservice, you have to add anopentracingorprometheusclient on your own. With this library, you instantiate everything before your connexion app starts and this library will take care to put it all together, so you get everything fine.Currently this library works only for Flask!This library give you a new classApp, which you have to use instead of the connexion FlaskApp, to get everything working. The App inheritates from connexion app, so you can use it with your old code, but replace your import withfrom connexion_plus import App.If you want to know more about the used libraries, please go to the corresponding documentaries.DependenciesConnexionopentracing-python-instrumentationFlask-Opentracingjaeger-clientrequestsprometheus-flask-exporterImportingfromconnexion_plusimportAppOpenTracing / Jaeger-ClientCurrently, all opentracing implementation (e.g.jaeger-client) are supported for tracing. But this library use a third party function, that only supports Flask. If you want to use it, you have to initialize the client before you start your connexion app and give it via thetracer-parameter to theconnexion_plusApp, where the magic happens.If you want to use a default tracer, you can useuse_tracer=Truesimply.The following example uses jaeger-client (pip install jaeger-client) implementation. (Currently installs with connexion-plus per default)fromconnexion_plusimportAppfromjaeger_clientimportConfigasjConfigconfig=jConfig(config={'logging':True,},)jaeger_tracer=config.initialize_tracer()app=App(__name__,use_tracer=jaeger_tracer)If you use the tracer, you get also a TracingHandler in your logging module under the empty name, so your logging message can be logged with opentracing.logging.getLogger('')You can edit the logging-level with theuse_logging_level-parameter of the addServices-method. DEBUG is the default level, so you get everything from the log within a route in your opentracing-ui. (As long as there are a span while you write a logging message, you will see the logging message in your span)importloggingfromconnexion_plusimportAppapp=App(app,use_tracer=config.initialize_tracer(),use_logging_level=logging.DEBUG)It improve the performance slightly, when you set the log-level to a higher level (INFO, WARNING).Prometheus / MetricsCurrently, it is only theprometheus-flask-exportersupported for connexion, so only for flask connexion. You only have to set themetrics-parameter toTruefromconnexion_plusimportAppapp=App(__name__,use_metric=True)Use a default error handlerFor a faster implementation, you can use a default error handler. Set the parameteruse_default_handlerto True for use a simple default handler. Otherwise give a function / method to this parameter, which handles your exceptions.Complete exampleIf you want to usetracerandmetricstogether, see here a complete example. This currently works only with flask (see prometheus)fromconnexion_plusimportAppfromjaeger_clientimportConfigasjConfigfromjaeger_client.metrics.prometheusimportPrometheusMetricsFactoryimportloggingconfig=jConfig(config={'logging':True,},# use this, if you want to track your tracing itself with prometheusmetrics_factory=PrometheusMetricsFactory(namespace=name),)jaeger_tracer=config.initialize_tracer()app=App(name,use_tracer=config.initialize_tracer(),use_metric=True,use_optimizer=True,use_cors=True,use_logging_level=logging.DEBUG)app.add_api('openapi.yaml',resolver=RestyResolver('api'))If you add the linemetrics_factory=PrometheusMetricsFactory(namespace='yourAppName')to your jaeger-client-config, you get the metrics out of jaeger into your flask app to track all metrics at once/metrics.More featuresIf you want to get the current tracing object in a request context, you can useFlaskTracingimportFlasktracingFlaskTracing.get_span(request)If you get a collision of your view-functions, you can usefrom connexion-plus.MultipleResourceResolver import MultipleResourceResolveras a replacement for RestyResolver to get better control of multi resource path e.g. /resource1/{id1}/resource2/{id2} tries to find the classesResource1Resource2orresource1resource2in the given "api" folder.Use of optimizerIf you want to use the optimizer or use custom configs for single routes, you can use the FlaskOptimize classfrom connexion_plus import FlaskOptimize.
This class has the methodsdo_not_minify(the decorated route will not minified before send),do_not_compress(the decorated route will not compressed before send) andset_cache_timeout(seconds)(default 24h) (the response from the decorated route will be cached untilseconds).Currently it is only be possible to deactivate the global configuse_optimizerand not activate single routes with e.g.minify. This could be your first contributation to this project. :)Importing Multiple ResourcesIf you want to make usage of multiple resource in a single URL (e.g. /Res1/{Para1}/Res2), you can use thefrom connexion-plus import MultiResourceResolveras your connexion resolver. This resolves the example:/Res1/{Para1}/Res2 resolves in Res1.Res2and as a convenient function, it resolves also/Res1/{Para1}/Res2 resolves in Res1Res2, so you can choose both. The first one searches for folders and at last a fileRes1/Res2.py. The second searches a file with this nameRes1Res2.py. Currently no classes inside the files are supported.If you want to add Methods to a resource (e.g. Res1 should answer for GET and POST), you have to add an__init__.pyto the folder and import there your resource-filefrom .Res1 import *. If you use the second method, you don't have a folder and a file with the nameRes1, so you don't need this workaround.ExamplesYou can find more examples in therepo.Tutorial1is a simple small (without bonuscode) script without an openapi definition.
Please useTutorial2if you want a complete usage example.Research data servicesThis library is under development for the projectresearch data services, a microservice ecosystem.