module Nero.Request
(
Request
, get
, post
, _GET
, _POST
, method
, payloaded
, params
, GET
, POST
, dummyRequest
, dummyRequestForm
) where
import Data.ByteString (ByteString)
import Nero.Prelude
import Nero.Param
import Nero.Payload
import Nero.Url
data Request = RequestGET GET
| RequestPOST POST
deriving (Show,Eq)
instance HasUrl Request where
url f (RequestGET (GET u)) = RequestGET . GET <$> f u
url f (RequestPOST (POST u pl)) = RequestPOST . flip POST pl <$> f u
instance HasHost Request where
host = url . host
instance HasPath Request where
path = url . path
instance HasQuery Request where
query = url . query
instance Param Request where
param k = params . ix k . traverse
instance Formed Request where
form = payloaded . form
instance HasBody Request where
body = maybe mempty body . preview payloaded
get :: Url -> Request
get u = _GET # GET u
post :: Url -> Payload -> Request
post u p = _POST # POST u p
_GET :: Prism' Request GET
_GET = prism' RequestGET $ \case
RequestGET g -> Just g
_ -> Nothing
_POST :: Prism' Request POST
_POST = prism' RequestPOST $ \case
RequestPOST p -> Just p
_ -> Nothing
method :: Request -> ByteString
method RequestGET {} = "GET"
method RequestPOST {} = "POST"
payloaded :: Traversal' Request Payload
payloaded _ rg@(RequestGET {}) = pure rg
payloaded f (RequestPOST (POST u pl)) =
RequestPOST <$> (POST <$> pure u <*> f pl)
params :: Traversal' Request MultiMap
params f request@(RequestGET {}) = query f request
params f (RequestPOST (POST u pl)) =
RequestPOST <$> (POST <$> query f u <*> form f pl)
data GET = GET Url deriving (Show,Eq)
instance HasUrl GET where
url f (GET u) = GET <$> f u
instance HasHost GET where
host = url . host
instance HasPath GET where
path = url . path
instance HasQuery GET where
query = url . query
data POST = POST Url Payload deriving (Show,Eq)
instance HasUrl POST where
url f (POST u p) = flip POST p <$> f u
instance HasPayload POST where
payload f (POST u p) = POST u <$> f p
instance HasHost POST where
host = url . host
instance HasPath POST where
path = url . path
instance HasQuery POST where
query = url . query
dummyRequest :: Request
dummyRequest = RequestGET $ GET dummyUrl
dummyRequestForm :: Request
dummyRequestForm = RequestPOST $ POST dummyUrl dummyPayloadForm