yada

bradford 2017-11-07T22:18:59.000069Z

Hi fam! I'm having a bit of trouble understanding the syntax for nested, complex routes. I read through the bidi docs but it doesn't seem to quite mesh with the yada examples I've seen. Could I get some advice on how to get this right?

bradford 2017-11-07T22:19:02.000084Z

(defn get-email-campaign [ctx] (print "Get campaign"))

(defn create-email-campaign [ctx] (print "create campaign"))

(defn list-email-campaigns [ctx] (print "list campaigns"))

(defn update-email-campaign [ctx] (print "update campaign"))

(def campaign-item-resource
  (yada/resource
   {:parameters {:path {:clientid String
                        :userid String
                        :campaignid String}}
    :produces   ["application/json"]
    :methods
    {:get  {:response get-email-campaign}
     :put {:parameters {:body {:name String
                               :country String}}
           :consumes   "application/json"
           :response   update-email-campaign}}}))

(def campaigns-resource
  (yada/resource
   {:parameters {:path {:clientid String
                        :userid String}}
    :produces   ["application/json"]
    :methods
    {:get  {:response list-email-campaigns}
     :post {:parameters {:body {:name String
                                :country String}}
            :consumes   "application/json"
            :response   create-email-campaign}}}))

(defn server-routes []
  [""
   [["/about" (yada/as-resource "Hello World")]

    ["/clients"
     [[["/" :clientid
        [[["/users"
           [[["/" :userid
              [[["/campaigns"
                 ["/" :campaignid
                  campaign-item-resource]]
                campaigns-resource]]]]]]]]]]]]
    ["/phone"
     [[["/" :number phone-resource]]]]]])

bradford 2017-11-07T22:21:40.000054Z

the error I get (but I know my route syntax is wrong) is

clojure.lang.ExceptionInfo: If a PatternSegment is represented by a vector, the second
                               element must be the keyword associated with the pattern: [[["/users" [[["/" :userid [[["/campaigns"] #yada.resource.Resource{:produces [{:media-type #yada.media_type.MediaTypeMap{:name "application/json", :type "application", :subtype "json", :parameters {}, :quality 1.0}}], :methods {:get {:response #object[smolder_admin_backend.server$get_email_campaign 0x761c3d5f "smolder_admin_backend.server$get_email_campaign@761c3d5f"]}, :post {:response #object[smolder_admin_backend.server$create_email_campaign 0x2d3c2e63 "smolder_admin_backend.server$create_email_campaign@2d3c2e63"], :consumes [{:media-type #yada.media_type.MediaTypeMap{:name "application/json", :type "application", :subtype "json", :parameters {}, :quality 1.0}}], :parameters {:body {:name java.lang.String, :country java.lang.String}}}}, :show-stack-traces? true, :parameters {:path {:clientid java.lang.String}}}]]]]]]]] {}

bradford 2017-11-07T23:16:23.000025Z

Got it! This is way cool.

(def my-routes ["/" {"about"    hello-resource
                     "clients/" {[:clientid "/"]
                                 {"users/" {[:userid "/"]
                                            {"campaigns" {["/" :campaignid] {#"/|" campaign-item-resource}
                                                          #"/|" campaigns-resource}}}}}
                     "phone/"   {[:number ""] phone-resource}}])

bradford 2017-11-07T23:16:38.000147Z

Dealing with potential trailing slashes is a bit weird, but at least intuitive