The snaplet-rest package

[ Tags: library, mit, web ] [ Propose Tags ]

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

Versions 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 repository head: git clone git://github.com/zimothy/snaplet-rest.git
Uploaded Sun Oct 20 11:59:58 UTC 2013 by TimothyJones
Distributions NixOS:0.1.0
Downloads 325 total (2 in the last 30 days)
Rating 0.0 (0 ratings) [clear rating]
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]
Hackage Matrix CI

Modules

[Index]

Downloads

Maintainer's Corner

For package maintainers and hackage trustees