Skip to content

Commit

Permalink
#205 use input as output - query params only
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Featch authored and Nick Featch committed Aug 19, 2016
1 parent 0d9eb97 commit b8f9e08
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
20 changes: 20 additions & 0 deletions src/protean/core/codex/placeholder.clj
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,26 @@
;; Extraction functions
;; =============================================================================

(defn- query-param-value
[tree request placeholder]
(let [qps (d/qps tree true)
ph-qps (into (sorted-map) (map #(hash-map (ffirst(holder? %)) (second %)) (map-invert qps)))
param (get-in ph-qps [placeholder])
value (get-in request [:params (keyword param)])]
(when-let [qps-v (get-in qps [param])]
(second (re-find (re-pattern (s/replace qps-v ph "(.*)")) value)))))

(defn response-bag
"Creates a bag of placeholder values for the response from the request"
[tree tree-response request]
(into
(sorted-map)
(map
#(hash-map
(second %)
(query-param-value tree request (first %)))
(holder? tree-response))))

(defn- diff [s1 s2]
(cond
(and (nil? (first s1)) (nil? (first s2))) []
Expand Down
4 changes: 2 additions & 2 deletions src/protean/core/transformation/sim.clj
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@
success (format-rsp (rand-nth successes))]
(if (empty? successes)
(log-warn "warning - no successes found for endpoint" [svc uri request-method])
(ph/swap success *tree* {} :gen-all true))))
(ph/swap success *tree* (ph/response-bag *tree* success *request*) :gen-all true))))

(defn error
"Returns a specific or randomly selected error response for an endpoint"
Expand All @@ -202,7 +202,7 @@
error-rsp (format-rsp (first error))]
(when (empty? errors) (log-warn "warning - no errors found for endpoint" [svc uri request-method]))
(when (empty? error) (log-warn "warning - sim extension error not described in codex" [svc uri request-method]))
(if (seq error) (ph/swap error-rsp *tree* {} :gen-all true) {:status x})))
(if (seq error) (ph/swap error-rsp *tree* (ph/response-bag *tree* error *request*) :gen-all true) {:status x})))
([] (error (Long. (name (first (rand-nth (d/error-status *tree*))))))))

(defn respond
Expand Down

0 comments on commit b8f9e08

Please sign in to comment.