Slide 1

Slide 1 text

Fast, testable and SANE APIs Ben Lovell

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

✈️

Slide 4

Slide 4 text

✈️

Slide 5

Slide 5 text

✈️

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

_benlovell t

Slide 11

Slide 11 text

benlovell

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

113581334398839860922

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

“I HAVE KILLED, AND I WILL KILL AGAIN”

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

Building JSON APIs awesome!

Slide 29

Slide 29 text

Rails-API

Slide 30

Slide 30 text

JSON API BIKESHEDDING

Slide 31

Slide 31 text

ActiveModel::Serializers because #to_json kinda sucks

Slide 32

Slide 32 text

HTTP it’s all about the semantics

Slide 33

Slide 33 text

/etc too meta to categorise

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

Components of good API design

Slide 36

Slide 36 text

FAST STANDARDISED INTUITIVE

Slide 37

Slide 37 text

FAST STANDARDISED INTUITIVE

Slide 38

Slide 38 text

FAST STANDARDISED INTUITIVE

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

JSON API

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

application/vnd.api+json IANA registered

Slide 44

Slide 44 text

W IP! http://git.io/Uh8Y8w

Slide 45

Slide 45 text

{ "links": { "posts.author": { "href": "http://example.com/people/{posts.author}", "type": "people" }, "posts.comments": { "href": "http://example.com/comments/{posts.comments}", "type": "comments" } }, "posts": [{ "id": "1", "title": "Rails is Omakase", "links": { "author": "9", "comments": [ "5", "12", "17", "20" ] } }] }

Slide 46

Slide 46 text

{ "posts": [{ "id": "1", "title": "Rails is Omakase", "links": { "author": "9", "comments": [ "5", "12", "17", "20" ] } }] }

Slide 47

Slide 47 text

