Skip to main content

Pip.Services Logo
Swagger UI for Python

This module is a part of the Pip.Services polyglot 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 factory
  • Services - Swagger UI service

Quick links:

Use

Install the Python package as

pip install pip-services3-swagger

Develop a RESTful service component. For example, it may look the following way. In the register method we load an Open API specification for the service. You can also enable swagger by default in the constractor by setting _swagger_enable property.

class MyRestService(RestService):
    def __init__(self):
        super();
        self._base_route = "myservice"
        self._swagger_enable = True

    def __greeting(self):
        name = bottle.request.query.get('name')
        response = "Hello, " + name + "!"
        return self.send_result(result)

    def register(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 using Swagger Editor

openapi: '3.0.2'
info:
  title: 'MyService'
  description: 'MyService REST API'
  version: '1'
paths:
  /myservice/greeting:
    get:
      tags:
        - myservice
      operationId: 'greeting'
      parameters:
      - name: correlation_id
        in: query
        description: Correlation ID
        required: false
        schema:
          type: string
      - name: name
        in: query
        description: Name of a person
        required: true
        schema:
          type: string
      responses:
        200:
          description: 'Successful response'
          content:
            application/json:
              schema:
                type: 'string'

Include Swagger service into config.yml file 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: http
    host: localhost
    port: 8080

# Swagger Service
- descriptor: "pip-services:swagger-service:http:default:1.0"

# My RESTful Service
- descriptor: "myservice:service:rest:default:1.0"
  swagger:
    enable: true

Finally, remember to add factories to your container, to allow it creating required components.

from pip_services3_rpc.build.DefaultRpcFactory import DefaultRpcFactory
from pip_services3_swagger.build.DefaultSwaggerFactory import DefaultSwaggerFactory
# ...
class MyProcess(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 at http://localhost:8080/greeting/swagger

Then open the Swagger UI using the link http://localhost:8080/swagger. The result shall look similar to the picture below.

Develop

For development you shall install the following prerequisites:

  • Python 3.7+
  • Visual Studio Code or another IDE of your choice
  • Docker

Install dependencies:

pip install -r requirements.txt

Run automated tests:

python test.py

Generate API documentation:

./docgen.ps1

Before committing changes run dockerized build and test as:

./build.ps1
./test.ps1
./clear.ps1

Contacts

The library is created and maintained by:

  • Sergey Seroukhov
  • Danil Prisiazhnyi

Metadata

Release files for pip-services3-swagger 3.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pip-services3-swagger 3.0.2
File Size Uploaded
pip_services3_swagger-3.0.2.tar.gz 5.0 MB Details

Release files / pip_services3_swagger-3.0.2.tar.gz

Download URL pip_services3_swagger-3.0.2.tar.gz
Size 5.0 MB
Tags Source
SHA-256 checksum
How to use checksums
a364952736db64006d922af3e7a306ebb3971ec68d1dc07aeadddb5426df4464
BLAKE2b-256 checksum
How to use checksums
ce1fd4860f8cbddbd265a71722a15d55c8472b2deda8feec681f9b19fcbe5095
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.26.0 setuptools/57.4.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.2

Release history Release notifications | RSS feed

This release

3.0.2 This release

1 release file

3.0.1

1 release file

3.0.0

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page