Skip to content

Instantly share code, notes, and snippets.

@zamaterian
Created January 15, 2016 14:41
Show Gist options
  • Save zamaterian/53cc25e71d5104515e76 to your computer and use it in GitHub Desktop.
Save zamaterian/53cc25e71d5104515e76 to your computer and use it in GitHub Desktop.
Example of our rest-api that combines liberator with compojure-api
(def auth-base-api
[:service-available? workaround-missing-mediatype
:handle-unauthorized unauthorized-handler
:authorized? authorized?
:allowed? allowed?
:available-media-types (conj available-media-types "application/json")
:handle-exception exception-handler
:processable? parse-and-coerce-body
:handle-unprocessable-entity unprocessable
:available-charsets ["utf-8"]
:as-response response-post-hooks])
; defed query params
(def ^:const tid-format "ISO-8601 ex: 2015-11-22T21:54 or 2015-11-22")
(def ^:const opt-registeringstid {'registeringstid :- (ring.swagger.schema/describe org.joda.time.DateTime tid-format) nil})
(def ^:const man-registeringstid ['registeringstid :- (ring.swagger.schema/describe org.joda.time.DateTime tid-format)])
(def ^:const opt-virkningstid {'virkningstid :- (ring.swagger.schema/describe org.joda.time.DateTime tid-format) nil})
(def ^:const man-virkningstid ['virkningstid :- (ring.swagger.schema/describe org.joda.time.DateTime tid-format)])
(clib/rest-api get-ejendom "/:vur-ejd-id" [vur-ejd-id]
clib/auth-base-api
[:allowed-methods [:get]
:exists? (fn [ctx] (let [e (cass/find-vurderings-ejendomme [vur-ejd-id] (or virkningstid (t/now)) registeringstid )]
(when-not (empty? e)
{::entity (first e)})))
:handle-ok ::entity]
:responses {200 {:schema s/ejendom-schema}
401 nil
404 nil
403 nil}
:query-params [[opt-registeringstid opt-virkningstid]]
:path-params [vur-ejd-id :- Long]
:header-params [x-callid :- String, {x-user :- String ""} authorization :- String]
:description "Hent en ejendom"
:tags ["ejendom"])
; example where either query params or header params is restricted to a specific method
each element in the :allowed-methods plus :any is compiled into a seperate GET POST PATCH route with a liberator resource as body
(clib/rest-api specifik-sag "/:sag-id" [sag-id]
clib/auth-base-api
[:allowed-methods [:get :patch]
:exists? (fn [_]
(let [sag (c/hent-sag sag-id false)]
(if-not (nil? sag)
{::entity sag}))) ;GET, PATCH
:etag (fn [ctx] (clib/etag ctx ::entity))
:patch! (fn [ctx]
(let [opdateret (c/opdater-sag (::entity ctx) (get-in ctx [:request :body-params]))]
{::entity opdateret}))
:respond-with-entity? (constantly true)
:handle-ok ::entity] ;GET, PATCH
[:responses {200 {:schema sag-s/Sag}
201 nil
204 nil
401 nil
403 nil
404 nil
409 nil}
:path-params [sag-id :- String]
:header-params [x-callid :- String, ^:patch if-match :- String {x-user :- String ""} authorization :- String]
:body [body sag-s/Sagsaendring]
:description "Se eller opdatér indstillinger for en sag ud fra sags-ID"
:tags ["sager"]])
@zamaterian
Copy link
Author

header-params [x-callid :- String, ^:patch if-match :- String {x-user :- String ""} authorization :- String]

the if-match header is only mandatory and coerced under the patch route

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment