Idiomatic Means of Testing Preconditions

43 views
Skip to first unread message

Aleksander Eskilson

unread,
Jun 14, 2015, 3:01:48 AM6/14/15
to clojure-...@googlegroups.com
Hi there,

How should we structure resources to ensure a given allowed-method can be safely executed? For example, if our API was to provide for the creation of a new user in some database by means of a :put! function, but before beginning the transaction, we want to ensure that the given user does not already exist, is there a Liberator decision point we could use to check against the database to ensure the precondition and then return an appropriate response should the precondition fail? Or should that be handled as an exception on the database end of the code called by the :put! function?

Thanks,
Alek

Philipp Meier

unread,
Jun 29, 2015, 4:56:35 PM6/29/15
to clojure-...@googlegroups.com
Hi Alek,


Am Sonntag, 14. Juni 2015 09:01:48 UTC+2 schrieb Aleksander Eskilson:

How should we structure resources to ensure a given allowed-method can be safely executed? For example, if our API was to provide for the creation of a new user in some database by means of a :put! function, but before beginning the transaction, we want to ensure that the given user does not already exist, is there a Liberator decision point we could use to check against the database to ensure the precondition and then return an appropriate response should the precondition fail? Or should that be handled as an exception on the database end of the code called by the :put! function?

It depends a little bit on the use case.If you want to refuse the request because the user exists you can use `conflict?` to signal the existence.

-billy.

Aleksander Eskilson

unread,
Jul 2, 2015, 12:29:18 PM7/2/15
to clojure-...@googlegroups.com
Hi Philipp,

Thanks, that sounds reasonable. Could you describe then any use cases for the `existed?` condition? `conflict?` sounds like a great way to check general preconditions. In the case where some object already resides in something like a database, and we don't want to insert duplicates, is that something the `existed?` condition was included to allow us to check? 

Alek

Aleksander Eskilson

unread,
Jul 2, 2015, 12:35:01 PM7/2/15
to clojure-...@googlegroups.com
Sorry, I mean to say the `exists?` condition, not the `existed?` condition. 

Philipp Meier

unread,
Jul 3, 2015, 9:39:15 AM7/3/15
to clojure-...@googlegroups.com
Hi Alek,


Am Donnerstag, 2. Juli 2015 18:35:01 UTC+2 schrieb Aleksander Eskilson:
Sorry, I mean to say the `exists?` condition, not the `existed?` condition. 

On Thursday, July 2, 2015 at 11:29:18 AM UTC-5, Aleksander Eskilson wrote:
Hi Philipp,

Thanks, that sounds reasonable. Could you describe then any use cases for the `existed?` condition? `conflict?` sounds like a great way to check general preconditions. In the case where some object already resides in something like a database, and we don't want to insert duplicates, is that something the `existed?` condition was included to allow us to check? 


I depends on what should happen if a record already exists. HTTP offers the status 409 CONFLICT for that case which is triggered in liberator by the `conflict?` decision. If you want do ignore the "error" (idempotency like PUT suggests) you you simply return Accepted, or OK. The best is to have a look at the decision graph in this case and to find a way, and finally check that the declaraitons make sence (exists? return false only of the resource does not exist etc.)

-billy.
Reply all
Reply to author
Forward
0 new messages