Arbitrary actions on objects that are not standard CRUD operations.
Arbitrary actions on objects that are not standard CRUD operations. This is very flexible, and must be carefully used only for good (and not evil). Actions may have side effects and may not be idempotent.
Example:
def convertToHtml(ids: Seq[Int]) = Rest.action { ctx => val pages = store.multiGet(ids) val htmlIfied = pages.map { page => MarkdownEngine.toHtml(page) } store.multiSave(htmlIfied) Ok(htmlIfied) }
Underlying HTTP request (ids=1,2,3,5,8,13):
POST /api/myResource?action=convertToHtml&ids=1,2,3,5,8,13
Creates a new resource given an input.
Creates a new resource given an input.
Example:
def create = Rest.create { ctx => val newElement = createResourceFromBody(ctx.body) val newId = store.save(newOne) Ok(Keyed(newId, Some(newElement))) }
Underlying HTTP request (body elided):
POST /api/myResource
Deletes an element from a collection.
Deletes an element from a collection.
Example:
def delete(id: Int) = Rest.delete { ctx => store.delete(id) Ok() }
Underlying HTTP request (id=4):
DELETE /api/myResource/4
Retrieval by any method other than retrival by Id [primary key].
Retrieval by any method other than retrival by Id [primary key]. (For example, alternate key lookup, or full text search. This is intentionally a flexible API and can be used appropriately in many contexts. A finder MUST NOT have side effects, and must be idempotent.
Example:
def alternateKey(name: String) = Rest.finder { ctx => val results = store.lookupByName(name=name) Ok(results.toSeq) }
Underlying HTTP request (finder name='alternateKey', name='hogwarts'):
GET /api/myResource?q=alternateKey&name=hogwarts
Gets all elements in a collection.
Gets all elements in a collection. Note: please use paging to avoid OOM-ing.
Example:
def getAll = Rest.getAll { ctx => val results = store.getAll(start=ctx.paging.start, limit=ctx.paging.limit) Ok(results) }
Underlying HTTP request (pagination: start=10, limit=5)
GET /api/myResource?start=10&limit=5
Gets a resource by ID
Gets a resource by ID
Example:
def get(id: Int) = Rest.get { ctx => Ok(Keyed(id, MyResource(name=s"Resource-$id"))) }
Underlying HTTP request (id = "1"):
GET /api/myResource/1
Gets a batch of resources from this collection.
Gets a batch of resources from this collection.
Example:
def multiGet(ids: Seq[Int]) = Rest.multiGet { ctx => Ok(ids.map(id => Keyed(id, MyResource(name=s"Resource-$id")))) }
Underlying HTTP request (ids = "1,2,3,4")
GET /api/myResource?ids=1,2,3,4
Patch (or partial update) a resource.
Patch (or partial update) a resource.
Example:
def patch(id: Int) = Rest.patch { ctx => val oldObj = store.get(id) val newObj = PatchEngine.patch(oldObj, ctx.body) store.save(id, newObj) Ok(Keyed(id, newObj)) }
Underlying HTTP request (id=10, body elided):
PATCH /api/myResource/10
Updates a resource with a new copy of the resource.
Updates a resource with a new copy of the resource.
Example:
def update(id: Int) = Rest.update { ctx => val newVersion = validateBody(ctx.body) store.save(id, newVersion) Ok(Keyed(id, newVersion)) }
Underlying HTTP request (body elided, id=2):
PUT /api/myResource/2
Set the authentication framework.
Set the body type.
Adds an error handling function to allow exceptions to generate custom errors.
Adds an error handling function to allow exceptions to generate custom errors.
Note: all of the partial functions are stacked, with later functions getting an earlier crack at an exception to handle it.
Error handling partial function.
the immutable RestActionBuilder to be used to build the naptime resource action.
Set the response type.
Set the response type. TODO: is this necessary?
A builder that helps build Rest Actions.