Upgrade to Pro — share decks privately, control downloads, hide ads and more …

HTTP & Stuff

HTTP & Stuff

Slides from my talk on HTTP APIs given at the joint MadRailers/MadJS meetup.

Brian Cobb

March 31, 2014
Tweet

Other Decks in Programming

Transcript

  1. API methods have certain characteristics (waves hands) Easier to develop

    against an API when these are explicit Can either make them explicit in the transport or in the messages Premise
  2. require 'sinatra' ! get '/' do [ 200, # status

    {}, # headers '' # body ] end
  3. require 'sinatra' ! get '/' do [ 200, # status

    {}, # headers '' # body ] end
  4. { "items": [{ "0": 1, "1": "apache2", "2": "2014-02-23T13:23:47Z" },

    { "0": 2, "1": "apt", "2": "2013-10-10T17:01:03Z" }] }
  5. { "cookbooks": [{ "id": 1, "name": "apache2", "last_modified": "2014-02-23T13:23:47Z" },

    { "id": 2, "name": "apt", "last_modified": "2013-10-10T17:01:03Z" }] }
  6. { "cookbooks": [{ "id": 1, "name": "apache2", "last_modified": "2014-02-23T13:23:47Z", "_links":

    { "unshare": "/cookbooks/apache2", "self": "/cookbooks/apache2" } }, { "id": 2, "name": "apt", "last_modified": "2013-10-10T17:01:03Z", "_links": { "self": "/cookbooks/apt" } }], "_links": { "share": "/cookbooks", "self": "/cookbooks", "search": "/cookbooks/search{?q,items,start}" } }
  7. Supermarket::Application.routes.draw do namespace :api, defaults: { format: :json } do

    namespace :v1 do get 'cookbooks' => 'cookbooks#index' get 'search' => 'cookbooks#search' get 'cookbooks/:cookbook' => 'cookbooks#show', as: :cookbook get 'cookbooks/:cookbook/versions/:version' => 'cookbook_versions#show', as: :cookbook_version post 'cookbooks' => 'cookbook_uploads#create' delete 'cookbooks/:cookbook' => 'cookbooks#destroy' end end end
  8. diff --git a/Gemfile b/Gemfile index 03c243b..cde0a05 100644 --- a/Gemfile +++

    b/Gemfile @@ -17,6 +17,7 @@ gem 'paperclip' gem 'virtus', require: false gem 'kaminari' +gem 'api-versions' gem 'sentry-raven', github: 'getsentry/raven-ruby' gem 'statsd-ruby', require: 'statsd'
  9. Supermarket::Application.routes.draw do api vendor_string: 'supermarket', default_version: 1 do version 1

    do scope 'v1' do # routes for application/vnd.supermarket+json;version=1 end end end ! namespace :api, defaults: { format: :json } do namespace :v1 do # existing routes end end end
  10. json.start @start json.total @total json.items @cookbooks do |cookbook| json.cookbook_name cookbook.name

    json.cookbook_maintainer cookbook.maintainer json.cookbook_description cookbook.description json.cookbook api_v1_cookbook_url(cookbook) end https://github.com/rails/jbuilder
  11. https://github.com/apotonick/roar class CookbookRepresenter < Roar::Decorator include Roar::Representer::JSON ! property :name

    property :description ! link :self do api_v1_cookbook_url(represented) end ! collection :cookbook_versions, class: CookbookVersion do property :version property :license ! link :self do api_v1_cookbook_url(represented.cookbook, represented) end ! link :artifact do represented.tarball.url end end end
  12. require 'oat/adapters/json_api' ! class CookbookSerializer < Oat::Serializer adapter Oat::Adapters::JsonAPI !

    schema do type "cookbook" link :self, href: api_v1_cookbook_url(item) ! properties do |props| map_properties :name, :description ! entities :cookbook_versions, item.cookbook_versions, CookbookVersionSerializer end end end https://github.com/ismasan/oat