{ "posts": [{ "id": 1 // a post document }] } { }, { }] } Singular Resource Resource Collection

Slide 48

Slide 48 text

{ }] } { "posts": [{ "id": 1 // a post document }, { "id": 2 // a post document }] } Singular Resource Resource Collection

Slide 49

Slide 49 text

{ "posts": [{ "id": "1", "title": "Rails is Omakase", "links": { "comments": [ "5", "12", "17", "20" ] } }] } To-Many Relationships

Slide 50

Slide 50 text

{ "posts": [{ "id": "1", "title": "Rails is Omakase", "links": { "comments": { "href": "http://example.com/comments/5,12,17", "ids": [ "5", "12", “17" ], "type": "comments" } } }] } To-Many Relationships (link style)

Slide 51

Slide 51 text

{ "links": { "posts.comments": "http://example.com/posts/{posts.id}/comments" }, "posts": [{ "id": "1", "title": "Rails is Omakase" }, { "id": "2", "title": "The Parley Letter" }] } Shorthand (link style) RFC6570 URI Template

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

/etc I did warn you

Slide 56

Slide 56 text

COMPOUND DOCUMENTS the fastest HTTP is no HTTP

Slide 57

Slide 57 text

GET /posts/1?include=comments GET /posts/1?include=comments.author GET /posts/1?include=author,comments

Slide 58

Slide 58 text

{ "links": { "posts.author": { "href": "http://example.com/people/{posts.author}", "type": "people" }, "posts.comments": { "href": "http://example.com/comments/{posts.comments}", "type": "comments" } }, "posts": [{ "id": "1", "title": "Rails is Omakase", "links": { "author": "9", "comments": [ “1" ] }}], "linked": { "authors": [{ "id": "9", "name": "@dhh" }], "comments": [{ "id": "1", "body": "Mmmmmakase" }] } }

Slide 59

Slide 59 text

SPARSE FIELDSETS

Slide 60

Slide 60 text

GET /posts/1?fields=id,title,body

Slide 61

Slide 61 text

HTTP SEMANTICS

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

wat

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

RAILS-API

Slide 68

Slide 68 text

❤️

Slide 69

Slide 69 text

Ruby is not as slow as you might think…

Slide 70

Slide 70 text

Ruby is not as slow as you might think… …IT’S SLOWER

Slide 71

Slide 71 text

So, how do we make Ruby fast?

Slide 72

Slide 72 text

By running less Ruby. DERP! So, how do we make Ruby fast?

Slide 73

Slide 73 text

So, how do we make Rails fast?

Slide 74

Slide 74 text

So, how do we make Rails fast? By running less Rails. DERP!

Slide 75

Slide 75 text

YO DAWG! I heard you like rails so I took some rails out of your rails so you could rails (a little bit faster than usual)

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

http://goo.gl/qfJG2d

Slide 79

Slide 79 text

use Rack::Sendfile use ActionDispatch::Static use Rack::Lock use # use Rack::Runtime use Rack::MethodOverride use ActionDispatch::RequestId use Rails::Rack::Logger use ActionDispatch::ShowExceptions use ActionDispatch::DebugExceptions use ActionDispatch::RemoteIp use ActionDispatch::Reloader use ActionDispatch::Callbacks use ActiveRecord::Migration::CheckPending use ActiveRecord::ConnectionAdapters::ConnectionManagement use ActiveRecord::QueryCache use ActionDispatch::Cookies use ActionDispatch::Session::CookieStore use ActionDispatch::Flash use ActionDispatch::ParamsParser use Rack::Head use Rack::ConditionalGet use Rack::ETag run KitchenSinkFullOfKitchenSinks::Application.routes

Slide 80

Slide 80 text

No content

Slide 81

Slide 81 text

use Rack::Sendfile use ActionDispatch::Static use Rack::Lock use # use Rack::Runtime use Rack::MethodOverride use ActionDispatch::RequestId use Rails::Rack::Logger use ActionDispatch::ShowExceptions use ActionDispatch::DebugExceptions use ActionDispatch::RemoteIp use ActionDispatch::Reloader use ActionDispatch::Callbacks use ActiveRecord::Migration::CheckPending use ActiveRecord::ConnectionAdapters::ConnectionManagement use ActiveRecord::QueryCache use ActionDispatch::Cookies use ActionDispatch::Session::CookieStore use ActionDispatch::Flash use ActionDispatch::ParamsParser use Rack::Head use Rack::ConditionalGet use Rack::ETag run KitchenSink::Application.routes

Slide 82

Slide 82 text

No content

Slide 83

Slide 83 text

require “rails/all”

Slide 84

Slide 84 text

require “rails/all”

Slide 85

Slide 85 text

irb(main):003:0> pp ActionController::API.ancestors - ActionController::Metal.ancestors [ ActionController::API, ActiveRecord::Railties::ControllerRuntime, ActionDispatch::Routing::RouteSet::MountedHelpers, ActionController::StrongParameters, ActionController::Instrumentation, ActionController::Rescue, ActiveSupport::Rescuable, ActionController::DataStreaming, ActionController::ForceSSL, AbstractController::Callbacks, ActiveSupport::Callbacks, ActionController::ConditionalGet, ActionController::Head, ActionController::Renderers::All, ActionController::Renderers, ActionController::Rendering, AbstractController::Rendering, AbstractController::ViewPaths, ActionController::Redirecting, ActionController::RackDelegation, ActiveSupport::Benchmarkable, AbstractController::Logger, ActionController::UrlFor, AbstractController::UrlFor, ActionDispatch::Routing::UrlFor, ActionDispatch::Routing::PolymorphicRoutes, ActionController::ModelNaming, ActionController::HideActions ]

Slide 86

Slide 86 text

No content

Slide 87

Slide 87 text

Y U NO Sinatra!!!

Slide 88

Slide 88 text

No content

Slide 89

Slide 89 text

No content

Slide 90

Slide 90 text

Y U NO Sinatra!!! “Any sufficiently advanced sinatra application is indistinguishable from rails”

Slide 91

Slide 91 text

No content

Slide 92

Slide 92 text

don’t reinvent this

Slide 93

Slide 93 text

ActiveModel Serializers

Slide 94

Slide 94 text

Jbuilder.encode do |json| json.content format_content(@message.content) json.(@message, :created_at, :updated_at) json.author do json.name @message.creator.name.familiar json.email_address @message.creator.email_address_with_name json.url url_for(@message.creator, format: :json) end if current_user.admin? json.visitors calculate_visitors(@message) end json.comments @message.comments, :content, :created_at json.attachments @message.attachments do |attachment| json.filename attachment.filename json.url url_for(attachment) end end

Slide 95

Slide 95 text

No content

Slide 96

Slide 96 text

DHH CHANGES NAME TO BASECAMP

Slide 97

Slide 97 text

Jbuilder json json json json json json end if json end json json json json end end

Slide 98

Slide 98 text

% rails g serializer …

Slide 99

Slide 99 text

class PostSerializer < ActiveModel::Serializer attributes :id, :title, :body has_many :comments end

Slide 100

Slide 100 text

class PostSerializer < ActiveModel::Serializer attributes :id, :title, :body, :synopsis has_many :comments def comments object.comments.where(:author => scope) end def synopsis object.body.truncate(30) end end

Slide 101

Slide 101 text

class PostsController < ApplicationController def show @post = Post.find(params[:id]) respond_with @post end end

Slide 102

Slide 102 text

CONVENTION!

Slide 103

Slide 103 text

VIEWS

Slide 104

Slide 104 text

class PostSerializer < ActiveModel::Serializer attributes :id, :title, :body has_many :comments has_many :tags has_many :images belongs_to :author end WARNING!

Slide 105

Slide 105 text

class attributes has_many has_many has_many belongs_to end CONGRATULATIONS!!! YOU SERIALIZED YOUR DATABASE

Slide 106

Slide 106 text

CONDITIONAL REQUESTS

Slide 107

Slide 107 text

$ curl -i https://api.example.com/user HTTP/1.1 200 OK Cache-Control: private, max-age=60 ETag: "644b5b0155e6404a9cc4bd9d8b1ae730" Last-Modified: Thu, 02 Feb 2014 15:31:30 GMT Status: 200 OK

Slide 108

Slide 108 text

$ curl -i https://api.example.com/user -H ‘If-None-Match: ”{Etag}”’ HTTP/1.1 304 Not Modified Cache-Control: private, max-age=60 ETag: "644b5b0155e6404a9cc4bd9d8b1ae730" Last-Modified: Thu, 02 Feb 2014 15:31:30 GMT Status: 304 Not Modified

Slide 109

Slide 109 text

class PostsController < ApplicationController def show @post = Post.find(params[:id]) if stale? @post respond_with @post end end end

Slide 110

Slide 110 text

class PostsController < ApplicationController def show @post = Post.find(params[:id]) if stale? @post respond_with @post end end end etag: Model#cache_key last_modified: Model#updated_at

Slide 111

Slide 111 text

class PostSerializer < ActiveModel::Serializer cached delegate :cache_key, :to => :object end

Slide 112

Slide 112 text

ActiveModel Serializers CACHE

Slide 113

Slide 113 text

GATEWAY CACHING

Slide 114

Slide 114 text

TESTABLE?

Slide 115

Slide 115 text

rack-test

Slide 116

Slide 116 text

No content

Slide 117

Slide 117 text

No content

Slide 118

Slide 118 text

JSON-API RAILS-API AMS = ❤️

Slide 119

Slide 119 text

JSON-API RAILS-API AMS =

Slide 120

Slide 120 text

THE REST…?

Slide 121

Slide 121 text

THANKS! @benlovell