DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Designing a Java Connector for Software Integrations
  • Vibe Coding With GitHub Copilot: Optimizing API Performance in Fintech Microservices
  • Revolutionizing Financial Monitoring: Building a Team Dashboard With OpenObserve
  • Unlocking the Benefits of a Private API in AWS API Gateway

Trending

  • A Modern Stack for Building Scalable Systems
  • Performance Optimization Techniques for Snowflake on AWS
  • Kubeflow: Driving Scalable and Intelligent Machine Learning Systems
  • Contextual AI Integration for Agile Product Teams
  1. DZone
  2. Data Engineering
  3. Databases
  4. Auto-Generating API Specifications as OpenAPI, WSDL, and Sphinx

Auto-Generating API Specifications as OpenAPI, WSDL, and Sphinx

This article presents a workflow for auto-generation of API specifications for your Zato services.

By 
Dariusz Suchojad user avatar
Dariusz Suchojad
·
Dec. 13, 19 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
23.2K Views

Join the DZone community and get the full member experience.

Join For Free

Sphinx statue

Auto-Generating API Specifications as OpenAPI, WSDL, and Sphinx


This article presents a workflow for auto-generation of API specifications for your Zato services. If you need to share your APIs with partners, external or internal, this is how it can be done.


Sample Services


Let's consider the services below — they represent a subset of a hypothetical API of a telecommunication company. In this case, they are to do with pre-paid cards. Deploy them on your servers in a module called api.py.

Note that their implementation is omitted, we only deal with their I/O, as it is expressed using SimpleIO.

What we would like to have, and what we will achieve here, is a website with static HTML describing the services in terms of a formal API specification.

Java
 




xxxxxxxxxx
1
32


 
1
# -*- coding: utf-8 -*-
2
 
          
3
# Zato
4
from zato.server.service import Int, Service
5
 
          
6
# #####################################################################
7
 
          
8
class RechargeCard(Service):
9
    """ Recharges a pre-paid card.
10
    Amount must not be less than 1 and it cannot be greater than 10000.
11
    """
12
    class SimpleIO:
13
        input_required = 'number', Int('amount')
14
        output_required = Int('status')
15
 
          
16
    def handle(self):
17
        pass
18
 
          
19
# #####################################################################
20
 
          
21
class GetCurrentBalance(Service):
22
    """ Returns current balance of a pre-paid card.
23
    """
24
    class SimpleIO:
25
        input_required = Int('number')
26
        output_required = Int('status')
27
        output_optional = 'balance'
28
 
          
29
    def handle(self):
30
        pass
31
 
          
32
# #####################################################################



Docstrings and SimpleIO


In the sample services, observe that:


  • Documentation is added as docstrings — this is something that services, being simply Python classes, will have anyway
  •    
  •  One of the services has a multi-line docstring, whereas the other one is single-line. This will be of significance later on
  •    
  • SimpleIO definitions use both string types and integers
You might also like:  Creating an API Specification in a Design Center

Command Line Usage


To generate API specifications, command zato apispec is used. This is part of the CLI that Zato ships with.

Typically, only well-chosen services should be documented publicly, and the main two options the command has are --include and --exclude.

Both accept a comma-separated list of shell-like glob patterns that indicate which services should or should not be documented.

For instance, if the code above is saved in api.py, the command to output their API specification is:

Java
 




xxxxxxxxxx
1


 
1
zato apispec /path/to/server        \
2
    --dir /path/to/output/directory \
3
    --include api.*



Next, we can navigate to the directory just created and type the command below to build HTML.

Java
 




xxxxxxxxxx
1


 
1
cd /path/to/output/directory
2
make html



OpenAPI, WSDL, and Sphinx


The result of the commands is as below — OpenAPI and WSDL files are in the menu column to the left.

Also, note that in the main index only the very first line of a docstring is used but upon opening a sub-page for each service its full docstring is used.


Branding and Customization


While the result is self-contained and it can be already used as-is, there is still room for more.

Given that the output is generated using Sphinx, it is possible to customize it as needed, for instance, by applying custom CSS or other branding information, such as the logo of a company exposing a given API.

All of the files used for generation of HTML are stored in config directories of each server — if the path to a server is /path/to/server, then the full path to Sphinx templates is in /path/to/server/config/repo/static/sphinxdoc/apispec.


Summary


That is everything — generating static documentation is a matter of just a single command. The output can be fully customized while the resulting OpenAPI and WSDL artifacts can be given to partners to let third-parties automatically generate API clients for your Zato services.

API

Opinions expressed by DZone contributors are their own.

Related

  • Designing a Java Connector for Software Integrations
  • Vibe Coding With GitHub Copilot: Optimizing API Performance in Fintech Microservices
  • Revolutionizing Financial Monitoring: Building a Team Dashboard With OpenObserve
  • Unlocking the Benefits of a Private API in AWS API Gateway

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!