42
Swagger Implement Web API documents Jiang Wu 2015-09-12 Jiang Wu Swagger 2015-09-12 1 / 42

Implement Web API with Swagger

Embed Size (px)

Citation preview

Page 1: Implement Web API with Swagger

SwaggerImplement Web API documents

Jiang Wu

2015-09-12

Jiang Wu Swagger 2015-09-12 1 / 42

Page 2: Implement Web API with Swagger

Context

1 Introduction

2 Swagger

3 PostgREST

4 Summary

Jiang Wu Swagger 2015-09-12 2 / 42

Page 3: Implement Web API with Swagger

Do you remember this?

Jiang Wu Swagger 2015-09-12 3 / 42

Page 4: Implement Web API with Swagger

本尊在此 It’s me!

Jiang Wu Swagger 2015-09-12 4 / 42

Page 5: Implement Web API with Swagger

Scenario 場景Why we adopted Swagger?

Rails application3 types of API consumers

▶ Native application(iOS/Android/PC/Mac)

▶ Third party service▶ Javascript: single page application

Jiang Wu Swagger 2015-09-12 5 / 42

Page 6: Implement Web API with Swagger

Constraints 約束

HTTP/1.1JSON data formatREST architecture style

Jiang Wu Swagger 2015-09-12 6 / 42

Page 7: Implement Web API with Swagger

Ruby Gems

grape REST servicesdoorkeeper OAuth providerThanks yorkxin’s blog for the help.

Jiang Wu Swagger 2015-09-12 7 / 42

Page 8: Implement Web API with Swagger

API documentations

Must be important, clear and accurate.重要 跨專案組,跨公司清楚 減少交流成本正確 和程式碼保持同步

Jiang Wu Swagger 2015-09-12 8 / 42

Page 9: Implement Web API with Swagger

程式設計師痛恨兩件事情寫⽂檔別⼈不寫⽂檔

Coders hate 2 things: writingdocumentation and no documentation.Solution’D’o not ’R’epeat ’Y’ourself Priciple

Jiang Wu Swagger 2015-09-12 9 / 42

Page 10: Implement Web API with Swagger

Remove duplication

documentation -> code WSDLcode -> documentation RDoc, YARD,

Rocco(annotated source code)literate programming noweb, Org Mode,

Literate Haskell

Jiang Wu Swagger 2015-09-12 10 / 42

Page 11: Implement Web API with Swagger

Context

1 Introduction

2 Swagger

3 PostgREST

4 Summary

Jiang Wu Swagger 2015-09-12 11 / 42

Page 12: Implement Web API with Swagger

Demo

https://api.gitcafe.com/apidoc/

Jiang Wu Swagger 2015-09-12 12 / 42

Page 13: Implement Web API with Swagger

DefinitionsSwaggerDescribe REST servicesSwagger UILive testable documentationgrape-swaggerGenerate API description

Jiang Wu Swagger 2015-09-12 13 / 42

Page 14: Implement Web API with Swagger

Describe REST services

API routesInput typesOutput typesAuthorizations

Jiang Wu Swagger 2015-09-12 14 / 42

Page 15: Implement Web API with Swagger

Implementation 實作1 Grape basic declaration2 Namespace and routes3 ’params’ -> input type4 Grape::Entity -> output type5 Doorkeeper -> OAuth authorizations6 Swagger information

Jiang Wu Swagger 2015-09-12 15 / 42

Page 16: Implement Web API with Swagger

Grape basic declarationclass API < Grape::API

# API routes prefixprefix 'api'# API versionversion 'v1', using: :path# load other APImount Endpoints

endJiang Wu Swagger 2015-09-12 16 / 42

Page 17: Implement Web API with Swagger

namespaces 命名空間namespace "projects" do

mount ProjectsAPI# namepsaces can be variable# and cascadablenamespace ":identity" do

mount SingleProjectAPIend

endJiang Wu Swagger 2015-09-12 17 / 42

Page 18: Implement Web API with Swagger

API routesSinatra like DSL, declared with HTTPmethods.desc "Show single project"# get|post|put|patch|deleteget "/:project" do

# Your implementationend

Jiang Wu Swagger 2015-09-12 18 / 42

Page 19: Implement Web API with Swagger

Input types 輸⼊類型params do

requires :name, type: Stringoptional :public,

type: Boolean,default: false

endValidate requests and return 400 statuscode for typecast error or missing value.

Jiang Wu Swagger 2015-09-12 19 / 42

Page 20: Implement Web API with Swagger

Output types 輸出類型class Project < Grape::Entity

# Output fieldsexpose :name,

documentation: {type: 'string',desc: 'Project name'

}end

Jiang Wu Swagger 2015-09-12 20 / 42

Page 21: Implement Web API with Swagger

Refer to other typesclass Project < Grape::Entity

expose :owner,# with 'using'

using: Account,documentation: {

type: 'Account'}

endJiang Wu Swagger 2015-09-12 21 / 42

Page 22: Implement Web API with Swagger

