3PRCNKNZBBKNOSRY2VXATUP4OCOEVA2UERVRBYTKUI3BG3LP4OXAC
post :: (HTTP :> es, JSON.ToJSON body) => body -> Text -> Eff es Response
post body = send . Post (JSON.encode body)
post :: (HTTP :> es, Fail :> es, Sign :> es, JSON.ToJSON body) => body -> Text -> Eff es Response
post body url = do
key <- send GetKey
res <- send (Exec (POST { payload = JSON.encode body, url }) key)
either fail return res
get :: (HTTP :> es) => Text -> Eff es Response
get = send . Get
setKey :: (S.KeySource s, HTTP :> es) => s -> Eff es a -> Eff es a
setKey key rest = send (SetKey (S.toSigner key) rest)
get :: (HTTP :> es, Fail :> es, Sign :> es) => Text -> Eff es Response
get url = do
key <- send GetKey
res <- send (Exec (GET { url }) key)
either fail return res
runHTTP src = reinterpret (runReader (S.toSigner src)) $ \env -> \case
SetKey key rest -> localSeqUnlift env $ \unlift ->
local (const key) (unlift rest)
Post payload url -> do
signer <- ask
req <- withSeqEffToIO (parse url)
ask >>= execute signer
(req { method = "POST" })
(Just $ BS.toStrict payload)
Get url -> do
signer <- ask
req <- withSeqEffToIO (parse url)
ask >>= execute signer req Nothing
where
runHTTP manager = interpret $ \_ -> \case
Exec req signer -> runFail $ do
request <- withSeqEffToIO (parse req.url)
case req of
GET {} -> execute signer request Nothing manager
POST { payload } -> execute signer
(request { method = "POST" })
(Just $ BS.toStrict payload)
manager
where
-- Share a single HTTP client between all threads using the HTTP effect.
-- We don't evaluate the HTTP effect in the global handler, because we
-- don't want failures to propagate to the main thread. This handler must
-- not fail unless there is something catastrophically wrong.
. runReader manager