classdojo / crisco

MIT License
0 stars 0 forks source link

Crisco should allow clients to define batch operations (get/update) for a collection of resources #23

Open cdolivares opened 11 years ago

cdolivares commented 11 years ago

Something like

exports.plugin = (crisco) ->

  BaseResource = crisco.BaseResource
  class aResource extends BaseResource

    @before
      authenticate: 'all'

    #typical RESTful routes
    @app.get "/apples/:apple_id/oranges/:orange_id/grapes", (CriscoModels, Aux, next) ->
      next(true)

    @app.getOne "/apples/:apple_id/oranges/:orange_id/grapes/:grape_id", (CriscoModels, Aux, next) ->
      next(true)

    @app.put "/apples/:apple_id/oranges/:orange_id/grapes/:grape_id", (CriscoModels, Aux, next) ->
      next(true)

    @app.post "/apples/:apple_id/oranges/:orange_id/grapes", (CriscoModels, Aux, next) ->
      next(true)

    #batch routes created flagging route as type "batch"
    @type "batch"
    @app.get "/apples/:apple_id/grapes", (CriscoModels, Aux, next) ->
      #gets all grapes for all oranges in apple_id context
      next(true)

    @type "batch"
    @app.put "/apples/:apple_id/grapes", (CriscoModels, Aux, next) ->
      #will accept some batch compliant body.
      next(true)
cdolivares commented 11 years ago

Sample Request

curl -XPUT /apples/:apple_id/grapes -H 'Expect: type="sync"' -d '{
  data: [
    {"grape": "1"},
    {"grape": "2"}
  ]
}'

Response

200
{
  data: {
    success: [],
    error: []
  }
}

or as an asynchronous polling request

curl -XPUT /apples/:apple_id/grapes -H 'Expect: type="async"' -d '{
  data: [
    {"grape": "1"},
    {"grape": "2"}
  ]
}'

Response

100
-H "Location: http://example.com/url/to/poll

Then client periodically

GET http://example.com/url/to/poll

Server reply on request not ready

202

Server reply on request ready

200
{
  data: {
    success: [],
    error: []
  }
}
cdolivares commented 11 years ago

The asynchronous protocol misappropriates the 100 status code. Both protocols slightly misuse the Expect header as well. Is there a better fit?