Package: pip-services3-swagger
Swagger UI for PythonThis module is a part of thePip.Servicespolyglot microservices toolkit.The swagger module provides a Swagger UI that can be added into microservices and seamlessly integrated with existing REST and Commandable HTTP services.The module contains the following packages:Build- Swagger service factoryServices- Swagger UI serviceQuick links:API ReferenceChange LogGet HelpContributeUseInstall the Python package aspipinstallpip-services3-swaggerDevelop a RESTful service component. For example, it may look the following way.
In theregistermethod we load an Open API specification for the service.
You can also enable swagger by default in the constractor by setting_swagger_enableproperty.classMyRestService(RestService):def__init__(self):super();self._base_route="myservice"self._swagger_enable=Truedef__greeting(self):name=bottle.request.query.get('name')response="Hello, "+name+"!"returnself.send_result(result)defregister(self):self.register_route('get','/greeting',ObjectSchema(True).with_required_property("name",TypeCode.String),self.__greeting)self.register_open_api_spec_from_file('./pip_services3_swagger/services/myservice.yml')The Open API specification for the service shall be prepared either manually
or usingSwagger Editoropenapi:'3.0.2'info:title:'MyService'description:'MyServiceRESTAPI'version:'1'paths:/myservice/greeting:get:tags:-myserviceoperationId:'greeting'parameters:-name:correlation_idin:querydescription:Correlation IDrequired:falseschema:type:string-name:namein:querydescription:Name of a personrequired:trueschema:type:stringresponses:200:description:'Successfulresponse'content:application/json:schema:type:'string'Include Swagger service intoconfig.ymlfile and enable swagger for your REST or Commandable HTTP services.
Also explicitely adding HttpEndpoint allows to share the same port betwee REST services and the Swagger service.---...# Shared HTTP Endpoint-descriptor:"pip-services:endpoint:http:default:1.0"connection:protocol:httphost:localhostport:8080# Swagger Service-descriptor:"pip-services:swagger-service:http:default:1.0"# My RESTful Service-descriptor:"myservice:service:rest:default:1.0"swagger:enable:trueFinally, remember to add factories to your container, to allow it creating required components.frompip_services3_rpc.build.DefaultRpcFactoryimportDefaultRpcFactoryfrompip_services3_swagger.build.DefaultSwaggerFactoryimportDefaultSwaggerFactory# ...classMyProcess(ProccesContainer):def__init__(self):super(MyProcess,self).__init__("myservice","MyService microservice")self._factories.add(DefaultRpcFactory())self._factories.add(DefaultSwaggerFactory())self._factories.add(MyServiceFactory())# ...Launch the microservice and open the browser to open the Open API specification athttp://localhost:8080/greeting/swaggerThen open the Swagger UI using the linkhttp://localhost:8080/swagger.
The result shall look similar to the picture below.DevelopFor development you shall install the following prerequisites:Python 3.7+Visual Studio Code or another IDE of your choiceDockerInstall dependencies:pipinstall-rrequirements.txtRun automated tests:pythontest.pyGenerate API documentation:./docgen.ps1Before committing changes run dockerized build and test as:./build.ps1
./test.ps1
./clear.ps1ContactsThe library is created and maintained by:Sergey SeroukhovDanil Prisiazhnyi