Created
May 21, 2015 12:18
-
-
Save zamaterian/d83f3a2816ecbc54013e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn resolve-symbol-value [sym] | |
;todo catch execptions and rethrow with sym name | |
(if (symbol? sym) | |
(var-get (resolve sym)) | |
sym)) | |
(defmulti restructure-swagger | |
(fn [method swagger-map liberator-map] (prn method) method)) | |
(defmethod restructure-swagger :get [_ swagger-map liberator-map] | |
(let [swagger (dissoc swagger-map :body :consumes) | |
swagger (assoc swagger :produces (:available-media-types liberator-map)) | |
swagger (update-in swagger [:responses] #(dissoc % :202 :204 :201)) | |
] | |
swagger)) | |
(defmethod restructure-swagger :post [method swagger-map liberator-map] | |
(let [swagger (dissoc swagger-map :return) | |
swagger (assoc swagger :produces (:available-media-types liberator-map) :consumes (:available-media-types liberator-map)) | |
swagger (update-in swagger [:responses] #(dissoc % :200 :202))] | |
swagger)) | |
(defmethod restructure-swagger :put [method swagger-map liberator-map] | |
(let [swagger (dissoc swagger-map :return) | |
swagger (assoc swagger :produces (:available-media-types liberator-map) :consumes (:available-media-types liberator-map)) | |
swagger (update-in swagger [:responses] #(dissoc % :200 :202)) | |
] | |
swagger)) | |
(defmethod restructure-swagger :patch [method swagger-map liberator-map] | |
(let [swagger (dissoc swagger-map :return) | |
swagger (assoc swagger :produces (:available-media-types liberator-map) :consumes (:available-media-types liberator-map)) | |
swagger (update-in swagger [:responses] #(dissoc % :200 :202)) | |
] | |
swagger)) | |
(defmethod restructure-swagger :delete [method swagger-map liberator-map] | |
(let [swagger (dissoc swagger-map :return :consumes :produces :body) | |
swagger (update-in swagger [:responses] #(dissoc % :201))] | |
swagger)) | |
(defmacro api1* [path params liberator-base liberator-resource swagger] | |
(let [lib-body# (concat (resolve-symbol-value liberator-base) (resolve-symbol-value liberator-resource)) | |
res# (apply hash-map lib-body#) | |
swagger-map# (apply hash-map (resolve-symbol-value swagger)) | |
lib-res# (apply list 'defresource (gensym "api-service-") path params lib-body#) | |
routes# (gensym "lib-route-") | |
methods# (:allowed-methods res#) | |
route-fn (fn [method] | |
(conj (apply vector | |
path | |
params | |
(mapcat identity (restructure-swagger method swagger-map# res#))) | |
lib-res#)) | |
] | |
`(defroutes routes# ~@(map #(apply list ({:get 'GET* :put 'PUT*} %) (route-fn %)) methods#)))) | |
(s/defschema TEST {:test-id Long}) | |
(s/defschema TEST2 {:2est-id Long}) | |
(def base-api | |
[;:handle-unauthorized clib/unauthorized-handler | |
;:authorized? clib/authorized? | |
:available-media-types (conj clib/available-media-types "application/json") | |
;:allowed? clib/allowed? | |
:handle-exception clib/exception-handler]) | |
(defroutes* app | |
(GET* "/workqueue" [] (workqueue-resource [:get])) | |
(api1* "/test/:id" [id] | |
base-api | |
[;:available-charsets ["utf-8"] | |
:location #(clib/build-entry-url (get % :request)) | |
:allowed-methods [:get :put] | |
:exists? (fn [_] {::entity [ (str id "dsfdasfadsfadsf")]}) | |
:handle-ok ::entity] | |
[:path-params [id :- Long] | |
:summary "Service beskrivelse" | |
:body [body TEST2] | |
:responses {;200 TEST | |
; 201 {:description "not found"} | |
400 {:errors "Validation error"} | |
404 {:errors "Not found"}} | |
:description "Update and retrive x " | |
:tags ["system"] | |
]) | |
) | |
(defapi handler | |
(middlewares [(clog/wrap-tracing) (wrap-params)(dev/wrap-trace :ui)] | |
(swagger-ui) | |
(swagger-docs | |
{: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 "Ecliipse Public License" | |
:url "http://www.eclipse.org/legal/epl-v10.html"}} | |
:tags [ | |
{:name "system", :description "system services"}]}) | |
app)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment