compojure.api.resource
resource
(resource data)
Creates a nested compojure-api Route from enchanced ring-swagger operations map. By default, applies both request- and response-coercion based on those definitions.
Extra keys:
-
:middleware Middleware in duct-format either at top-level or under methods. Top-level mw are applied first if route matches, method-level mw are applied next if method matches
-
:coercion A function from request->type->coercion-matcher, used in resource coercion for :body, :string and :response. Setting value to
(constantly nil)
disables both request- & response coercion. See tests and wiki for details.
Enchancements to ring-swagger operations map:
1) :parameters use ring request keys (query-params, path-params, …) instead of swagger-params (query, path, …). This keeps things simple as ring keys are used in the handler when destructuring the request.
2) at resource root, one can add any ring-swagger operation definitions, which will be available for all operations, using the following rules:
2.1) :parameters are deep-merged into operation :parameters 2.2) :responses are merged into operation :responses (operation can fully override them) 2.3) all others (:produces, :consumes, :summary,…) are deep-merged by compojure-api
3) special keys :handler
and/or :async-handler
either under operations or at top-level. They should be 1-ary and 3-ary Ring handler functions, respectively, that are responsible for the actual request processing. Handler lookup order is the following:
3.1) If called asynchronously, operations-level :async-handler 3.2) Operations-level :handler 3.3) If called asynchronously, top-level :async-handler 3.4) Top-level :handler
4) request-coercion is applied once, using deep-merged parameters for a given operation or resource-level if only resource-level handler is defined.
5) response-coercion is applied once, using merged responses for a given operation or resource-level if only resource-level handler is defined.
Note: Swagger operations are generated only from declared operations (:get, :post, ..), despite the top-level handler could process more operations.
Example:
(resource {:parameters {:query-params {:x Long}} :responses {500 {:schema {:reason s/Str}}} :get {:parameters {:query-params {:y Long}} :responses {200 {:schema {:total Long}}} :handler (fn [request] (ok {:total (+ (-> request :query-params :x) (-> request :query-params :y))}))} :post {} :handler (constantly (internal-server-error {:reason “not implemented”}))})