The snaplet-rest package

[Tags: library, mit]

REST resources for the Snap framework.

As an example, let's translate the following datatype into a resource.

 data User = User Username String Int

 type Username = CI String

We need a type to represent changes to the resource. This partial type indicates what properties to change: either the name, the age, or both.

 data UserPart = UserPart (Maybe String) (Maybe Int)

This type also acts as a search mechanism: we can search by names, ages, or both. We can use either a username or a UserPart search to find users, and define a function to convert URL query string parameters to this search.

 type UserId = Either Username UserPart

 userIdFromParams :: Params -> Maybe UserId

Now we have the pieces required to define our CRUD behaviour.

 createUser :: User -> AppHandler ()

 readUser :: UserId -> AppHandler [User]

 updateUser :: UserId -> UserPart -> AppHandler Bool

 deleteUser :: UserId -> AppHandler Bool

If we've implemented Aeson instances, we can add JSON as a media format without having to define these manually. Once the behaviour is attached to the resource, it can be served in the handler.

 serveUser :: AppHandler ()
 serveUser = serveResource $ resource
     & addMedia jsonInstances
     & setCreate createUser
     & setRead readUser
     & setUpdate updateUser
     & setDelete deleteUser
     & setFromParams userIdFromParams

Properties

Version0.1.0
Dependenciesaeson (>=0.6.2 && <0.7), base (>=4.6.0 && <4.7), blaze-builder (>=0.3.1 && <0.4), bytestring (>=0.10.0 && <0.11), case-insensitive (>=1.0.0 && <1.1), http-media (>=0.1.0 && <0.2), lens (>=3.9.0 && <3.10), mtl (>=2.1.2 && <2.2), snap (>=0.13.0 && <0.14), snap-accept (>=0.1.0 && <0.2), snap-core (>=0.9.4 && <0.10), text (>=0.11.3 && <0.12), utf8-string (>=0.3.7 && <0.4), xmlhtml (>=0.2.3 && <0.3)
LicenseMIT
Copyright(c) 2013 Timothy Jones
AuthorTimothy Jones
MaintainerTimothy Jones <git@zimothy.com>
CategoryWeb
Home pagehttp://github.com/zimothy/snaplet-rest
Source repositoryhead: git clone git://github.com/zimothy/snaplet-rest.git
Upload dateSun Oct 20 11:59:58 UTC 2013
Uploaded byTimothyJones
Downloads101 total (8 in last 30 days)

Modules

Downloads

Maintainers' corner

For package maintainers and hackage trustees