A Tour of Swagger for APIs

34
A tour of Swagger for (REST) APIs Allen Dean Information Development, IBM Watson™ #stc15

Transcript of A Tour of Swagger for APIs

Page 1: A Tour of Swagger for APIs

A tour of Swagger for (REST) APIs

Allen DeanInformation Development, IBM Watson™

#stc15

Page 2: A Tour of Swagger for APIs

Swagger is API documentation

Right?

© 2015 International Business Machines Corporation 2

Page 3: A Tour of Swagger for APIs

Well yes. But, just one of the tools…

…called Swagger UI

© 2015 International Business Machines Corporation 3

Page 4: A Tour of Swagger for APIs

Swagger is a specification…

…for describing a REST API

© 2015 International Business Machines Corporation 4

Page 5: A Tour of Swagger for APIs

And Swagger is a…

…set of core tools© 2015 International Business Machines Corporation 5

Page 6: A Tour of Swagger for APIs

And more tools…

© 2015 International Business Machines Corporation 6

Page 7: A Tour of Swagger for APIs

© 2015 International Business Machines Corporation 7

A robust community

Page 8: A Tour of Swagger for APIs

Lots of support…

…through community tools

• Projects by languages:• Clojure, Go, Java, JavaScript, .Net, Node.js, PERL, PHP, Python,

Ruby..

• Projects by frameworks or platforms:• Angular.js, Apache (Ant, Maven, Wink), Django, Express, Flask,

Gradle, Grunt, MongoDB, Spring…

• Projects by types• Servers, clients, converters, generators, parsers, validators…

© 2015 International Business Machines Corporation 8

Page 9: A Tour of Swagger for APIs

• A framework that helps to produce and consume APIs and helps visualize.

• A specification that helps describe and document them.

© 2015 International Business Machines Corporation 9

A technology and a methodology

Page 10: A Tour of Swagger for APIs

How do you start?

© 2015 International Business Machines Corporation 10

Page 11: A Tour of Swagger for APIs

© 2015 International Business Machines Corporation 11

Decide how to create and deliver

• Type of output• Online and hosted: Swagger UI (a JSON object) • File-based or offline: (HTML, markdown, etc.)

• Method to create Swagger JSON• Generate from code (annotations)• Manually: write JSON by hand• Design and build: Swagger Node

• When to generate from code• Build regularly• Create at run time

Page 12: A Tour of Swagger for APIs

© 2015 International Business Machines Corporation 12

Type of output• Online:

• Swagger UI runs in a web browser. • Not set up for printing or viewing offline

• Static: HTML and other static outputs:• Swagger codegen

• Generate the Swagger JSON and a simple HTML file• bootprint-swagger project

• Swagger to HTML• Swagger2Markup project

• Designed to integrate Swagger generated output with other API documentation

Page 13: A Tour of Swagger for APIs

© 2015 International Business Machines Corporation 13

Decide how to create and deliver

• Type of output• Online and hosted: Swagger UI (a JSON object) • File-based or offline: (HTML, markdown, etc.)

• Method to create Swagger JSON• Generate from code (annotations)• Manually: write JSON by hand• Design and build: Swagger Node

• When to generate from code• Build regularly• Create at run time

Page 14: A Tour of Swagger for APIs

© 2015 International Business Machines Corporation 14

Write Swagger-compliant JSON

Page 15: A Tour of Swagger for APIs

© 2015 International Business Machines Corporation 15

Manually create with Swagger Editor

Page 16: A Tour of Swagger for APIs

© 2015 International Business Machines Corporation 16

Generate from annotations

Page 17: A Tour of Swagger for APIs

© 2015 International Business Machines Corporation 17

Add Swagger to an existing API

Find out what Swagger gives you for free: Annotate only your methods:

@Api() and @ApiOperation annotations:

Page 18: A Tour of Swagger for APIs

© 2015 International Business Machines Corporation 18

Hand-crafted JSON vs. annotations

• Hand-craft: For when you don’t have access to the code• Annotations: Easier to maintain. More in sync. Developers

can own or share

