{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
module Discord.Rest.Prelude where
import Prelude hiding (log)
import Data.Default (def)
import Control.Exception (throwIO)
import Control.Monad.IO.Class (MonadIO, liftIO)
import Data.Monoid ((<>))
import qualified Data.Text as T
import qualified Network.HTTP.Req as R
import Discord.Types
authHeader :: Auth -> R.Option 'R.Https
authHeader auth =
R.header "Authorization" (formatAuth auth)
<> R.header "User-Agent" agent
where
agent = "DiscordBot (https://github.com/aquarial/discord-haskell, 0.8.0)"
infixl 5 //
(//) :: Show a => R.Url scheme -> a -> R.Url scheme
(//) url part = url R./: T.pack (show part)
data JsonRequest where
Delete :: R.Url 'R.Https -> R.Option 'R.Https -> JsonRequest
Get :: R.Url 'R.Https -> R.Option 'R.Https -> JsonRequest
Patch :: R.HttpBody a => R.Url 'R.Https -> a -> R.Option 'R.Https -> JsonRequest
Put :: R.HttpBody a => R.Url 'R.Https -> a -> R.Option 'R.Https -> JsonRequest
Post :: R.HttpBody a => R.Url 'R.Https -> RestIO a -> R.Option 'R.Https -> JsonRequest
class Request a where
majorRoute :: a -> String
jsonRequest :: a -> JsonRequest
newtype RestIO a = RestIO { restIOtoIO :: IO a }
deriving (Functor, Applicative, Monad, MonadIO)
instance R.MonadHttp RestIO where
handleHttpException = liftIO . throwIO
getHttpConfig = pure $ def { R.httpConfigCheckResponse = \_ _ _ -> Nothing }