2

I have an app that generates keys based for specific datastores. If there are 7 such routes and 5 apps, there would be a total of 35 routes in my event.clj (or, if split out, 7/file in 5 files). I would like to do the following dynamically:

(defnested "/:app-name"
    (defpage "/generate/event" {:keys [app-name event-name time] :as key-map}
     (response/json
      {:key (key-model/build-key :event key-map)}))

    (defpage "/generate/event/unread" {:keys [app-name event-name] :as key-map}
     (response/json
      {:key (key-model/build-key :unread-for-event key-map)}))
)

That way I can write each route once and then pass around the app-name (instead of passing it in the query params, which works but isn't very RESTful.

BONUS

How can I dynamically call a namespace so that key-model/build-key becomes a call to redis-model/build-key or riak-model/build-key based on the app name?

Chris
  • 11,819
  • 19
  • 91
  • 145

1 Answers1

2

Not sure if I understand the question but isn't this what your are looking for:

(defpage "/:app-name/generate/event" {:keys [app-name event-name time] :as key-map}
    (response/json
      {:key (key-model/build-key :event key-map)}))

(defpage "/:app-name/generate/event/unread" {:keys [app-name event-name] :as key-map}
     (response/json
      {:key (key-model/build-key :unread-for-event key-map)}))

BONUS

I will also go for a simple solution of having a map with the funcitons to generate the keys, something like:

(def key-gen {"redis" redis-model/build-key
       "riak" riak/build-key})

(response/json
      {:key ((get key-gen app-name) :event key-map)})

If you really want to dynamically find the build-key function, you can do something like:

(defn build-key [app-name] 
   (let [the-ns (symbol (str app-name "-model"))] 
      (require the-ns) 
      (ns-resolve the-ns 'build-key)))
DanLebrero
  • 8,545
  • 1
  • 29
  • 30
  • yep I can do that for each of the routes, but I'm looking for a better solution for the method calling (so someone can add a backend without worrying about adding it to a hash-map in the routes) Though using `get` would allow for a default value to return a 404 or 422 – Chris Sep 28 '12 at 20:00
  • updated answer. You can still return 404 or 422 if the `the-ns` or the return value is nil – DanLebrero Sep 28 '12 at 20:52
  • This is just the kind of crazy I'm looking for ;) – Chris Oct 01 '12 at 12:59