Top Banner
Swaggered web apis in Clojure Tommi Reiman @ikitommi
22

Swaggered web apis in Clojure

May 06, 2015

Download

Technology

metosin

Slides from Clojure Finland March Meetup: Building Swagger-enabled web apis in Clojure (with Compojure-Api & fnhouse)
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Swaggered web apis in Clojure

Swaggered web apis in Clojure

Tommi Reiman @ikitommi

Page 2: Swaggered web apis in Clojure

Buzz •  Web apis •  SOA •  REST •  CQRS •  Microservices

Purpose  of  REST  is  to  be  able  to  say  You're  Doing  It  

Wrong  

Page 3: Swaggered web apis in Clojure

Swagger •  Definition

–  Swagger is a specification and complete framework implementation for describing, producing, consuming, and visualizing RESTful web services

–  https://helloreverb.com/developers/swagger •  Clojure Libs

–  Swag (https://github.com/narkisr/swag) –  Octohipster (https://github.com/myfreeweb/octohipster) –  Ring-Swagger

•  Compojure-Api (https://github.com/metosin/compojure-api) •  fnhouse (demo) (https://github.com/ikitommi/fnhouse)

Page 4: Swaggered web apis in Clojure

Ring-Swagger •  New Swagger implementation for Ring

–  https://github.com/metosin/ring-swagger –  Based on Schema for data models & coercion

•  Mapping of Schemas to JSON Schemas •  Symmetric parsers for common types (inc. Dates, DateTimes) •  Add-ons (coercers, schema predicates, etc.)

–  Provides Swagger spec endpoints as Ring routes •  Not yet feature complete, PRs welcome.

–  Independent to routing libs •  Currently bindings exist to Compojure-Api & fnhouse

•  Why didn’t just use (the otherwise cool) –  Swag? è Has it’s own schema models –  Octohipster? è Married with the Liberator, JSON Schemas

Page 5: Swaggered web apis in Clojure

Ring-Swagger-UI •  Jar-packaged version of the Swagger-ui

–  https://github.com/metosin/ring-swagger-ui –  Follows original npm package versions

•  Currently manual building process

Page 6: Swaggered web apis in Clojure

Ring-http-response •  Real http status codes for Ring-apps –  Ported from Spray (thanks dudes) –  https://github.com/metosin/ring-http-response

Page 7: Swaggered web apis in Clojure

Compojure-Api •  Dead-simple web api lib on top of Compojure –  https://github.com/metosin/compojure-api –  Macros & middlewares with good defaults for web apis –  Schema-based models, coercion and destructuring –  Swagger documentation

Page 8: Swaggered web apis in Clojure

•  A Schema is a Clojure(Script) data structure describing a data shape, which can be used to document and validate functions and data. –  https://github.com/prismatic/schema

Page 9: Swaggered web apis in Clojure

Schema coersion

Page 10: Swaggered web apis in Clojure

ring.swagger.schema •  On top of schema.core – Defmodel (validation & support for nested models) –  Extended json-coercing –  Support for Dates, Keywords, Sets –  Extra meta-data placeholders – Common predicates

Page 11: Swaggered web apis in Clojure

Compojure-Api

Page 12: Swaggered web apis in Clojure

Lispmacrophilia •  Lispmacrophilia, also

called code-generation-walking-madness, is the uncontrollable urge to play with the lisp macros.

Compile-­‐>me  

Run-­‐>me  

©  Federal  Prac>ce  Group  

A  macro  

Page 13: Swaggered web apis in Clojure

Macro-peeling & source linking

Page 14: Swaggered web apis in Clojure

Macro-peeling & source linking

Page 15: Swaggered web apis in Clojure

Macro-peeling & source linking

Page 16: Swaggered web apis in Clojure

Start programming already.

Page 17: Swaggered web apis in Clojure

Next things, maybe •  Ring-Swagger

–  Support for vanilla schemas –  Make feature complete

•  Compojure-Api –  Lein-template –  Bidi, bidi, bidi! –  Typed query & path params –  FILE*, WS*

•  kekkonen.io –  The CQRS-lib

Page 18: Swaggered web apis in Clojure

fnhouse •  Fnhouse is a library that

builds on top of ring to provide a concise way to write web handlers safely, readably, without global variables, without repeating yourself, and with hooks to do much more.

•  https://github.com/Prismatic/fnhouse

Page 19: Swaggered web apis in Clojure

fnhouse •  Defnks with web-metadata –  Builds on schema & plumbing

•  Separate steps to collect, wrap to ring-handlers, extract docs

•  Demo

Page 20: Swaggered web apis in Clojure

fnhouse & ring-swagger? •  https://github.com/ikitommi/fnhouse •  fnhouse.swagger (42 loc) •  bootstrap it:

Page 21: Swaggered web apis in Clojure

The trip •  My first real open sourced libs –  Positive pressure not to deliver crap – Using Travis now for own private projects too

•  Learned a lot, again. •  Met nice new people •  My Lispmacrophilia worn out – …for now at least

Page 22: Swaggered web apis in Clojure

Takeaways •  Swagger and Schema are awesome •  The libs (pull requests welcome!)

–  Ring-http-request - for http-requests –  Ring-Swagger - brings Swagger to Ring apps –  Compojure Api - to build web apis

•  Fnhouse kicks ass –  & with Swagger

•  Follow @ikitommi, @metosin & @PrismaticEng for updates