api documentation languages and contract first design

35
API Languages & Contract-First Design John Zaccone - Software Engineer http://www.ipponusa.com

Upload: john-zaccone

Post on 20-Mar-2017

747 views

Category:

Engineering


1 download

TRANSCRIPT

Page 1: API Documentation Languages and Contract First Design

API Languages & Contract-First Design

John Zaccone - Software Engineerhttp://www.ipponusa.com

Page 2: API Documentation Languages and Contract First Design

Overview

● API Documentation Languages

● Demo

● Contract-First Design

● Refactor the Demo

● Cool Tools

2

Page 3: API Documentation Languages and Contract First Design

Let’s REST together

3

Page 4: API Documentation Languages and Contract First Design

We need Documentation for our REST APIs

● API Doc Languages define a specification

● Specifications are Machine-Readable

● Parsers and tools that understand the specification can provide great features for little work

4

Page 5: API Documentation Languages and Contract First Design

Value Added

● API Doc specifications are open-source

● Value comes from suite of tools and parsers build on that specification

● Interactive documentation, code generation, postman collections, mock servers, traffic inspection, API testing and analytics and much, much more

5

Page 6: API Documentation Languages and Contract First Design

Bottom Up or Top Down

● Top-down (aka contract-first)

○ Write doc first

○ Generate code from docs

● Bottom-up

○ Write code first

○ Generate docs from code

6

Page 7: API Documentation Languages and Contract First Design

Comparing What’s Out There

Swagger RAML API Blueprint

Sponsored By Reverb Mulesoft Apiary

Initial Commit July, 2011 Sep, 2013 April, 2013

Designed For Code First Contract First Contract First

Workgroup Yes Yes No

Format JSON w/ YAML YAML Markdown

Community 2,386 942 1,614

7

Page 8: API Documentation Languages and Contract First Design

Modeling REST

They all support:

● Resources● HTTP Methods● Query Parameters● URL Parameters● Header Parameters● Status Codes, Content-Types

8

Page 9: API Documentation Languages and Contract First Design

Modeling REST

Swagger RAML API Blueprint

Authentication Basic, API-Key, OAuth2

Basic, Digest, OAuth1, OAuth2

Basic, API-Key, OAuth2

File Inclusions Yes, $ref Yes

Nested Resources Yes Yes

API Versioning Yes Yes

Sample Representations

Yes Yes

9

Page 10: API Documentation Languages and Contract First Design

Integrations ⭑

Swagger RAML APIBlueprint

Server-Side java, scala, ruby, nodejs

java

Client-Side scala, flash, java, objc, php, python,

ruby

js

Parsers js, java js, java c++, .net, ruby

10

Page 11: API Documentation Languages and Contract First Design

Overview

● API Documentation Languages

● Demo

● Contract-First Design

● Refactor the Demo

● Cool Tools

11

Page 12: API Documentation Languages and Contract First Design

Pet Store API

● Get all Pets

● Add a Pet

● Get Pet by ID

● Paging and Security

12

Page 13: API Documentation Languages and Contract First Design

A History

● Swagger starts in July 2011, provides API documentation automatically from code

● Push for “contract-first” design

● RAML (Sept, 2013) found that Swagger “was best suited for documenting an existing API, not for designing an API from scratch”

● Swagger 2.0 addresses contract-first design weaknesses (~Sept, 2014)

13

Page 14: API Documentation Languages and Contract First Design

Overview

● API Documentation Languages

● Demo

● Contract-First Design

● Refactor the Demo

● Cool Tools

14

Page 15: API Documentation Languages and Contract First Design

Contracts Are Valuable

● Enable 3rd-party developers to build awesome apps

● Channel of revenue

● Drive innovation by sharing internal APIs

● APIs are enablers

15

Page 16: API Documentation Languages and Contract First Design

16

Page 17: API Documentation Languages and Contract First Design

Problems

● Doesn’t consider needs of consumers

● Not-so-nice APIs

○ Complicated models

○ Unfriendly business logic

● Consumers might have a different view of the world

17

Page 18: API Documentation Languages and Contract First Design

18

Page 19: API Documentation Languages and Contract First Design

API Facade Pattern

● Hide the complexity

