how to handle the filter like "base/fruits?name='apple'" with the liberator

25 views
Skip to first unread message

harve...@gmail.com

unread,
Jan 21, 2018, 7:55:51 AM1/21/18
to Liberator
I want to supply the feature of filter with my rest api. However, I feel confused to settle the place to do that.

Philipp Meier

unread,
Jan 21, 2018, 2:47:04 PM1/21/18
to Liberator

Am Sonntag, 21. Januar 2018 13:55:51 UTC+1 schrieb harve...@gmail.com:
I want to supply the feature of filter with my rest api. However, I feel confused to settle the place to do that.

I'm very sorry but I don't understand what you want to do. Are you talking about filtering a list of resulsts, like from db query?

Cheers,
-billy. 

harve...@gmail.com

unread,
Jan 22, 2018, 2:45:21 AM1/22/18
to Liberator
thank u for reply.

Here is my scenario:
[ring.middleware.params :refer [wrap-params]]

(def handler
(-> app
wrap-params
;; (wrap-trace :header :ui)
))

(defroutes app
(ANY "/forms/:id" [id] (form-item id))
(ANY "/forms" [] form-collection)
)

(defresource form-item [id]
:allowed-methods [:get :put :delete]
:available-media-types ["application/json"]
:last-modified #(:created_at (:res %))
:exists? #(get-form-by id %)
:handle-ok #(generate-string (:res %))
:can-put-to-missing? false
:delete! #(delete!-form-by id %)
)

(defresource form-collection
:allowed-methods [:get :post]
:available-media-types ["application/json"]
:exists? #(get-forms-all %)
:post! #(insert!-form %)
:handle-ok #(generate-string (:res %))
)

I want to add a resource like /forms?name='happy'&age='20'

How do I handle the query parameter in the url with the liberator context.

I know that the wrap-params of the ring will handle the query-params, but how does the liberator get that?

在 2018年1月22日星期一 UTC+8上午3:47:04,Philipp Meier写道:

Philipp Meier

unread,
Jan 22, 2018, 3:53:17 AM1/22/18
to Liberator


Am Montag, 22. Januar 2018 08:45:21 UTC+1 schrieb harve...@gmail.com:


(defresource form-collection
:allowed-methods [:get :post]
:available-media-types ["application/json"]
:exists? #(get-forms-all %)
:post! #(insert!-form %)
:handle-ok #(generate-string (:res %))
)

I want to add a resource like /forms?name='happy'&age='20'

How do I handle the query parameter in the url with the liberator context.

I know that the wrap-params of the ring will handle the query-params, but how does the liberator get that?

The important thing is that the original ring request is available in the context at the key :request. The context is
passed to every decision function.

You can see an example in this part of the liberator tutorial (reight below the decision graph).

-billy.

Sean Johnson

unread,
Jan 24, 2018, 4:04:03 PM1/24/18
to Liberator
If you want to see a live example of using query params in a Liberator resource, in addition to the rest of the URL, here's a live example:

https://github.com/open-company/open-company-storage/blob/mainline/src/oc/storage/api/activity.clj#L122

https://github.com/open-company/open-company-storage/blob/mainline/src/oc/storage/api/activity.clj#L144

It's taking a `direction` and start from the URL, e.g.`...?direction=around&start=2018-01-01`

Also notice when the Compojure routes are setup, the wrap-params ring middleware is used: https://github.com/open-company/open-company-storage/blob/mainline/src/oc/storage/app.clj#L75

Cheers,
Sean
Reply all
Reply to author
Forward
0 new messages