Scala & Swagger at Wordnik

38
Scala & Swagger at Wordnik Tony Tam @fehguy

description

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 http://swagger.wordnik.com

Transcript of Scala & Swagger at Wordnik

Page 1: Scala & Swagger at Wordnik

Scala & Swagger at Wordnik

Tony Tam@fehguy

Page 2: Scala & Swagger at Wordnik

Agenda

•Who is Wordnik?

•Why bother with this Scala thing?

•What did we get from it?

•What is Swagger

•Why it should matter to you

Page 3: Scala & Swagger at Wordnik

Wordnik Tech Overview

•API based infrastructure

• Micro service oriented architecture

• Business by Scala, Java, MongoDB

• Beauty by Javascript, Ruby

•20M API calls daily

• Powered by Swagger, inside & out

•20B+ hot documents in MongoDB

Page 4: Scala & Swagger at Wordnik

Why Bother with Scala?

•Not just looking for next new thing

• “I was just getting good at ${language.name}!”

•Technology selection has consequences

• .net vs OSS

• Flash vs. HTML5

•Java is…

• Stable

• Fast

• Widely accepted

Page 5: Scala & Swagger at Wordnik

Why Bother with Scala?

•Look at the scenery

• One size does NOT fit all

• Rise of Ruby, Javascript, node.js, etc.

•Hey, they did lots of things right

• Focus on making things easy

Page 6: Scala & Swagger at Wordnik

So What is it?

•JVM-based language

• Started by Martin Odersky

•Functional & OO

•Typesafe

•Fully compatible with Java

Includes Dizzying Syntax

Page 7: Scala & Swagger at Wordnik

Java Pain Points

•DRY is difficult with Java

• Interfaces are great

• Implementation?

• Many ways to peel the onion!

•Easy to follow

• Often explosive LOC!

Page 8: Scala & Swagger at Wordnik

Example

•Our REST service

• Back-end returns different object implementations depending on operation

Page 9: Scala & Swagger at Wordnik

Example

•Many operations produce models with similar attributes

•Resulting in a mess

of classes

Page 10: Scala & Swagger at Wordnik

Example

•An Interface…

•An Implementation…

Page 11: Scala & Swagger at Wordnik

Example

•Another Impl…

Page 12: Scala & Swagger at Wordnik

Example

•And another…

Page 13: Scala & Swagger at Wordnik

Example

•Implementation is duplicated everywhere

•Annotations are duplicated everywhere

• Am I lazy to complain?

Consistency

Matters!

Page 14: Scala & Swagger at Wordnik

Simplified with Scala

•Objects can have Traits

• Like java interfaces + ruby mixins

• Creates “proper” subtypes

•Compiler built, concrete classes

• The “Word” trait…

• The corresponding impl…

Page 15: Scala & Swagger at Wordnik

Simplified with Scala

•More traits. Reuse these across your codebase!

•The other Impls:

Page 16: Scala & Swagger at Wordnik

Simplified with Scala

•Big deal?

• Just the beginning of the good stuff

•Scala syntax

• In Java, strict syntax is enforced

• In Scala: “it’s OK unless it’s not”

Page 17: Scala & Swagger at Wordnik

XML in Scala

•Support is baked in

• Declaring XML

Page 18: Scala & Swagger at Wordnik

XML in Scala

•Parsing XML is easy

Page 19: Scala & Swagger at Wordnik

Text Processing

•But first, the match statement

• Like switch:

Page 20: Scala & Swagger at Wordnik

Text Processing

•But much more powerful

Page 21: Scala & Swagger at Wordnik

Text Processing

•Pattern matching + “match” statement

• Extract name in different formats

Page 22: Scala & Swagger at Wordnik

Text Processing

(Erin,McKean)(Joe,Hyrkin)(Anthony,L.,Tam)Kumanan

Page 23: Scala & Swagger at Wordnik

Summary of Scala @ Wordnik

•Lost NONE of our Java codebase

•Code reuse is up

•Code consistency is up

•Flexibility is up

•LOC is down

•Readability is…

Page 24: Scala & Swagger at Wordnik

Why Swagger?

•Integration with REST APIs is troublesome and inconsistent

• Different vendors have different REST semantics

• Client libraries vary wildly by vendor, language

• Documentation for developers is an afterthought

• Input parameters, allowable values, models, responses are found via trial & error

•Internally a PITA

•YOUR API is too hard to develop against!

Page 25: Scala & Swagger at Wordnik

How Does it Work?

•Your server produces a Resource List

• All available APIshttp://petstore.swagger.wordnik.com/api/resources.json

“It’s like a sitemap for your

API!”

Page 26: Scala & Swagger at Wordnik

How Does it Work?

•Each API declares itself

• Available operations

• Parameters

• Type (path, query, body)

• Allowable values/data types

• Input/output models

• Error responses with descriptions

Page 27: Scala & Swagger at Wordnik

API Response

http://petstore.swagger.wordnik.com/api/pet.json

Page 28: Scala & Swagger at Wordnik

API Response

•Knowing input/output models is helpful!

Based on JSON

Schema (Draft)

http://tools.ietf.org/html/draft-zyp-json-schema-03

Page 29: Scala & Swagger at Wordnik

But what’s the benefit?

•A Sandbox!

Page 30: Scala & Swagger at Wordnik

Client Library Generation

•Code generation based on Resource Spec

• Template-based Framework

• Consume REST Methods, Models, Parameters

• Produce client libraries

•“Know before you go HTTP”

• Required values, fields are known by the client

•Only expose what’s allowed!

• Swagger filtering removes methods/models you don’t have access to

Page 31: Scala & Swagger at Wordnik

Client Library GenerationOrdinary dev access

Super user

access

Page 32: Scala & Swagger at Wordnik

Test Framework

•JSON-driven tests for your Client + API

Expected dataTest Suites

Page 33: Scala & Swagger at Wordnik

Test Framework

Page 34: Scala & Swagger at Wordnik

Easy to add

•For Java/Scala via JAX-RS…

• Add swagger-core.jar

• Annotate your models per your @Provider class

• Annotate your resources

Page 35: Scala & Swagger at Wordnik

Easy to add

•For node.js via express

• Require swagger.js

• Declare your swagger specs, models

• Could use AST/DSL to do automatically

• Add your operations, configure and start

Page 36: Scala & Swagger at Wordnik

Easy DIY

•The Swagger spec is Language Agnostic!

• ANY swagger-compliant server can…

• Use the Swagger client lib generator

• Use the test framework

• Use the sandbox UI

•More server support from Wordnik

• Play, Rails

•.net, others community developed

Page 37: Scala & Swagger at Wordnik

Where to go Next

•Try Swaggerswagger.wordnik.com

•See it in actiondeveloper.wordnik.com/docs

•Download the source/samplesswagger.wordnik.com/downloads

•Discuss itgroups.google.com/group/wordnik-api

#swagger_doc on freenode

Page 38: Scala & Swagger at Wordnik

Questions?