2. WHAT IS SWAGGER?
• The goal of Swagger™ is to define a standard, language-agnostic
interface to REST APIs which allows both humans and
computers to discover and understand the capabilities of the
service without access to source code, documentation, or
through network traffic inspection.
• Swagger is a formal specification surrounded by a large
ecosystem of tools
3. SWAGGER ECOSYSTEM
• Swagger Editor
edit API specifications inYAML inside browser and preview
documentations in real time.
• Swagger Codegen
allows generation of both client libraries and server stubs from a
Swagger definition.
• Swagger UI
dependency-free collection of HTML, Javascript, and CSS assets that
dynamically generate beautiful documentation from a Swagger-
compliant API
http://swagger.io/tools/
4. USAGE PATTERNS FOR API PROVIDERS
Swagger
Editor
Swagger
Definition
Swagger
Codegen
Swagger
UI
API
Swagger Editor
Swagger
Definition
Swagger
Codegen
Swagger
UI
API
API
Swagger-Core
JAX-RS
Automatic
generation
top-down approach
bottom-up approach
5. SWAGGER SPECIFICATION
• The Swagger representation of the API is made of a single file
swagger.json (but can refer to other resources)
• Represented as JSON, butYAML can be used
• All field names are case sensitive
• Primitive data types in the Swagger Specification are based
on the types supported by the JSON-Schema Draft 4.
• Models are described using the Schema Object which is a
subset of JSON Schema Draft 4.
https://github.com/swagger-api/swagger-spec/
6. SWAGGER OBJECT
info
API metadata
paths
available paths and operations
definitions
data types produced and consumed by operations
parameters
parameters that can be used across operations
responses
responses that can be used across operations
securityDefinitions
security scheme definitions
security
alternative security schemes that can be used
tags
list of tags with additional metadata
externalDocs
additional external documentation
12. UNFORMAL SPECIFICATION
• We want to design DEMO API that can provide access to our database of
articles
• Every article in the database has a list of authors, so internally we have 2
main data objects:Article and Author
• All responses from our API should be made in JSON
• We want to able:
1. get all articles
GET /v1/articles
2. get all authors from the particular article
GET /v1/article/{id}/authors
19. SWAGGER-UI
• a part of the Swagger project
• a dependency-free collection of HTML, JS and CSS assets that
dynamically generate documentation
• can host in any server environment
• easy to install and configure
• customizable and supports easy localization and translation
• supports invocation of all HTTP methods (GET, PUT, POST,
DELETE, PATCH, OPTIONS)
20. SWAGGER UI SETUP
• Make swagger.json and all external $ref accessible via internet
• Setup CORS settings and (optionally) api_key to access them
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, DELETE, PUT,
PATCH, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Content-Type, api_key,Authorization';
• Download Swagger UI to the server e.g. as swagger-ui
• Make swagger-ui/dist folder accessible via browser (e.g.
configure webserver)
• Just run open it in the browser
23. PYSWAGGER
• a python client for Swagger enabled REST API
• supports Swagger 1.2, 2.0 on python 2.6, 2.7, 3.3, 3.4
• tries hard to fully supports Swagger Spec in all aspects
• supports JSON andYAML
• provides client implementation based on various http
clients in python
• easy to unittest API
• easy to validate API definition according to spec