module Nero.Payload
(
Payload
, payloadText
, Encoding
, utf8Encoding
, HasPayload(..)
, Body
, HasBody(..)
, Form
, _Form
, Formed(..)
, dummyPayloadForm
) where
import Data.ByteString.Lazy (ByteString)
import Nero.Prelude
import Nero.Param
import Nero.Binary
data Payload = PayloadText Encoding Body
| PayloadBinary Body
| PayloadForm Form
deriving (Show,Eq)
data Encoding = Utf8
| Unknown String
deriving (Show,Eq)
utf8Encoding :: Encoding
utf8Encoding = Utf8
payloadText :: Encoding -> Body -> Payload
payloadText = PayloadText
class HasPayload a where
payload :: Lens' a Payload
type Body = ByteString
class HasBody a where
body :: a -> Body
instance HasBody Payload where
body (PayloadText _ b) = b
body (PayloadBinary b) = b
body (PayloadForm fo) = render fo
type Form = MultiMap
_Form :: Prism' Payload Form
_Form = prism' PayloadForm $ \case
PayloadForm f -> Just f
_ -> Nothing
class Formed a where
form :: Traversal' a Form
instance Formed Payload where
form = _Form
instance Param Payload where
param k = form . ix k . traverse
dummyPayloadForm :: Payload
dummyPayloadForm = PayloadForm mempty