module Nero.Payload
(
Payload
, payloadText
, Encoding
, utf8Encoding
, Payloaded(..)
, Body
, HasBody(..)
, Form
, _Form
, Formed(..)
, dummyPayloadForm
) where
import Data.ByteString.Lazy (ByteString)
import Nero.Prelude
import Nero.Param
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 Payloaded a where
payload :: Traversal' 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) = encodeMultiMap 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