Slide 1

Slide 1 text

API Contracts using Open API Billie Thompson @PurpleBooth https://github.com/PurpleBooth/book-store

Slide 2

Slide 2 text

Contents 1. Why are some APIs terrible? 2. API Contracts: Your Options 3. How to write OpenAPI Specifications 4. A Practical Example 5. Conclusion

Slide 3

Slide 3 text

Why do some APIs terrible? Even though they don't have downtime, or bugs

Slide 4

Slide 4 text

Conway's Law

Slide 5

Slide 5 text

"organizations which design systems ... are constrained to produce designs which are copies of the communication structures of these organizations" — M. Conway

Slide 6

Slide 6 text

"If you have four groups working on a compiler, you'll get a 4-pass compiler" — Eric S Raymond

Slide 7

Slide 7 text

Changing communication between stakeholders

Slide 8

Slide 8 text

Sit in a room with the stake holders And write down the results (In a computer readable document)

Slide 9

Slide 9 text

The Why! » Learn about requirements » Share Expectations » Humanising teams creates respect » Living Documentation

Slide 10

Slide 10 text

A Contract Options for defining one

Slide 11

Slide 11 text

Things to consider (It's got to do these in PHP) » Generate Clients » Validate Standard » Documentation Generation » Pleasant Editor

Slide 12

Slide 12 text

Your Options » RAML » API Blueprint » OpenAPI/Swagger (JSON Schema + Operations)

Slide 13

Slide 13 text

RAML

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

RAML » Generate Clients: Yes, roughly 1 choice per language » Validate Standard: No (not in PHP anyway) » Documentation Generation: Yes, loads » Pleasant Editor: Yes Note: Weird split between version 0.8, 1.0.

Slide 16

Slide 16 text

API Blueprint

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

API Blueprint » Generate Clients: Yes, roughly 2 generators (poor generally) » Validate Standard: Yes » Documentation Generation: Yes, 3 or 4 things » Pleasant Editor: Yes Note Blueprint was sponsored by Apiary, who have now thrown themselves behind OpenAPI Specification

Slide 19

Slide 19 text

OpenAPI Specification

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

OpenAPI Specification » Generate Clients: Yes, lots for PHP and other languages » Validate Standard: Yes » Documentation Generation: Yes, loads » Pleasant Editor: Yes, lots

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

Some History » ~2011 - Swagger Released based on JSON Schema » ~2014 - v2 Released » January 1st 2016 - Swagger becomes OpenAPI Specification

Slide 24

Slide 24 text

Swagger is awesome but: » Don't use swaggers YAML format. » Inheritance Doesn't work well » Nulls aren't well supported (It's still pretty awesome though)

Slide 25

Slide 25 text

How to write Open API Specifications

Slide 26

Slide 26 text

{ "swagger": "2.0", "info": { }, "paths": { }, "definitions": { } }

Slide 27

Slide 27 text

{ "swagger": "2.0", "info": { "version": "1.0.0", "title": "Swagger Petstore", "description": "A sample API that uses a petstore as an example", "termsOfService": "http://swagger.io/terms/", "contact": { "name": "Swagger API Team" } }, "consumes": [ "application/json" ], "produces": [ "application/json" ],

Slide 28

Slide 28 text

{"paths": { "/pets": { "get": { "description": "Returns all pets from the system that the user has access to", "produces": [ "application/json" ], "responses": { "200": { "description": "A list of pets.", "schema": { "type": "array", "items": { "$ref": "#/definitions/Pet" } } } } } } },

Slide 29

Slide 29 text

{"definitions": { "Pet": { "type": "object", "required": [ "id", "name" ], "properties": { "id": { "type": "integer", "format": "int64" }, "name": { "type": "string" }, "tag": { "type": "string" } } } }

Slide 30

Slide 30 text

A Practical Example Mandatory Symfony Example

Slide 31

Slide 31 text

Demo Checklist » The swagger file for this Example » Generating Clients » The Implementation » Validating the Spec » The Documentation UI

Slide 32

Slide 32 text

The tooling rocks!

Slide 33

Slide 33 text

What to take away

Slide 34

Slide 34 text

Questions? @PurpleBooth » https://github.com/PurpleBooth/book-store » http://apievangelist.com/2016/09/16/a-look-at-the-state-of-api- documentation-solutions/ » https://github.com/janephp/openapi » https://github.com/fitbug/guzzle-swagger-validation-middleware » https://github.com/OAI/OpenAPI-Specification » http://swagger.io/swagger-ui/ » http://editor.swagger.io/