{-# language DataKinds #-} module Fedi.Requests where import Data.Aeson qualified as A import Fedi.Helpers import Fedi.UserDetails import Fedi.Crypto import Network.HTTP.Req qualified as Req import Data.ByteString.Lazy qualified as BSL import Text.URI qualified as URI sendPost :: (A.FromJSON output, A.ToJSON input) => UserDetails -> String -> input -> IO output sendPost details url payload = do let encoded = BSL.toStrict $ A.encode payload signed <- sign details encoded uri <- URI.mkURI $ fromString url (url', scheme) <- maybe (throw "couldn't parse uri") pure (Req.useHttpsURI uri) print ("url", url') Req.runReq Req.defaultHttpConfig do r <- Req.req Req.POST url' (Req.ReqBodyBs encoded) Req.jsonResponse ( scheme <> Req.header "ContentType" "application/activity+json" <> Req.header "Digest" signed.signedDigest <> Req.header "Signature" signed.signedMessage ) pure $ Req.responseBody r sendGet :: (A.FromJSON a) => String -> IO a sendGet url = do uri <- URI.mkURI $ fromString url (url', scheme) <- maybe (throw "couldn't parse uri") pure (Req.useHttpsURI uri) print ("url", url') Req.runReq Req.defaultHttpConfig do r <- Req.req Req.GET url' Req.NoReqBody Req.jsonResponse ( scheme <> Req.header "ContentType" "application/activity+json" ) pure $ Req.responseBody r