liberator

2018-02-26T05:09:12.000130Z

Hey guys, so i'm trying to a template for a restful api, and i'm running into an issue involving the different resource handles

2018-02-26T05:10:42.000135Z

within my (defresource, I want to handle an unauthorized access, but I want to also respect the Accepts: datatype similar to how the :handle-ok handler works

2018-02-26T05:12:05.000106Z

Currently, if i set my handler as :handle-unauthorized (fn [_] {:type :error :message "unauthorized"}) it produces an internal error

2018-02-26T05:14:35.000191Z

err woops, it's a bit different, the function being sent is more like (fn [_] {:message (payload/error "Unauthorized Access" :type ::unauthorized-access)}) which generates a payload with keys :data, :data-type :payload-type and :extra

2018-02-26T05:15:12.000070Z

but this causes an arity error, my guess is the handler is trying to incorporate it within the context similar to the other handlers and actions

2018-02-26T05:22:51.000049Z

Looking over the, code it seems like handle-ok is implemented using defhandler, along with handle-unauthorized, yet the return of handle-ok acts completely different from the rest of the handlers. Am I missing something?

ordnungswidrig 2018-02-26T07:59:06.000369Z

@benzap this is a fundamental flaw with liberator’s response creation, because, when handle-unauthorized is called, the media-type is not yet negotiated. Issue #275 is related, https://github.com/clojure-liberator/liberator/issues/275

ordnungswidrig 2018-02-26T08:00:08.000471Z

In this comment I made a suggestion how to fix it: https://github.com/clojure-liberator/liberator/issues/215#issuecomment-103006363

ordnungswidrig 2018-02-26T08:03:13.000334Z

Another solution is define :as-response and wrap liberator.reponse/as-response and set a default media-type in the context:

(defresource hmmm
...
:as-response (fn [d ctx] (liberator.representation/as-response d (update-in ctx [:representation :media-type] #(or % "application/json"))))
...

2018-02-26T08:28:32.000109Z

@ordnungswidrig thanks, I ended up just returning a string, but now I guess i'll rethink it 🙂

ordnungswidrig 2018-02-26T08:33:21.000276Z

This issue is bugging me for a while, but I did not come up with a well designed fix yet. Maybe updating representation/as-response to fall-back to a default from :available-media-types but I’m not sure.

2018-02-26T08:56:03.000047Z

I made a small modification to your code to hold onto the already present Accept header, seems to be working

2018-02-26T08:59:51.000152Z

This sure isn't a well-designed fix, but it seems to be doing the trick lol

2018-02-26T09:00:18.000076Z

Regardless of that, everything else in liberator is pretty amazing, I don't think I could have thrown together a restful api this quickly

ordnungswidrig 2018-02-26T09:01:05.000320Z

Thanks! I really enjoy if people’s productivity increases.