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

Clojure Colombia Opening Meetup

Clojure Colombia Opening Meetup

An initial introduction to the Coljure (Clojure Colombia), and the basics of how to use swagger + compojure api to build simple APIs using clojure

sarcilav

March 17, 2016
Tweet

More Decks by sarcilav

Other Decks in Programming

Transcript

  1. Agradecimientos 4 Barista Ventures 4 Atom House 4 Clojure Clojure

    Colombia (AKKA Coljure), Marzo 17, 2016 2
  2. Coljure online 4 clojurians slack -> clojure-colombia 4 Colombia.dev slack

    -> fp, lisp, jvm-langs Clojure Colombia (AKKA Coljure), Marzo 17, 2016 3
  3. Coljure team 4 Oscar Rendon 4 Sebastián Castillo 4 Hernán

    Metaute 4 Sebastián Arcila Clojure Colombia (AKKA Coljure), Marzo 17, 2016 4
  4. Elementos principales de nuestra api en clojure 4 Compojure 4

    Schema 4 Swagger Clojure Colombia (AKKA Coljure), Marzo 17, 2016 6
  5. Compojure Compojure is a small routing library for Ring that

    allows web applications to be composed of small, independent parts. Clojure Colombia (AKKA Coljure), Marzo 17, 2016 7
  6. Example (ns hello-world.core (:require [compojure.core :refer :all] [compojure.route :as route]))

    (defroutes app (GET "/" [] "<h1>Hello World</h1>") (route/not-found "<h1>Page not found</h1>")) Clojure Colombia (AKKA Coljure), Marzo 17, 2016 8
  7. Example (1/3) (ns schema-examples (:require [schema.core :as s :include-macros true

    ;; cljs only ])) (def Data "A schema for a nested data type" {:a {:b s/Str :c s/Int} :d [{:e s/Keyword :f [s/Num]}]}) Clojure Colombia (AKKA Coljure), Marzo 17, 2016 10
  8. Example (2/3) (s/validate Data {:a {:b "abc" :c 123} :d

    [{:e :bc :f [12.2 13 100]} {:e :bc :f [-1]}]}) ;; Success! Clojure Colombia (AKKA Coljure), Marzo 17, 2016 11
  9. Example (3/3) (s/validate Data {:a {:b 123 :c "ABC"}}) ;;

    Exception -- Value does not match schema: ;; {:a {:b (not (instance? java.lang.String 123)), ;; :c (not (integer? "ABC"))}, ;; :d missing-required-key} Clojure Colombia (AKKA Coljure), Marzo 17, 2016 12
  10. Swagger 4 Technically speaking - Swagger is a formal specification

    surrounded by a large ecosystem of tools, which includes everything from front-end user interfaces, low-level code libraries and commercial API management solutions. 4 Blah blah Clojure Colombia (AKKA Coljure), Marzo 17, 2016 13
  11. Ring-Swagger Route definitions are expected as a clojure Map defined

    by the Schema Contract. The Schema allows mostly any extra keys as ring-swagger tries not to be on your way - one can pass any valid Swagger spec data in. Clojure Colombia (AKKA Coljure), Marzo 17, 2016 14
  12. Example (require '[ring.swagger.swagger2 :as rs]) (rs/swagger-json {}) ; {:swagger "2.0"

    ; :info {:title "Swagger API" ; :version "0.0.1"} ; :produces ["application/json"] ; :consumes ["application/json"] ; :definitions {} ; :paths {}} Clojure Colombia (AKKA Coljure), Marzo 17, 2016 15
  13. A real example (2/3) (s/defschema User {:id s/Str, :name s/Str

    :address {:street s/Str :city (s/enum :tre :hki)}}) Clojure Colombia (AKKA Coljure), Marzo 17, 2016 17
  14. A real example (3/3) (s/with-fn-validation (rs/swagger-json {:info {:version "1.0.0" :title

    "Sausages" :description "Sausage description" :termsOfService "http://helloreverb.com/terms/" :contact {:name "My API Team" :email "[email protected]" :url "http://www.metosin.fi"} :license {:name "Eclipse Public License" :url "http://www.eclipse.org/legal/epl-v10.html"}} :tags [{:name "user" :description "User stuff"}] :paths {"/api/ping" {:get nil} "/user/:id" {:post {:summary "User Api" :description "User Api description" :tags ["user"] :parameters {:path {:id s/Str} :body User} :responses {200 {:schema User :description "Found it!"} 404 {:description "Ohnoes."}}}}}})) Clojure Colombia (AKKA Coljure), Marzo 17, 2016 18
  15. Links 4 Compojure - https://github.com/weavejester/ compojure 4 Schema - https://github.com/plumatic/schema

    4 Swagger - http://swagger.io/getting-started/ 4 Ring-Swagger - https://github.com/metosin/ring- swagger Clojure Colombia (AKKA Coljure), Marzo 17, 2016 19
  16. Y ahora uno de verdad lein new luminus myapi +swagger

    Clojure Colombia (AKKA Coljure), Marzo 17, 2016 20