Skip to content

Commit

Permalink
Fix defresource so it no longer implicitly binds 'request'
Browse files Browse the repository at this point in the history
  • Loading branch information
jonase committed Oct 18, 2015
1 parent 1a2a510 commit 1531c30
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 30 deletions.
12 changes: 7 additions & 5 deletions CHANGES.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

# Unreleased

* The `defresource` macro no longer implicitly binds `request`.

* Values can be added to the context at the beginning of the execution
flow using the :initialize-context action.

Expand Down Expand Up @@ -30,7 +32,7 @@
## Bugs fixed

* #162 This release actually contains the changes announced for 0.12.1
Due to whatever reason the revision in clojars did not match
Due to whatever reason the revision in clojars did not match
what was tagged as 0.12.1 in the git repository.

# New in 0.12.1
Expand Down Expand Up @@ -105,14 +107,14 @@
location from context key :location
* Extractor for graphivz dot file that reads core.clj
* Bump hiccup dependency to 1.0.2
* Add can-put-to-missing?
* Add can-put-to-missing?
* Fix representation render-map-csv
* Make liberator build with lein 2.0.0RC1 (manage dependencies)
* Drop unnecessary methods from Representation
* Dispatch Representation on MapEquivalence and Sequential which
increased robustness
* Fixes to HTML Table representation (missing tr)
* Render Clojure Representation using \*print-dup\*
* Render Clojure Representation using \*print-dup\*
* Support "application/edn" representation

## Bugs fixed
Expand All @@ -129,12 +131,12 @@

* Include olympics example data with source

## Bugs fixes
## Bugs fixes
* Handle line-break and whitespace in Accept headers
* Ignore case in character set negotiation
* #12 String representation sets character set
* #9 Missing media-type for "hello george" example
* #11
* #11
* #14 Use newer org.clojure:data.csv

# New in 0.7.0
Expand Down
47 changes: 23 additions & 24 deletions src/liberator/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
(:import (javax.xml.ws ProtocolException)))

(defmulti coll-validator
"Return a function that evaluaties if the give argument
a) is contained in a collection
"Return a function that evaluaties if the give argument
a) is contained in a collection
b) equals an argument
c) when applied to a function evaluates as true"
c) when applied to a function evaluates as true"
(fn [x] (cond
(coll? x) :col
(fn? x) :fn)))
Expand All @@ -23,10 +23,10 @@
(defmethod coll-validator :default [x]
(partial = x))

