![Page 1: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome](https://reader034.vdocuments.us/reader034/viewer/2022051318/58a0588b1a28ab5c1c8b5613/html5/thumbnails/1.jpg)
Kekkonen ���making your Clojure web APIs more awesome
ClojuTRE 2015Tommi Reiman
@ikitommi
![Page 2: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome](https://reader034.vdocuments.us/reader034/viewer/2022051318/58a0588b1a28ab5c1c8b5613/html5/thumbnails/2.jpg)
Topics• API all the things!• Challenges• Kekkonen• Done
![Page 3: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome](https://reader034.vdocuments.us/reader034/viewer/2022051318/58a0588b1a28ab5c1c8b5613/html5/thumbnails/3.jpg)
API all the things!• Clojure(Script) APIs• RPC• Web APIs• REST APIs• Future?
![Page 4: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome](https://reader034.vdocuments.us/reader034/viewer/2022051318/58a0588b1a28ab5c1c8b5613/html5/thumbnails/4.jpg)
Clojure(Script) APIs• Namespaces and functions (Vars)• Extension via multimethods & protocols• Simple, beautiful, no remoting
![Page 5: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome](https://reader034.vdocuments.us/reader034/viewer/2022051318/58a0588b1a28ab5c1c8b5613/html5/thumbnails/5.jpg)
RPC• Expose the tagged Clojure functions outwards• Easy remoting• refactoring? external clients?
![Page 6: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome](https://reader034.vdocuments.us/reader034/viewer/2022051318/58a0588b1a28ab5c1c8b5613/html5/thumbnails/6.jpg)
Web APIs• Map Clojure functions into http endpoints• Thinking in HTTP terms• Swagger docs
![Page 7: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome](https://reader034.vdocuments.us/reader034/viewer/2022051318/58a0588b1a28ab5c1c8b5613/html5/thumbnails/7.jpg)
REST Apis• Map Clojure functions into resources• Resource containers• Swagger docs (yada!)
![Page 8: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome](https://reader034.vdocuments.us/reader034/viewer/2022051318/58a0588b1a28ab5c1c8b5613/html5/thumbnails/8.jpg)
Future?• Datomic & DataScript• Falcor/Netflix
– A JavaScript library for efficient data fetching
• Relay/Facebook– A JAVASCRIPT FRAMEWORK FOR BUILDING
DATA-DRIVEN REACT APPLICATIONS
• The Web after Tomorrow– http://tonsky.me/blog/the-web-after-tomorrow/
© Nikita Prokopov
![Page 9: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome](https://reader034.vdocuments.us/reader034/viewer/2022051318/58a0588b1a28ab5c1c8b5613/html5/thumbnails/9.jpg)
Hmph.• We build complex UIs• Reagent is cool• Need to do remoting, public api-docs too• Business rules need to be enforced both on
server (all) & frontend (part)– Shared code is cool, but number of combinations…
• REST doesn’t even try to solve these issues
![Page 10: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome](https://reader034.vdocuments.us/reader034/viewer/2022051318/58a0588b1a28ab5c1c8b5613/html5/thumbnails/10.jpg)
CQRS? ���making your Clojure web APIs more awesome
![Page 11: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome](https://reader034.vdocuments.us/reader034/viewer/2022051318/58a0588b1a28ab5c1c8b5613/html5/thumbnails/11.jpg)
Commands & Queries• http://martinfowler.com/bliki/CQRS.html • Verbs (actions) instead of Nouns (resources)• Command log (auditing) as a bonus• != Event Sourcing• Great for user interactions
– Rules usually per interaction, not resource– Fine-grained à lot’s of actions
© Mar.n Fowler
![Page 12: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome](https://reader034.vdocuments.us/reader034/viewer/2022051318/58a0588b1a28ab5c1c8b5613/html5/thumbnails/12.jpg)
Commands & Queries
Watch
GiveStar GetRepository
Fork
Unwatch RemoveStar ListRespotories
![Page 13: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome](https://reader034.vdocuments.us/reader034/viewer/2022051318/58a0588b1a28ab5c1c8b5613/html5/thumbnails/13.jpg)
Commands & Queries
Watch
GiveStar GetRepository
Fork
Unwatch RemoveStar
[repo-‐id :-‐ s/Str] check-‐repo-‐auth (watched? false)
[repo-‐id :-‐ s/Str] check-‐repo-‐auth
[repo-‐id :-‐ s/Str] check-‐repo-‐auth (watched? true)
[repo-‐id :-‐ s/Str] check-‐repo-‐auth (starred? false)
[repo-‐id :-‐ s/Str] check-‐repo-‐auth
[repo-‐id :-‐ s/Str] check-‐repo-‐auth (starred? true)
ListRespotories
![Page 14: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome](https://reader034.vdocuments.us/reader034/viewer/2022051318/58a0588b1a28ab5c1c8b5613/html5/thumbnails/14.jpg)
DIY CQRS lib
![Page 15: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome](https://reader034.vdocuments.us/reader034/viewer/2022051318/58a0588b1a28ab5c1c8b5613/html5/thumbnails/15.jpg)
![Page 16: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome](https://reader034.vdocuments.us/reader034/viewer/2022051318/58a0588b1a28ab5c1c8b5613/html5/thumbnails/16.jpg)
Rethinking the APIs• Idea– Expose simple Clojure functions as message handlers– Manage handlers in virtual namespaces– Data-driven, no macros, no magic– Explicit extensions via protocols, options and meta-data– Transports abstracted away, http via ring
• Lessons learned from ring-swagger & compojure-api– Clients as first-class citizens
• Remote api documentation as data: rules as data• Public http api documentation via Swagger
![Page 17: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome](https://reader034.vdocuments.us/reader034/viewer/2022051318/58a0588b1a28ab5c1c8b5613/html5/thumbnails/17.jpg)
Thanks to• Prismatic Schema, (Plumbing)• Elegance of fnhouse• Ring-swagger• Best parts of compojure-api• Schema-tools
![Page 18: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome](https://reader034.vdocuments.us/reader034/viewer/2022051318/58a0588b1a28ab5c1c8b5613/html5/thumbnails/18.jpg)
Concepts• Handlers & namespaces
– Functions with meta-data, contextàresponse• Context
– A message context consumed by the handler– Opinion: should contain :data –key with the actual payload
• Registry– Collects and enriches handlers into namespaces– Handler invocation & input (pre-)validation– Holds global state
• Ring-adapter– http-bindings for invoking the handlers + schema coercion
• API– Wrapping everything up + exception handling
![Page 19: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome](https://reader034.vdocuments.us/reader034/viewer/2022051318/58a0588b1a28ab5c1c8b5613/html5/thumbnails/19.jpg)
registry
handler
handler
handler
ns
ns
ns
API
Ring-‐hander
SwaggerUI O
O O
O
CQRS-‐API O
ac.on & context
M
M M
request
![Page 20: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome](https://reader034.vdocuments.us/reader034/viewer/2022051318/58a0588b1a28ab5c1c8b5613/html5/thumbnails/20.jpg)
Show me
![Page 21: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome](https://reader034.vdocuments.us/reader034/viewer/2022051318/58a0588b1a28ab5c1c8b5613/html5/thumbnails/21.jpg)
Extensions• Registry/Ring/API Options– Declare things, plug-in transformers
![Page 22: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome](https://reader034.vdocuments.us/reader034/viewer/2022051318/58a0588b1a28ab5c1c8b5613/html5/thumbnails/22.jpg)
Extensions• Handler & namespace meta-data– Explicit way to extend handler functionality at runtime– Context enrichment, Authorization, Api-doc info, …
![Page 23: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome](https://reader034.vdocuments.us/reader034/viewer/2022051318/58a0588b1a28ab5c1c8b5613/html5/thumbnails/23.jpg)
HTTP API
![Page 24: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome](https://reader034.vdocuments.us/reader034/viewer/2022051318/58a0588b1a28ab5c1c8b5613/html5/thumbnails/24.jpg)
Demo
![Page 25: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome](https://reader034.vdocuments.us/reader034/viewer/2022051318/58a0588b1a28ab5c1c8b5613/html5/thumbnails/25.jpg)
![Page 26: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome](https://reader034.vdocuments.us/reader034/viewer/2022051318/58a0588b1a28ab5c1c8b5613/html5/thumbnails/26.jpg)
Client-side?• Expose handler data to the clients as clojure (or
JSON) data– list all, available or validated
• Either ask from server or apply the rules for the local dataset– Server: simple, more traffic– Client: shared validation-functions, 2+ datasources
![Page 27: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome](https://reader034.vdocuments.us/reader034/viewer/2022051318/58a0588b1a28ab5c1c8b5613/html5/thumbnails/27.jpg)
![Page 28: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome](https://reader034.vdocuments.us/reader034/viewer/2022051318/58a0588b1a28ab5c1c8b5613/html5/thumbnails/28.jpg)
TODO• Finalize the lib (1-2 months?)• Feedback from the community• RE-Kekkonen (a reagent template)• Async (just rewrite the api-middleware)• Event sourcing
![Page 29: ClojuTRE2015: Kekkonen - making your Clojure web APIs more awesome](https://reader034.vdocuments.us/reader034/viewer/2022051318/58a0588b1a28ab5c1c8b5613/html5/thumbnails/29.jpg)
Thanks. hVps://github.com/metosin/kekkonen
[email protected] #ring-‐swagger at Slack @metosin at TwiVer
hiring are we.