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

Describing Your World with Seahorse

Describing Your World with Seahorse

So you've just added a suite of RESTful APIs to your web service. Now you need to generate documentation and build Ruby, Python, and JavaScript clients to consume those new APIs. With so many moving parts, how do you keep your service, documentation and clients in sync?

We all know how to describe data using ActiveRecord models. Have you considered using a model to describe your service?
A service model provides a number of benefits including: easy to generate API documentation, consistent server side parameter validation, versioned APIs, easy to build clients, and more. It also represents a unified view of your API which helps to keep your code and documentation DRY. But what does a service model look like? For instance, did you know that your APIs can be described using just four parameter types? What if your could express your APIs using a Rails DSL?

This talk will introduce Seahorse, a DSL for describing API operations for just about any web service. It provides all of the above functionality, allowing you to describe your service model in a single place with Ruby code. We will demonstrate how to use Seahorse to generate clients, model existing real-world APIs, and even build one of our own.

Given at RailsConf 2013.

Trevor Rowe

May 07, 2013
Tweet

Other Decks in Programming

Transcript

  1. “The  shape  of  your  API  should  be   driven  by

     your  Domain  model,  not  your   persistence  model.”   -­‐  Some  guy  at  RailsConf  
  2. Why  You  “Need”  an  API  Model   •  Filter  incoming

     parameters   •  Perform  parameter  type  conversion   •  Generate  comprehensive  API   documenta:on   •  Generate  clients   •  Versioning    
  3. Describing  Your  World  with  Seahorse   •  Seahorse  describes  the

     “shape”  of  a  web   service  API   •  It  does  not  describe  the  protocol   •  Extracted  from  our  work  on  building  AWS   clients  in  mul:ple  languages   –  PHP   –  Python   –  JavaScript   –  Ruby  
  4. Q:  What  does  Rails  provide  to  help   me  build

     API  models?   A:  Not  much  
  5. Whitelisted  AXributes     •  Not  type  aware    

    •  Doesn’t  support  mul:ple  configura:ons   •  Ugly  to  use  with  nested_attrbiutes_for    
  6. •  String   •  Integer   •  Float   • 

    DateTime   •  Boolean   •  Blob   Scalar  Types  
  7. •  Documenta:on   •  Valida:on   – Required   – Min/max  length

      – Regex  pa\erns   – Enum   •  HTTP  binding   Type  Metadata  
  8. Why  You  “Need”  an  API  Model   •  Filter  incoming

     parameters   •  Perform  parameter  type  conversion   •  Generate  comprehensive  API  documenta:on   •  Generate  clients   •  Versioning    
  9. Why  You  “Need”  an  API  Model   •  Filter  incoming

     parameters   •  Perform  parameter  type  conversion   •  Generate  comprehensive  API   documentaGon   •  Generate  clients   •  Versioning    
  10. Why  You  “Need”  an  API  Model   •  Filter  incoming

     parameters   •  Perform  parameter  type  conversion   •  Generate  comprehensive  API  documenta:on   • Generate  clients   •  Versioning    
  11. Why  You  “Need”  an  API  Model   •  Filter  incoming

     parameters   •  Perform  parameter  type  conversion   •  Generate  comprehensive  API  documenta:on   •  Generate  clients   •  Versioning    
  12. EnumeraGon  and  PaginaGon   •  Opera:on  level  metadata   • 

    Describe  what  the  interes:ng  bits  are  to   enumerate   •  Describes  how  to  “get  more”  
  13. Resource  “Waiters”   •  Common  problem  for  web  services  to

     be   eventually  consistent   •  Describe  what  state  is  desired   •  Describe  failure  states   •  Poll  for  resource  states  
  14. Strongly  Typed  Clients   •  Create  Ruby  Struct  classes  for

     each  type   •  Faster  than  hash  based  access   •  Gives  be\er  feedback  on  invalid  keys   •  Easier  to  document  
  15. API  Models,  Not  a  Novel  Concept*   •  Rails  Specific

      –  Grape   –  Rails::api   –  Rocket  pants   •  Others   –  Rest.li   –  Apache  Thrib   –  Swagger   *  Same  same,  but  different   *  Why  aren’t  you  using  one?