OAuth authorizations

Rails + doorkeeperOAuth admin pageOAuth grant flow

Grape + doorkeeperHandle OAuth token

Jiang Wu Swagger 2015-09-12 22 / 42

Page 23: Implement Web API with Swagger

Swagger information (1)Delare withadd_swagger_documentation

api_versionmount_pathauthorizationsinfo

Jiang Wu Swagger 2015-09-12 23 / 42

Page 24: Implement Web API with Swagger

Swagger informations (2)Add documentaions of

namespaceparamsoutput types (options of ’desc’)OAuth scopes (options of ’desc’)API codes (options of ’get’/’post’)

Jiang Wu Swagger 2015-09-12 24 / 42

Page 25: Implement Web API with Swagger

Live TestSimiliar as doctest of Python""" input and output, REPL way>>> factorial(5)120"""def factorial(n):Documentation is both sample and test.

Jiang Wu Swagger 2015-09-12 25 / 42

Page 26: Implement Web API with Swagger

API test before Swagger

Write it by yourselfBrowser plugins (not quite works)RestClient Firefox + ChromeProprietary softwaresPostman Chrome + NodeJS

Paw Mac

Jiang Wu Swagger 2015-09-12 26 / 42

Page 27: Implement Web API with Swagger

Short summary of Swagger

API routesInput typesOutput typesAuthorizations

Jiang Wu Swagger 2015-09-12 27 / 42

Page 28: Implement Web API with Swagger

Ruby Swagger clients

REST clients are not Swagger specificI started my work 3 days agoWill be available soonVirtus gem for type checkMeta programming DSL

Jiang Wu Swagger 2015-09-12 28 / 42

Page 29: Implement Web API with Swagger

Unleash power of database

API DatabaseRoutes Tables/ViewsInput types Constraints, TypesOutput types Table columnsAuthorizations Roles

Jiang Wu Swagger 2015-09-12 29 / 42

Page 30: Implement Web API with Swagger

Context

1 Introduction

2 Swagger

3 PostgREST

4 Summary

Jiang Wu Swagger 2015-09-12 30 / 42

Page 31: Implement Web API with Swagger

Design philisophy

Can we use the declarativeinformation in a relational dbschema to mechanically generatean HTTP API?

begriffs

Jiang Wu Swagger 2015-09-12 31 / 42

Page 32: Implement Web API with Swagger

Features of PostgreSQL

Data types Array, HStore, GIS,JSONB(since 9.4)

Procedure languages PL/pgSQL,PL/Python, PL/V8

Message queue NOTIFY/LISTENFull text search tsvector/tsquery

Jiang Wu Swagger 2015-09-12 32 / 42

Page 33: Implement Web API with Swagger

Schema -> APISchema path -> API versionTables/views -> API routesWhere clause -> query params

GET/projects?age=lt.P7D&public=eq.trueQuery projects created in 7 days andpublic.

Jiang Wu Swagger 2015-09-12 33 / 42

Page 34: Implement Web API with Swagger

Pagination -> Range headersGET /items HTTP/1.1Range-Unit: itemsRange: 0-24

HTTP/1.1 206 Partial ContentAccept-Ranges: itemsContent-Range: 0-24/100Range-Unit: items

Jiang Wu Swagger 2015-09-12 34 / 42

Page 35: Implement Web API with Swagger

DML -> HTTP methods

insert POSTupdate PATCHupsert/merge PUTdelete DELETE

Jiang Wu Swagger 2015-09-12 35 / 42

Page 36: Implement Web API with Swagger

DB Roles -> Authorizations

Need extra works to support normaldatabase-based authentication andauthorizations.

Jiang Wu Swagger 2015-09-12 36 / 42

Page 37: Implement Web API with Swagger

Why PostgREST?

Counter attack to NoSQLBare metal speed (written in Haskell)HTTP protocol

Jiang Wu Swagger 2015-09-12 37 / 42

Page 38: Implement Web API with Swagger

Context

1 Introduction

2 Swagger

3 PostgREST

4 Summary

Jiang Wu Swagger 2015-09-12 38 / 42

Page 39: Implement Web API with Swagger

SwaggerDescribe REST servicesProvide Live testable documentationwith Swagger UICan generate with grape-swaggerDisadvantage: have to investigateacross components when debuggingRuby client: under work

Jiang Wu Swagger 2015-09-12 39 / 42

Page 40: Implement Web API with Swagger

PostgREST

DB Schema -> APIRediscover values of RDBMSAdvantage: can utilize more maturetools built around RDBMS

Jiang Wu Swagger 2015-09-12 40 / 42

Page 41: Implement Web API with Swagger

Apply DRY principle

Abstract not duplicateSelect proper toolsBring values

Jiang Wu Swagger 2015-09-12 41 / 42

Page 42: Implement Web API with Swagger

Q&A

Questions?Twitter: @masterwujiangGithub: @nouseLinkedin: @nouse

Jiang Wu Swagger 2015-09-12 42 / 42