snaplet-rest: REST resources for the Snap web framework

[ library, mit, web ] [ Propose Tags ] [ Report a vulnerability ]

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

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.0
Dependencies aeson (>=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) [details]
License MIT
Copyright (c) 2013 Timothy Jones
Author Timothy Jones
Maintainer Timothy Jones <git@zimothy.com>
Category Web
Home page http://github.com/zimothy/snaplet-rest
Source repo head: git clone git://github.com/zimothy/snaplet-rest.git
Uploaded by TimothyJones at 2013-10-20T11:59:58Z
Distributions
Reverse Dependencies 1 direct, 0 indirect [details]
Downloads 1064 total (1 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]