{-# LANGUAGE DeriveLift #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE UndecidableInstances #-}
module Aws.Lambda.Runtime.Common
( RunCallback
, LambdaResult(..)
, LambdaError(..)
, LambdaOptions(..)
, DispatcherOptions(..)
, ApiGatewayDispatcherOptions(..)
, DispatcherStrategy(..)
, ToLambdaResponseBody(..)
, unLambdaResponseBody
, defaultDispatcherOptions
) where
import Aws.Lambda.Runtime.ApiGatewayInfo
import Aws.Lambda.Runtime.Context (Context)
import Aws.Lambda.Utilities
import Data.Aeson (FromJSON, ToJSON)
import qualified Data.ByteString.Lazy as Lazy
import Data.Text (Text)
import qualified Data.Text as Text
import GHC.Generics (Generic)
import Language.Haskell.TH.Syntax (Lift)
newtype ApiGatewayDispatcherOptions = ApiGatewayDispatcherOptions
{ propagateImpureExceptions :: Bool
} deriving (Lift)
newtype DispatcherOptions = DispatcherOptions
{ apiGatewayDispatcherOptions :: ApiGatewayDispatcherOptions
} deriving (Lift)
defaultDispatcherOptions :: DispatcherOptions
defaultDispatcherOptions =
DispatcherOptions (ApiGatewayDispatcherOptions True)
data DispatcherStrategy =
UseWithAPIGateway |
StandaloneLambda
deriving (Lift)
type RunCallback context =
LambdaOptions context -> IO (Either LambdaError LambdaResult)
newtype LambdaResponseBody =
LambdaResponseBody { unLambdaResponseBody :: Text }
deriving newtype (ToJSON, FromJSON)
class ToLambdaResponseBody a where
toStandaloneLambdaResponse :: a -> LambdaResponseBody
instance {-# OVERLAPPING #-} ToLambdaResponseBody String where
toStandaloneLambdaResponse = LambdaResponseBody . Text.pack
instance {-# OVERLAPPING #-} ToLambdaResponseBody Text where
toStandaloneLambdaResponse = LambdaResponseBody
instance ToJSON a => ToLambdaResponseBody a where
toStandaloneLambdaResponse = LambdaResponseBody . toJSONText
data LambdaError =
StandaloneLambdaError LambdaResponseBody |
ApiGatewayLambdaError (ApiGatewayResponse ApiGatewayResponseBody)
data LambdaResult =
StandaloneLambdaResult LambdaResponseBody |
ApiGatewayResult (ApiGatewayResponse ApiGatewayResponseBody)
data LambdaOptions context = LambdaOptions
{ eventObject :: !Lazy.ByteString
, functionHandler :: !String
, executionUuid :: !String
, contextObject :: !(Context context)
} deriving (Generic)