(defn console-logger [category values]
(defn console-logger [category values]
#(apply println "LOG " category " " values))

(def ^:dynamic *loggers* nil)
(def ^:dynamic *loggers* nil)

(defmacro with-logger [logger & body]
`(binding [*loggers* (conj (or *loggers* []) ~logger)]
Expand Down Expand Up @@ -94,7 +94,7 @@
`(defn ~name [~'context]
(decide ~(keyword name) ~test ~then ~else ~'context)))

(defmacro defdecision
(defmacro defdecision
([name then else]
(defdecision* name nil then else))
([name test then else]
Expand Down Expand Up @@ -282,22 +282,22 @@

(defhandler handle-precondition-failed 412 "Precondition failed.")

(defdecision if-match-star-exists-for-missing?
(defdecision if-match-star-exists-for-missing?
if-match-star
handle-precondition-failed
method-put?)

(defhandler handle-not-modified 304 nil)

(defdecision if-none-match?
(defdecision if-none-match?
#(#{ :head :get} (get-in % [:request :request-method]))
handle-not-modified
handle-precondition-failed)

(defdecision put-to-existing? (partial =method :put)
conflict? multiple-representations?)

(defdecision post-to-existing? (partial =method :post)
(defdecision post-to-existing? (partial =method :post)
post! put-to-existing?)

(defhandler handle-accepted 202 "Accepted")
Expand All @@ -322,7 +322,7 @@
handle-not-modified)

(defdecision if-modified-since-valid-date?
(fn [context]
(fn [context]
(if-let [date (parse-http-date (get-in context [:request :headers "if-modified-since"]))]
{::if-modified-since-date date}))
modified-since?
Expand All @@ -341,7 +341,7 @@
if-none-match?
if-modified-since-exists?)

(defdecision if-none-match-star?
(defdecision if-none-match-star?
#(= "*" (get-in % [:request :headers "if-none-match"]))
if-none-match?
etag-matches-for-if-none?)
Expand All @@ -360,7 +360,7 @@
if-none-match-exists?)

(defdecision if-unmodified-since-valid-date?
(fn [context]
(fn [context]
(when-let [date (parse-http-date (get-in context [:request :headers "if-unmodified-since"]))]
{::if-unmodified-since-date date}))
unmodified-since?
Expand All @@ -377,7 +377,7 @@
if-unmodified-since-exists?
handle-precondition-failed)

(defdecision if-match-star?
(defdecision if-match-star?
if-match-star if-unmodified-since-exists? etag-matches-for-if-match?)

(defdecision if-match-exists? (partial header-exists? "if-match")
Expand All @@ -390,7 +390,7 @@

(defhandler handle-not-acceptable 406 "No acceptable resource available.")

(defdecision encoding-available?
(defdecision encoding-available?
(fn [ctx]
(when-let [encoding (conneg/best-allowed-encoding
(get-in ctx [:request :headers "accept-encoding"])
Expand Down Expand Up @@ -425,7 +425,7 @@
(defdecision language-available?
#(try-header "Accept-Language"
(when-let [lang (conneg/best-allowed-language
(get-in % [:request :headers "accept-language"])
(get-in % [:request :headers "accept-language"])
((get-in context [:resource :available-languages]) context))]
(if (= lang "*")
true
Expand All @@ -437,8 +437,8 @@

(defn negotiate-media-type [context]
(try-header "Accept"
(when-let [type (conneg/best-allowed-content-type
(get-in context [:request :headers "accept"])
(when-let [type (conneg/best-allowed-content-type
(get-in context [:request :headers "accept"])
((get-in context [:resource :available-media-types] (constantly "text/html")) context))]
{:representation {:media-type (conneg/stringify type)}})))

Expand All @@ -452,7 +452,7 @@
;; client accepts all media types" [p100]
;; in this case we do content-type negotiation using */* as the accept
;; specification
(if-let [type (liberator.conneg/best-allowed-content-type
(if-let [type (liberator.conneg/best-allowed-content-type
"*/*"
((get-in context [:resource :available-media-types]) context))]
[false {:representation {:media-type (liberator.conneg/stringify type)}}]
Expand Down Expand Up @@ -583,7 +583,7 @@
(initialize-context {:request request
:resource (map-values make-function (merge default-functions kvs))
:representation {}})

(catch ProtocolException e ; this indicates a client error
{:status 400
:headers {"Content-Type" "text/plain"}
Expand All @@ -607,11 +607,11 @@
kvs (rest kvs)]
;; Rather than call resource, create anonymous fn in callers namespace for better debugability.
`(defn ~name [~@args]
(fn [~'request]
(run-resource ~'request (get-options (list ~@kvs))))))
(fn [request#]
(run-resource request# (get-options (list ~@kvs))))))
`(def ~name
(fn [~'request]
(run-resource ~'request (get-options (list ~@kvs)))))))
(fn [request#]
(run-resource request# (get-options (list ~@kvs)))))))

(defn by-method
"returns a handler function that uses the request method to
Expand All @@ -623,4 +623,3 @@
:delete \"Entity was deleted successfully.\"})"
[map]
(fn [ctx] ((make-function (get map (get-in ctx [:request :request-method]) ctx)) ctx)))

8 changes: 7 additions & 1 deletion test/test_defresource.clj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
(parametrized-config media-type)
:handle-ok (fn [_] (format "The text is %s" txt)))

(defresource non-anamorphic-request [request]
:handle-ok (str request))

(facts "about defresource"
(fact "its simple form should behave as it always has"
(without-param {:request-method :get})
Expand All @@ -77,7 +80,10 @@
=> {:headers {"Content-Type" "text/plain;charset=UTF-8"}, :body "with-service-available?-multimethod", :status 200})
(fact "should allow multi methods as decisions alternate path"
(with-decisions-multimethod {:request-method :get :service-available? :not-available})
=> {:headers {"Content-Type" "text/plain;charset=UTF-8"}, :body "Service not available.", :status 503}))
=> {:headers {"Content-Type" "text/plain;charset=UTF-8"}, :body "Service not available.", :status 503})
(fact "should allow 'request' to be used as a resource parameter name, this was a bug at a time."
(:body ((non-anamorphic-request "test") {:request-method :get}))
=> "test"))


(def fn-with-options
Expand Down

0 comments on commit 1531c30

Please sign in to comment.