Fork me on GitHub
#yada
<
2017-03-31
>
korny08:03:52

Does Yada have some way to use Json schemas, or is there an easy way to convert Json schema to p[ris|lu]matic schema? I found https://github.com/joelittlejohn/js-ps which might do the trick, but be interested if there are other options

dominicm08:03:28

I think schema can convert to json schema, yeah

korny08:03:08

it's the other way round I'm looking for - I have an API I want to emulate, which is specified by a JSON schema, I want to plug that into a yada endpoint. I could actually convert it manually, but it'd be nice to slurp in the json schema and use that to validate...

borkdude09:03:11

I need to pass additional information to my yada.security/verify defmethod (configuration data). How do I do this?

dominicm09:03:00

@borkdude additional keys on the resource

borkdude10:03:04

@dominicm I’m not sure where to put it. If I put an extra key on the resource, I get a schema error. Also when I put it on the :access-controlmap

borkdude10:03:49

I can abuse the verify key to stick a map in there, but it feels like a hack

dominicm10:03:20

You have to namespace keys

dominicm10:03:27

:myapp/verify-config

imre10:03:32

https://clojurians.slack.com/archives/C0702A7SB/p1490955080102536 this should be in all caps bold at every page of the yada manual

borkdude10:03:09

I now have the following resource:

(yada/resource
                   {:dre/auth-cfg auth-cfg
                    :access-control
                    {:scheme “CUSTOM"
                     :authorization
                     {:methods
                      {:get
                       [:or
                        "SuperAdmin"
                        “NormalUser"]}}}
                    :methods {:get {:produces "text/html"
                                    :response (fn [ctx]
                                                "hello")}}
                    :responses {403
                                {:response
                                 sso-redirect}}})
I want this for most of my resources. Just write a function which merges these properties in a yada-resource, or is there some other way to avoid duplication?

borkdude10:03:29

As auth is not route based in Yada, this is question I have compared to other frameworks

malcolmsparks10:03:11

Tree walking with clojure.walk/postwalk is very useful

borkdude10:03:14

I now have this:

(yada/resource
                   (merge (with-default-auth auth-cfg)
                          {:methods {:get {:produces "text/html"
                                           :response (fn [ctx]
                                                       “hello”)}}}))
Looks better already.

borkdude11:03:17

Any examples of the post walk alternative somewhere @malcolmsparks ?

borkdude11:03:43

I’m already content with this solution though. I just write a wrapper for yada/resource which fills in the default auth details.

borkdude12:03:07

Can this be abbreviated or does Yada require a set of roles for each method?

:access-control
     {:scheme "DRE"
      :authorization
      {:methods
       {:get
        [:or
         "SuperAdmin"
         "DataExtractor"
         "DataHub"]
        :post
        [:or
         "SuperAdmin"
         "DataExtractor"
         "DataHub"]
        :put
        [:or
         "SuperAdmin"
         "DataExtractor"
         "DataHub"]
        :delete
        [:or
         "SuperAdmin"
         "DataExtractor"
         “DataHub”]}}}

dominicm12:03:22

@borkdude if you want to avoid repitition youcan: - postwalk the bidi route structure - use a custom (myapp/resource) which adds parameters as appropriate - define a custom access-control thing in the multi-method

borkdude14:03:31

More idiomatic Yada for this one?

(fn [ctx]
                           (let [user-id (get-user-id ctx)
                                 conn (:connection database)
                                 uuid (get-in ctx [:parameters :path :uuid])]
                             (let [n (db/delete-signal! conn user-id uuid)]
                               (if (pos? n)
                                 (assoc (:response ctx) :status 200)
                                 (assoc (:response ctx) :status 404)
                                 ))))
(the lines with assoc)

borkdude14:03:53

so I want to return a 200 when something has been updated, 404 when nothing has been updated

mpenet14:03:23

put the "if" at the :status value level

mpenet14:03:37

no need to repeat (assoc (:response ctx) .. etc

borkdude14:03:00

oh yes, that’s a good one, but I was wondering actually if Yada has something less low level for 404 and 200

dominicm15:03:57

if you return nil, that's a 404

dominicm15:03:25

(when (pos? n) "")

borkdude15:03:19

hmm, I get a 204 for nil

borkdude15:03:37

(serving json)

borkdude15:03:10

the 200 for the empty string works though, but it feels a bit weird because normally I return just clojure data

borkdude15:03:52

Hmm, this returns a 404:

(yada/response-for (yada/resource {:produces "application/json"
                                     :response (fn [ctx] nil)}))

borkdude15:03:11

but it has context/type text/plain

borkdude15:03:56

Trying this:

(yada/response-for
   (yada/resource {:produces "application/json"
                   :response (fn [ctx] nil)})
   :get
   "/"
   {:headers {"Accept" “application/json”}})
Still text/plain.

borkdude15:03:38

Maybe the HTTP spec says so?

borkdude17:03:45

What should I return when I want an async handler?

borkdude17:03:01

Sorry for flooding this channel in the last 24 hours btw and thanks for helping me get started 😄

malcolmsparks18:03:22

Or a stream. In the yada training earlier this week we returned manifold subscriptions of an event bus ties to a Datomic tx-report-queue

malcolmsparks18:03:39

manifold.stream/map a #(str "data: " % "\n\n") and produce text/event-stream and you have a browsable real time view of your Datomic db. Just works, much more thanks to @ztellman than I

malcolmsparks18:03:56

No apologies for flooding this channel- that's what it's for. Stick at it, eventually yada becomes easier.

borkdude21:03:13

I have a resource like this:

{
    :methods
    {:post
     {:response (fn [ctx]
                  (let [res (d/deferred)]
                    (manifold.time/in 5000
                           #(d/success! res {:a 10}))
                    res))}}}
When I fire 20 requests and then request an image in another browser tab img/icon.png (served from the classpath), it’s still pending for quite some time. Not sure what’s going on.