Scala & Swagger at Wordnik


Published on

A deck on the practical reasons why Wordnik moved to the Scala programming language. Also covered is the Swagger REST API framework which is available at

Published in: Technology, Education
  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Scala & Swagger at Wordnik

  1. 1. Scala & Swagger at Wordnik<br />Tony Tam<br />@fehguy<br />
  2. 2. Agenda<br />Who is Wordnik?<br />Why bother with this Scala thing?<br />What did we get from it?<br />What is Swagger<br />Why it should matter to you<br />
  3. 3. Wordnik Tech Overview<br />API based infrastructure<br />Micro service oriented architecture<br />Business by Scala, Java, MongoDB<br />Beauty by Javascript, Ruby<br />20M API calls daily<br />Powered by Swagger, inside & out<br />20B+ hot documents in MongoDB<br />
  4. 4. Why Bother with Scala?<br />Not just looking for next new thing<br />“I was just getting good at ${}!”<br />Technology selection has consequences<br />.netvs OSS<br />Flash vs. HTML5<br />Java is…<br />Stable<br />Fast<br />Widely accepted<br />
  5. 5. Why Bother with Scala?<br />Look at the scenery<br />One size does NOT fit all<br />Rise of Ruby, Javascript, node.js, etc.<br />Hey, they did lots of things right<br />Focus on making things easy<br />
  6. 6. So What is it?<br />JVM-based language<br />Started by Martin Odersky<br />Functional & OO<br />Typesafe<br />Fully compatible with Java<br />IncludesDizzying Syntax<br />
  7. 7. Java Pain Points<br />DRY is difficult with Java<br />Interfaces are great<br />Implementation?<br />Many ways to peel the onion! <br />Easy to follow<br />Often explosive LOC!<br />
  8. 8. Example<br />Our REST service<br />Back-end returns different object implementations depending on operation<br />
  9. 9. Example<br />Many operations produce models with similar attributes<br />Resulting in a mess<br /> of classes<br />
  10. 10. Example<br />An Interface…<br />An Implementation…<br />
  11. 11. Example<br />Another Impl…<br />
  12. 12. Example<br />And another…<br />
  13. 13. Example<br />Implementation is duplicated everywhere<br />Annotations are duplicated everywhere<br />Am I lazy to complain?<br />Consistency<br />Matters!<br />
  14. 14. Simplified with Scala<br />Objects can have Traits<br />Like java interfaces + ruby mixins<br />Creates “proper” subtypes<br />Compiler built, concrete classes<br />The “Word” trait…<br />The corresponding impl…<br />
  15. 15. Simplified with Scala<br />More traits. Reuse these across your codebase!<br />The other Impls:<br />
  16. 16. Simplified with Scala<br />Big deal?<br />Just the beginning of the good stuff<br />Scala syntax<br />In Java, strict syntax is enforced<br />In Scala: “it’s OK unless it’s not”<br />
  17. 17. XML in Scala<br />Support is baked in<br />Declaring XML<br />
  18. 18. XML in Scala<br />Parsing XML is easy<br />
  19. 19. Text Processing<br />But first, the match statement<br />Like switch:<br />
  20. 20. Text Processing<br />But much more powerful<br />
  21. 21. Text Processing<br />Pattern matching + “match” statement<br />Extract name in different formats<br />
  22. 22. Text Processing<br />(Erin,McKean)<br />(Joe,Hyrkin)<br />(Anthony,L.,Tam)<br />Kumanan<br />
  23. 23. Summary of Scala @ Wordnik<br />Lost NONE of our Java codebase<br />Code reuse is up<br />Code consistency is up<br />Flexibility is up<br />LOC is down<br />Readability is…<br />
  24. 24. Why Swagger?<br />Integration with REST APIs is troublesome and inconsistent<br />Different vendors have different REST semantics<br />Client libraries vary wildly by vendor, language<br />Documentation for developers is an afterthought<br />Input parameters, allowable values, models, responses are found via trial & error<br />Internally a PITA<br />YOUR API is too hard to develop against!<br />
  25. 25. How Does it Work?<br />Your server produces a Resource List<br />All available APIs<br /><br />“It’s like a sitemap for your API!”<br />
  26. 26. How Does it Work?<br />Each API declares itself<br />Available operations<br />Parameters<br />Type (path, query, body)<br />Allowable values/data types<br />Input/output models<br />Error responses with descriptions<br />
  27. 27. API Response<br /><br />
  28. 28. API Response<br />Knowing input/output models is helpful!<br />Based on JSON Schema (Draft)<br /><br />
  29. 29. But what’s the benefit?<br />A Sandbox!<br />
  30. 30. Client Library Generation<br />Code generation based on Resource Spec<br />Template-based Framework<br />Consume REST Methods, Models, Parameters<br />Produce client libraries<br />“Know before you go HTTP”<br />Required values, fields are known by the client<br />Only expose what’s allowed!<br />Swagger filtering removes methods/models you don’t have access to<br />
  31. 31. Client Library Generation<br />Super user access<br />Ordinary dev access<br />
  32. 32. Test Framework<br />JSON-driven tests for your Client + API<br />Expected data<br />Test Suites<br />
  33. 33. Test Framework<br />
  34. 34. Easy to add<br />For Java/Scala via JAX-RS…<br />Add swagger-core.jar<br />Annotate your models per your @Providerclass<br />Annotate your resources<br />
  35. 35. Easy to add<br />For node.js via express<br />Require swagger.js<br />Declare your swagger specs, models<br />Could use AST/DSL to do automatically<br />Add your operations, configure and start<br />
  36. 36. Easy DIY<br />The Swagger spec is Language Agnostic!<br />ANY swagger-compliant server can…<br />Use the Swagger client lib generator<br />Use the test framework<br />Use the sandbox UI<br />More server support from Wordnik<br />Play, Rails<br />.net, others community developed<br />
  37. 37. Where to go Next<br />Try Swagger<br /><br />See it in action<br /><br />Download the source/samples<br /><br />Discuss it<br /><br />#swagger_doc on freenode<br />
  38. 38. Questions?<br />
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.