Swagger is a description standard of REST API. I will show you features of Swagger UI, and how to make it out with grape and grape-swagger. At the end introduces PostgREST and emphasizes DRY principle.
5. Scenario 場景
Why we adopted Swagger?
Rails application
3 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
15. Implementation 實作
1 Grape basic declaration
2 Namespace and routes
3 ’params’ -> input type
4 Grape::Entity -> output type
5 Doorkeeper -> OAuth authorizations
6 Swagger information
Jiang Wu Swagger 2015-09-12 15 / 42
16. Grape basic declaration
class API < Grape::API
# API routes prefix
prefix 'api'
# API version
version 'v1', using: :path
# load other API
mount Endpoints
end
Jiang Wu Swagger 2015-09-12 16 / 42
17. namespaces 命名空間
namespace "projects" do
mount ProjectsAPI
# namepsaces can be variable
# and cascadable
namespace ":identity" do
mount SingleProjectAPI
end
end
Jiang Wu Swagger 2015-09-12 17 / 42
18. API routes
Sinatra like DSL, declared with HTTP
methods.
desc "Show single project"
# get|post|put|patch|delete
get "/:project" do
# Your implementation
end
Jiang Wu Swagger 2015-09-12 18 / 42
19. Input types 輸⼊類型
params do
requires :name, type: String
optional :public,
type: Boolean,
default: false
end
Validate requests and return 400 status
code for typecast error or missing value.
Jiang Wu Swagger 2015-09-12 19 / 42
23. Swagger information (1)
Delare with
add_swagger_documentation
api_version
mount_path
authorizations
info
Jiang Wu Swagger 2015-09-12 23 / 42
24. Swagger informations (2)
Add documentaions of
namespace
params
output types (options of ’desc’)
OAuth scopes (options of ’desc’)
API codes (options of ’get’/’post’)
Jiang Wu Swagger 2015-09-12 24 / 42
25. Live Test
Similiar 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
26. API test before Swagger
Write it by yourself
Browser plugins (not quite works)
RestClient Firefox + Chrome
Proprietary softwares
Postman Chrome + NodeJS
Paw Mac
Jiang Wu Swagger 2015-09-12 26 / 42
27. Short summary of Swagger
API routes
Input types
Output types
Authorizations
Jiang Wu Swagger 2015-09-12 27 / 42
28. Ruby Swagger clients
REST clients are not Swagger specific
I started my work 3 days ago
Will be available soon
Virtus gem for type check
Meta programming DSL
Jiang Wu Swagger 2015-09-12 28 / 42
29. Unleash power of database
API Database
Routes Tables/Views
Input types Constraints, Types
Output types Table columns
Authorizations Roles
Jiang Wu Swagger 2015-09-12 29 / 42
31. Design philisophy
Can we use the declarative
information in a relational db
schema to mechanically generate
an HTTP API?
begriffs
Jiang Wu Swagger 2015-09-12 31 / 42
32. Features of PostgreSQL
Data types Array, HStore, GIS,
JSONB(since 9.4)
Procedure languages PL/pgSQL,
PL/Python, PL/V8
Message queue NOTIFY/LISTEN
Full text search tsvector/tsquery
Jiang Wu Swagger 2015-09-12 32 / 42
33. Schema -> API
Schema path -> API version
Tables/views -> API routes
Where clause -> query params
GET
/projects?age=lt.P7D&public=eq.true
Query projects created in 7 days and
public.
Jiang Wu Swagger 2015-09-12 33 / 42
35. DML -> HTTP methods
insert POST
update PATCH
upsert/merge PUT
delete DELETE
Jiang Wu Swagger 2015-09-12 35 / 42
36. DB Roles -> Authorizations
Need extra works to support normal
database-based authentication and
authorizations.
Jiang Wu Swagger 2015-09-12 36 / 42
37. Why PostgREST?
Counter attack to NoSQL
Bare metal speed (written in Haskell)
HTTP protocol
Jiang Wu Swagger 2015-09-12 37 / 42
39. Swagger
Describe REST services
Provide Live testable documentation
with Swagger UI
Can generate with grape-swagger
Disadvantage: have to investigate
across components when debugging
Ruby client: under work
Jiang Wu Swagger 2015-09-12 39 / 42
40. PostgREST
DB Schema -> API
Rediscover values of RDBMS
Advantage: can utilize more mature
tools built around RDBMS
Jiang Wu Swagger 2015-09-12 40 / 42