webapi-0.2.0.0: WAI based library for web api

LicenseBSD3
Stabilityexperimental
Safe HaskellNone
LanguageHaskell2010

WebApi.Param

Contents

Description

Param serialization and deserialization. ToParam and EncodeParam are responsible for serialization part. EncodeParam converts the value into a wire format. ToParam is responsible for creating (nested) key value pairs, which can be then used to deserialize to original type. For example

encodeParam 5 == "5"

data Foo = Foo { foo :: Int }
         deriving (Show, Eq, Generic)

data Bar = Bar { bar :: Foo }
         deriving (Show, Eq, Generic)

instance ToParam Foo 'FormParam
instance ToParam Bar 'FormParam

toParam (Proxy :: Proxy 'FormParam) "" (Bar (Foo 5)) == [("bar.foo","5")]

Deserialization works analogously, FromParam and DecodeParam are counterparts to ToParam and EncodeParam respectively. Generic instances are provided for all of them. This means that the user only need to derive Generic in their type, and provide instance with an empty body. Note that for headers FromHeader and ToHeader is being used in place of FromParam and ToParam. Nesting is not supported for headers.

Synopsis

Serialization

class ToParam a parK where Source

Serialize a type to a given type of kind ParamK.

Minimal complete definition

Nothing

Methods

toParam :: Proxy (parK :: ParamK) -> ByteString -> a -> [SerializedData parK] Source

Instances

ToParam Bool QueryParam Source 
ToParam Bool FormParam Source 
ToParam Bool PathParam Source 
ToParam Bool Cookie Source 
ToParam Char QueryParam Source 
ToParam Char FormParam Source 
ToParam Char PathParam Source 
ToParam Char Cookie Source 
ToParam Double QueryParam Source 
ToParam Double FormParam Source 
ToParam Double PathParam Source 
ToParam Double Cookie Source 
ToParam Float QueryParam Source 
ToParam Float FormParam Source 
ToParam Float PathParam Source 
ToParam Float Cookie Source 
ToParam Int QueryParam Source 
ToParam Int FormParam Source 
ToParam Int PathParam Source 
ToParam Int Cookie Source 
ToParam Int8 QueryParam Source 
ToParam Int8 FormParam Source 
ToParam Int8 PathParam Source 
ToParam Int8 Cookie Source 
ToParam Int16 QueryParam Source 
ToParam Int16 FormParam Source 
ToParam Int16 PathParam Source 
ToParam Int16 Cookie Source 
ToParam Int32 QueryParam Source 
ToParam Int32 FormParam Source 
ToParam Int32 PathParam Source 
ToParam Int32 Cookie Source 
ToParam Int64 QueryParam Source 
ToParam Int64 FormParam Source 
ToParam Int64 PathParam Source 
ToParam Int64 Cookie Source 
ToParam Integer QueryParam Source 
ToParam Integer FormParam Source 
ToParam Integer PathParam Source 
ToParam Integer Cookie Source 
ToParam Word QueryParam Source 
ToParam Word FormParam Source 
ToParam Word PathParam Source 
ToParam Word Cookie Source 
ToParam Word8 QueryParam Source 
ToParam Word8 FormParam Source 
ToParam Word8 PathParam Source 
ToParam Word8 Cookie Source 
ToParam Word16 QueryParam Source 
ToParam Word16 FormParam Source 
ToParam Word16 PathParam Source 
ToParam Word16 Cookie Source 
ToParam Word32 QueryParam Source 
ToParam Word32 FormParam Source 
ToParam Word32 PathParam Source 
ToParam Word32 Cookie Source 
ToParam Word64 QueryParam Source 
ToParam Word64 FormParam Source 
ToParam Word64 PathParam Source 
ToParam Word64 Cookie Source 
ToParam () parK Source 
ToParam ByteString QueryParam Source 
ToParam ByteString FormParam Source 
ToParam ByteString PathParam Source 
ToParam ByteString Cookie Source 
ToParam Text QueryParam Source 
ToParam Text FormParam Source 
ToParam Text PathParam Source 
ToParam Text Cookie Source 
ToParam UTCTime QueryParam Source 
ToParam UTCTime FormParam Source 
ToParam UTCTime PathParam Source 
ToParam UTCTime Cookie Source 
ToParam Day QueryParam Source 
ToParam Day FormParam Source 
ToParam Day PathParam Source 
ToParam Day Cookie Source 
ToParam FileInfo FileParam Source 
ToParam a par => ToParam [a] par Source 
ToParam a par => ToParam (Maybe a) par Source 
ToParam a par => ToParam (Vector a) par Source 
EncodeParam a => ToParam (NonNested a) Cookie Source 
EncodeParam a => ToParam (NonNested a) FormParam Source 
EncodeParam a => ToParam (NonNested a) QueryParam Source 
ToJSON a => ToParam (JsonOf a) PathParam Source 
(ToJSON a, FromJSON a) => ToParam (JsonOf a) Cookie Source 
(ToJSON a, FromJSON a) => ToParam (JsonOf a) FormParam Source 
(ToJSON a, FromJSON a) => ToParam (JsonOf a) QueryParam Source 
EncodeParam a => ToParam (OptValue a) Cookie Source 
EncodeParam a => ToParam (OptValue a) FormParam Source 
EncodeParam a => ToParam (OptValue a) QueryParam Source 
(ToParam a par, ToParam b par) => ToParam (Either a b) par Source 
(EncodeParam a, EncodeParam b) => ToParam (a, b) PathParam Source 
(EncodeParam a, EncodeParam b, EncodeParam c) => ToParam (a, b, c) PathParam Source 
(EncodeParam a, EncodeParam b, EncodeParam c, EncodeParam d) => ToParam (a, b, c, d) PathParam Source 
(EncodeParam a, EncodeParam b, EncodeParam c, EncodeParam d, EncodeParam e) => ToParam (a, b, c, d, e) PathParam Source 
(EncodeParam a, EncodeParam b, EncodeParam c, EncodeParam d, EncodeParam e, EncodeParam f) => ToParam (a, b, c, d, e, f) PathParam Source 
(EncodeParam a, EncodeParam b, EncodeParam c, EncodeParam d, EncodeParam e, EncodeParam f, EncodeParam g, EncodeParam h) => ToParam (a, b, c, d, e, f, g, h) PathParam Source 
(EncodeParam a, EncodeParam b, EncodeParam c, EncodeParam d, EncodeParam e, EncodeParam f, EncodeParam g, EncodeParam h, EncodeParam i) => ToParam (a, b, c, d, e, f, g, h, i) PathParam Source 
(EncodeParam a, EncodeParam b, EncodeParam c, EncodeParam d, EncodeParam e, EncodeParam f, EncodeParam g, EncodeParam h, EncodeParam i, EncodeParam j) => ToParam (a, b, c, d, e, f, g, h, i, j) PathParam Source 

class ToHeader a where Source

Serialize a type to the header params

Minimal complete definition

Nothing

Methods

toHeader :: a -> [Header] Source

Instances

toQueryParam :: ToParam a QueryParam => a -> Query Source

Serialize a type into query params.

toFormParam :: ToParam a FormParam => a -> [(ByteString, ByteString)] Source

Serialize a type into form params.

toFileParam :: ToParam a FileParam => a -> [(ByteString, FileInfo FilePath)] Source

Serialize a type into file params.

toPathParam :: ToParam a PathParam => a -> [ByteString] Source

Serialize a type into path params.

toCookie :: ToParam a Cookie => a -> [(ByteString, ByteString)] Source

Serialize a type into cookie.

toNonNestedParam :: (ToParam (NonNested a) parK, EncodeParam a) => Proxy (parK :: ParamK) -> ByteString -> a -> [SerializedData parK] Source

Serialize a type without nesting.

Deserialization

class FromParam a parK where Source

(Try to) Deserialize a type from a given type of kind ParamK.

Minimal complete definition

Nothing

Instances

FromParam Bool QueryParam Source 
FromParam Bool FormParam Source 
FromParam Bool Cookie Source 
FromParam Char QueryParam Source 
FromParam Char FormParam Source 
FromParam Char Cookie Source 
FromParam Double QueryParam Source 
FromParam Double FormParam Source 
FromParam Double Cookie Source 
FromParam Float QueryParam Source 
FromParam Float FormParam Source 
FromParam Float Cookie Source 
FromParam Int QueryParam Source 
FromParam Int FormParam Source 
FromParam Int Cookie Source 
FromParam Int8 QueryParam Source 
FromParam Int8 FormParam Source 
FromParam Int8 Cookie Source 
FromParam Int16 QueryParam Source 
FromParam Int16 FormParam Source 
FromParam Int16 Cookie Source 
FromParam Int32 QueryParam Source 
FromParam Int32 FormParam Source 
FromParam Int32 Cookie Source 
FromParam Int64 QueryParam Source 
FromParam Int64 FormParam Source 
FromParam Int64 Cookie Source 
FromParam Integer QueryParam Source 
FromParam Integer FormParam Source 
FromParam Integer Cookie Source 
FromParam Word QueryParam Source 
FromParam Word FormParam Source 
FromParam Word Cookie Source 
FromParam Word8 QueryParam Source 
FromParam Word8 FormParam Source 
FromParam Word8 Cookie Source 
FromParam Word16 QueryParam Source 
FromParam Word16 FormParam Source 
FromParam Word16 Cookie Source 
FromParam Word32 QueryParam Source 
FromParam Word32 FormParam Source 
FromParam Word32 Cookie Source 
FromParam Word64 QueryParam Source 
FromParam Word64 FormParam Source 
FromParam Word64 Cookie Source 
FromParam () parK Source 
FromParam ByteString QueryParam Source 
FromParam ByteString FormParam Source 
FromParam ByteString Cookie Source 
FromParam Text QueryParam Source 
FromParam Text FormParam Source 
FromParam Text Cookie Source 
FromParam UTCTime QueryParam Source 
FromParam UTCTime FormParam Source 
FromParam UTCTime Cookie Source 
FromParam Day QueryParam Source 
FromParam Day FormParam Source 
FromParam Day Cookie Source 
FromParam FileInfo FileParam Source 
(Show (DeSerializedData par), FromParam a par) => FromParam [a] par Source 
FromParam a par => FromParam (Maybe a) par Source 
(Show (DeSerializedData par), FromParam a par) => FromParam (Vector a) par Source 
(DecodeParam a, Typeable * a) => FromParam (NonNested a) Cookie Source 
(DecodeParam a, Typeable * a) => FromParam (NonNested a) FormParam Source 
(DecodeParam a, Typeable * a) => FromParam (NonNested a) QueryParam Source 
DecodeParam a => FromParam (OptValue a) Cookie Source 
DecodeParam a => FromParam (OptValue a) FormParam Source 
DecodeParam a => FromParam (OptValue a) QueryParam Source 
(FromParam a par, FromParam b par) => FromParam (Either a b) par Source 

class FromHeader a where Source

(Try to) Deserialize a type from the header params

Minimal complete definition

Nothing

Instances

newtype Validation e a Source

Datatype representing the parsed result of params.

Constructors

Validation 

Fields

getValidation :: Either e a
 

Instances

data ParamErr Source

Errors that occured during deserialization.

Constructors

NotFound ByteString

The key was not found.

ParseErr ByteString Text

A parse error occured while deserializing the type.

class ParamErrToApiErr apiErr where Source

Convert the ParamErr that occured during deserialization into ApiErr type which can then be put in Response.

Methods

toApiErr :: [ParamErr] -> apiErr Source

fromQueryParam :: FromParam a QueryParam => Query -> Validation [ParamErr] a Source

(Try to) Deserialize a type from query params.

fromFormParam :: FromParam a FormParam => [(ByteString, ByteString)] -> Validation [ParamErr] a Source

(Try to) Deserialize a type from form params.

fromFileParam :: FromParam a FileParam => [(ByteString, FileInfo FilePath)] -> Validation [ParamErr] a Source

(Try to) Deserialize a type from file params.

fromCookie :: FromParam a Cookie => [(ByteString, ByteString)] -> Validation [ParamErr] a Source

(Try to) Deserialize a type from cookie.

lookupParam :: Proxy (parK :: ParamK) -> ByteString -> Trie (DeSerializedData parK) -> Maybe (DeSerializedData parK) Source

Lookup a value from the Trie using the given key.

fromNonNestedParam :: (FromParam (NonNested a) parK, DecodeParam a) => Proxy (parK :: ParamK) -> ByteString -> Trie (DeSerializedData parK) -> Validation [ParamErr] a Source

(Try to) Deserialize a type without nesting.

Wrappers

newtype JsonOf a Source

Serializing JsonOf will produce a JSON representation of the value contained within. This is useful if params has to be sent as JSON.

Constructors

JsonOf 

Fields

getValue :: a
 

newtype OptValue a Source

Use this type if a key is required but the value is optional.

Constructors

OptValue 

Fields

toMaybe :: Maybe a
 

newtype NonNested a Source

Use this type if for serialization / deserialization nesting is not required. The type contained within most likely requires EncodeParam / DecodeParam.

Constructors

NonNested 

Fields

getNonNestedParam :: a
 

Helpers

data ParamK Source

(Kind) Describes the various types of Param.

filePath :: FileInfo -> FilePath Source

Obtain the file path from FileInfo.

nest :: ByteString -> ByteString -> ByteString Source

Nest the key with a prefix.

nest "pfx" "key" == "pfx.key"
nest "" "key" == "key"