scotty-rest-0.1.0.0: Webmachine-style REST library for scotty

Safe HaskellNone
LanguageHaskell2010

Web.Scotty.Rest

Contents

Synopsis

REST handler to Scotty

rest :: MonadIO m => RoutePattern -> Config m -> ScottyT RestException m () Source

rest is used where you would use e.g. get in your Scotty app, and will match any method:

main = scotty 3000 $ do
  get  "/foo" (text "Hello!")
  rest "/bar" defaultConfig {
      contentTypesProvided = return [("text/html",html "Hello, World!")]
    }

Callback result types

data Authorized Source

Response to isAuthorized callback.

Constructors

Authorized

User is authenticated and authorized.

NotAuthorized !Text

User is not authorized. The given challenge will be sent as part of the WWW-Authenticate header.

data DeleteResult Source

Response to deleteResource callback.

Constructors

NotDeleted

The resource could not be deleted.

Deleted

The deletion operation has been fully completed.

DeletedWithResponse !MediaType !Text

The deletion operation has been fully completed. Respond with the given body.

DeleteEnacted

Accepted for processing, but the processing has not been completed (and may never be).

data ETag Source

An ETag validator generated by the generateEtag callback.

Constructors

Strong Text

A strong ETag validator

Weak Text

A weak ETag validator

data Moved Source

Result of the resourceMoved callback.

Constructors

NotMoved

Resource has not moved.

MovedTemporarily !Url

Resource has been temporarily moved to the given URL.

MovedPermanently !Url

Resource has been permanently moved to the given URL.

data ProcessingResult Source

Result of a Processing request (e.g. POST, PUT, PATCH)

Constructors

Succeeded

Processing succeeded. Returns 204 No Content or 201 Created based on whether the resource existed previously or not.

SucceededWithContent !MediaType !Text

Processing succeeded with the given content. Returns 201 Created or 200 OK/300 Multiple Representations based on whether the resource existed previously or not.

SucceededWithLocation !Url

Processing succeeded with the given content. Returns 201 Created or 200 OK/300 Multiple Representations based on whether the resource existed previously or not.

Redirect !Url

Redirects (with 303 See Other) to the given URL.

Failed

Processing failed. Returns 400 Bad Request.

data Representation Source

Response to multipleChoices callback.

Constructors

UniqueRepresentation

The resource has a unique representation.

MultipleRepresentations !MediaType !Text

There are multiple representations of this resource. Respond with the given body.

MultipleWithPreferred !MediaType !Text !Url

There are multiple representations of this resource, but one is preferred. Respond with the given body, and Url (in the Location header).

Config

data RestConfig m Source

The callbacks that control a handler's behaviour. defaultConfig returns a config with default values. For typical handlers, you only need to override a few of these callbacks.

Constructors

RestConfig 

Fields

allowedMethods :: m [StdMethod]

List of allowed methos.

Default: [GET, HEAD, OPTIONS]

resourceExists :: m Bool

Does this resource exist?

Default: True

previouslyExisted :: m Bool

Did this resource exist previously?

Default: False

isConflict :: m Bool

Only for PUT requests. Does this request result in a conflict?

Default: False

contentTypesAccepted :: m [(MediaType, m ProcessingResult)]

A list of the content types accepted, together with handlers producing a ProcessingResult for that MediaType.

Default: []

contentTypesProvided :: m [(MediaType, m ())]

A list of the content types provided, together with handlers producing a response body for that MediaType.

Default: []

languagesProvided :: m (Maybe [Language])

A list of the languages provided in order of preference. If the `Accept-Language` header was not sent, the first charset is selected. If Nothing, the `Accept-Language` header is ignored.

Default: Nothing

charsetsProvided :: m (Maybe [Text])

A list of the character sets provided in order of preference. If the `Accept-Charset` header was not sent, the first charset is selected. If Nothing, the `Accept-Charset` header is ignored.

Default: Nothing

deleteResource :: m DeleteResult

A handler for DELETE requests. The handler is responsible for deleting the resource and returning a DeleteResult.

Default: NotDeleted

optionsHandler :: m (Maybe (MediaType, m ()))

A handler for OPTIONS requests. If implemented, the handler will be run (possibly producing a body), and the given media type will be sent in the `Content-Type` header. When Nothing, the Allow header will be sent with the allowed methods (from allowedMethods).

Default: Nothing

generateEtag :: m (Maybe ETag)

An ETag for the resource.

Default: Nothing

expires :: m (Maybe UTCTime)

When the resource expires (if ever). This will be sent in the expires header.

Default: Nothing

lastModified :: m (Maybe UTCTime)

When the resource was last modified.

Default: Nothing

malformedRequest :: m Bool

If True, the request is considered malformed and a 400 Bad Request is returned.

Default: False

isAuthorized :: m Authorized

Is authentication is required, and has it failed or has not been provided?

Default: Authorized

forbidden :: m Bool

If True, access to this resource is forbidden, and 403 Forbidden is returned.

Default: False.

serviceAvailable :: m Bool

Is the service available?

Default True

allowMissingPost :: m Bool

Do we allow POSTing to this resource if it does not exist?

Default True

multipleChoices :: m Representation

Are there multiple representations for this resource?

Default: UniqueRepresentation

resourceMoved :: m Moved

Has this resource moved?

Default: NotMoved

variances :: m [Text]

Returns a list of header names that should be included in a given response's Vary header. The standard content negotiation headers (Accept, Accept-Encoding, Accept-Charset, Accept-Language) do not need to be specified here as they will be added automatically when e.g. several content types are provided.

Default: []

Instances

defaultConfig :: MonadIO m => Config m Source

A RestConfig with default values. To override one or more fields, use record syntax:

defaultConfig {
  contentTypesProvided = return [("text/html",html "Hello, World!")]
}

Rest Exceptions

Re-exports

data MediaType :: *

An HTTP media type, consisting of the type, subtype, and parameters.

data StdMethod :: *

HTTP standard method (as defined by RFC 2616, and PATCH which is defined by RFC 5789).

Constructors

GET 
POST 
HEAD 
PUT 
DELETE 
TRACE 
CONNECT 
OPTIONS 
PATCH 

Utilities

toHttpDateHeader :: UTCTime -> Text Source

Formats a UTCTime as a HTTP date, e.g. Sun, 06 Nov 1994 08:49:37 GMT.