● Define an API that users want to use

● Simple models

● Consistent interfaces

19

Page 20: API Documentation Languages and Contract First Design

Agile in API Design

20

Page 21: API Documentation Languages and Contract First Design

Contract-First

Swagger RAML API Blueprint

Language Writability

Good Good Good

Re-usable Patterns

Good Great OK

Mock Server 3rd Party Yes Yes, External Tool

Collaboration Platform

Open-Source Solution?

Enterprise Offering Enterprise Offering

21

Page 22: API Documentation Languages and Contract First Design

Common Patterns in REST

● Many patterns of REST are repeated over and over...

● Get /my_collection

● Post /my_collection

● Filtering, ordering and paging

22

Page 23: API Documentation Languages and Contract First Design

Patterns with RAML

● ResourceTypes define common behaviors for resources

● Traits define common behaviors for methods

● Example: /petstype: collection

23

Page 24: API Documentation Languages and Contract First Design

Overview

● API Documentation Languages

● Demo

● Contract-First Design

● Refactor the Demo

● Cool Tools

24

Page 25: API Documentation Languages and Contract First Design

Take Away

● Swagger wins

○ Community support

○ Extensive client-side and server-side integrations

● RAML wins

○ Advanced re-use patterns for resources and methods

○ Supports multiple files, and enterprise solution for collaboration

○ Great for designing APIs from scratch

25

Page 26: API Documentation Languages and Contract First Design

Keep an Eye Out...

● Community growth in RAML

● Better ways to write APIs in Swagger

● @swagger, @mulesoft

● swagger2raml, raml2swagger

26

Page 27: API Documentation Languages and Contract First Design

Overview

● API Documentation Languages

● Demo

● Contract-First Design

● Refactor the Demo

● Cool Tools

27

Page 28: API Documentation Languages and Contract First Design

Client SDK Generator

● https://github.com/swagger-api/swagger-codegen

● Supports a growing list of languages

28

Page 29: API Documentation Languages and Contract First Design

RAML API Designer with MongoDB

● https://github.com/brianmc/raml-store

29

Page 30: API Documentation Languages and Contract First Design

Enterprise Solutions

● RAML (https://anypoint.mulesoft.com)

● API Blueprint (http://apiary.io/)

30

Page 31: API Documentation Languages and Contract First Design

Verify Code == Docs

● Aboa: (https://github.com/cybertk/abao)

● Dredd: (https://github.com/apiaryio/dredd)

● Command-line tool to check if service implementation matches RAML doc

● Integrate into your CI (Jenkins) Build

31

Page 32: API Documentation Languages and Contract First Design

API Mashups

● API Notebook (https://api-notebook.anypoint.mulesoft.com)

● Live javascript

● Quickly create clients from RAML definitions

● Make PoC and prototypes

● Saved as Github Gists

32

Page 33: API Documentation Languages and Contract First Design

Other

● RAML and Swagger imports into your Postman client

● Sublime plugin for API Blueprint and RAML

● raml2html (https://github.com/kevinrenskers/raml2html)

33

Page 34: API Documentation Languages and Contract First Design

Questions

[email protected]

@JohnZaccone

34

Page 35: API Documentation Languages and Contract First Design

References

● https://speakerdeck.com/apistrat/another-api-blueprint-raml-and-swagger-comparison

● https://blog.soa.com/spread-the-wealth-developer-communities-for-your-apis-part-2-top-down-or-bottom-up-structuring-your-community/

● https://www.youtube.com/watch?v=vu8_QLkW1mg

● http://apiux.com/2013/04/09/rest-metadata-formats/

● http://stackoverflow.com/questions/26917188/how-to-break-swagger-2-0-json-file-into-multiple-modules/26917653#26917653

● http://raml.org/spec.html

● http://stackoverflow.com/users/1103327/ron?tab=answers&sort=votes (Swagger Questions)

● http://www.programmableweb.com/news/reverb-apigee-announce-swagger-2.0-workgroup/2014/05/23

● http://forums.raml.org/t/how-is-this-different-from-swagger/24

● https://github.com/swagger-api/swagger-spec/wiki/Swagger-1.2-to-2.0-Migration-Guide

35