• Characteristics:• Delivered online through the Swagger UI or converted to static• Generated from annotations or lovingly hand-crafted

• Potential issues with hand crafting• "Oh the pain" of writing JSON• Docs in sync only as of last writing

Page 19: A Tour of Swagger for APIs

© 2015 International Business Machines Corporation 19

Decide how to create and deliver

• Type of output• Online and hosted: Swagger UI (a JSON object) • File-based or offline: (HTML, markdown, etc.)

• Method to create Swagger JSON• Generate from code (annotations)• Manually: write JSON by hand• Design and build: Swagger Node

• When to generate from code• Build regularly• Create at run time

Page 20: A Tour of Swagger for APIs

© 2015 International Business Machines Corporation 20

Regularly built JSONFor when you are not ready to generate at run time.

•Characteristics:• Delivered online through the Swagger UI or converted to static• Generated from annotations

•Potential issues• Docs in sync only as of last writing

Page 21: A Tour of Swagger for APIs

© 2015 International Business Machines Corporation 21

JSON at run timeDocumentation is always in sync with the code

•Characteristics:• Delivered online through the Swagger UI• Created from annotations• Generated at run time

•Potential issues• Can be technically challenging

Page 22: A Tour of Swagger for APIs

Survey of tools and components

© 2015 International Business Machines Corporation 22

Page 23: A Tour of Swagger for APIs

• JSON object• Can use YAML to

construct it

• Version 2 is current• Swagger UI is backward

compatible with v1.2

© 2015 International Business Machines Corporation 23

Swagger-compliant file

Page 24: A Tour of Swagger for APIs

© 2015 International Business Machines Corporation 24

Swagger UI

Can run locally and display hosted APIs

Page 25: A Tour of Swagger for APIs

© 2015 International Business Machines Corporation 25

Swagger Editor

Can view a swagger file without the Swagger UI

Page 26: A Tour of Swagger for APIs

© 2015 International Business Machines Corporation 26

Swagger-core annotations

Output is swagger-compliant

Page 27: A Tour of Swagger for APIs

© 2015 International Business Machines Corporation 27

Swagger-tools

Showing validation from the CLI

Page 28: A Tour of Swagger for APIs

© 2015 International Business Machines Corporation 28

Swagger.ed

A visualization tool

Page 29: A Tour of Swagger for APIs

© 2015 International Business Machines Corporation 29

Swagger-ed

Page 30: A Tour of Swagger for APIs

Is Swagger for you?

© 2015 International Business Machines Corporation 30

Page 31: A Tour of Swagger for APIs

• Can generate reference docs from existing APIs• Useful in visualizing and testing your API• Easy to share• An active community (questions get answered, bugs get

addressed, and features get added)• Works with a lot of languages, platforms, frameworks• Open source. Licensed under Apache• It has an API explorer

© 2015 International Business Machines Corporation 31

Swagger is a good choice because…

Page 32: A Tour of Swagger for APIs

• Your APIs are not good RESTful citizens• Swagger works best with REST

• Your APIs are complex and Swagger might not be "expressive" enough

• You don't like the look of the Swagger UI• Difficult to customize

• It has an API explorer

© 2015 International Business Machines Corporation 32

Swagger is not for you because…

Page 33: A Tour of Swagger for APIs

Where to find out more

Delivered by Swagger• Swagger website: http://swagger.io/

• Swagger demo: http://petstore.swagger.io/

• Swagger Editor: http://editor.swagger.io/

• Swagger Google Group: https://groups.google.com/forum/#!forum/swagger-swaggersocket

• GitHub: https://github.com/swagger-api

From others• Api Evangelist: Swagger:

http://swagger.apievangelist.com/

• Swagger.ed: http://chefarchitect.github.io/apispots/swaggered/

• API Description Languages: Which is the Right One for Me? http://www.slideshare.net/SOA_Software/api-description-languages-which-is-the-right-one-for-me

© 2015 International Business Machines Corporation #stc15 33

Page 34: A Tour of Swagger for APIs

Questions

© 2015 International Business Machines Corporation 34