{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralisedNewtypeDeriving #-}
{-# LANGUAGE UndecidableInstances #-}

module Aws.Lambda.Runtime.ALB.Types
  ( ALBRequest (..),
    ALBRequestContext (..),
    ALBResponse (..),
    ALBResponseBody (..),
    ToALBResponseBody (..),
    mkALBResponse,
  )
where

import Aws.Lambda.Utilities (toJSONText, tshow)
import Data.Aeson
  ( FromJSON (parseJSON),
    KeyValue ((.=)),
    Object,
    ToJSON (toJSON),
    Value (Null, Object, String),
    eitherDecodeStrict,
    object,
    (.:),
  )
import Data.Aeson.Types (Parser)
import qualified Data.Aeson.Types as T
import qualified Data.Aeson.Key as K
import qualified Data.CaseInsensitive as CI
import Data.HashMap.Strict (HashMap)
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import GHC.Generics (Generic)
import Network.HTTP.Types (Header, ResponseHeaders)

data ALBRequest body = ALBRequest
  { forall body. ALBRequest body -> Text
albRequestPath :: !Text,
    forall body. ALBRequest body -> Text
albRequestHttpMethod :: !Text,
    forall body. ALBRequest body -> Maybe (HashMap Text Text)
albRequestHeaders :: !(Maybe (HashMap Text Text)),
    -- TODO: They won't be url decoded in ALB
    forall body. ALBRequest body -> Maybe (HashMap Text Text)
albRequestQueryStringParameters :: !(Maybe (HashMap Text Text)),
    forall body. ALBRequest body -> Bool
albRequestIsBase64Encoded :: !Bool,
    forall body. ALBRequest body -> ALBRequestContext
albRequestRequestContext :: !ALBRequestContext,
    forall body. ALBRequest body -> Maybe body
albRequestBody :: !(Maybe body)
  }
  deriving (Int -> ALBRequest body -> ShowS
forall body. Show body => Int -> ALBRequest body -> ShowS
forall body. Show body => [ALBRequest body] -> ShowS
forall body. Show body => ALBRequest body -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ALBRequest body] -> ShowS
$cshowList :: forall body. Show body => [ALBRequest body] -> ShowS
show :: ALBRequest body -> String
$cshow :: forall body. Show body => ALBRequest body -> String
showsPrec :: Int -> ALBRequest body -> ShowS
$cshowsPrec :: forall body. Show body => Int -> ALBRequest body -> ShowS
Show)

-- We special case String and Text in order
-- to avoid unneeded encoding which will wrap them in quotes and break parsing
instance {-# OVERLAPPING #-} FromJSON (ALBRequest Text) where
  parseJSON :: Value -> Parser (ALBRequest Text)
parseJSON = forall body.
(Object -> Key -> Parser (Maybe body))
-> Value -> Parser (ALBRequest body)
parseALBRequest forall a. FromJSON a => Object -> Key -> Parser a
(.:)

instance {-# OVERLAPPING #-} FromJSON (ALBRequest String) where
  parseJSON :: Value -> Parser (ALBRequest String)
parseJSON = forall body.
(Object -> Key -> Parser (Maybe body))
-> Value -> Parser (ALBRequest body)
parseALBRequest forall a. FromJSON a => Object -> Key -> Parser a
(.:)

instance FromJSON body => FromJSON (ALBRequest body) where
  parseJSON :: Value -> Parser (ALBRequest body)
parseJSON = forall body.
(Object -> Key -> Parser (Maybe body))
-> Value -> Parser (ALBRequest body)
parseALBRequest forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
parseObjectFromStringField

-- We need this because API Gateway is going to send us the payload as a JSON string
parseObjectFromStringField :: FromJSON a => Object -> T.Key -> Parser (Maybe a)
parseObjectFromStringField :: forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
parseObjectFromStringField Object
obj Key
fieldName = do
  Value
fieldContents <- Object
obj forall a. FromJSON a => Object -> Key -> Parser a
.: Key
fieldName
  case Value
fieldContents of
    String Text
stringContents ->
      case forall a. FromJSON a => ByteString -> Either String a
eitherDecodeStrict (Text -> ByteString
T.encodeUtf8 Text
stringContents) of
        Right Maybe a
success -> forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe a
success
        Left String
err -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
err
    Value
Null -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Maybe a
Nothing
    Value
other -> forall a. String -> Value -> Parser a
T.typeMismatch String
"String or Null" Value
other

parseALBRequest :: (Object -> T.Key -> Parser (Maybe body)) -> Value -> Parser (ALBRequest body)
parseALBRequest :: forall body.
(Object -> Key -> Parser (Maybe body))
-> Value -> Parser (ALBRequest body)
parseALBRequest Object -> Key -> Parser (Maybe body)
bodyParser (Object Object
v) =
  forall body.
Text
-> Text
-> Maybe (HashMap Text Text)
-> Maybe (HashMap Text Text)
-> Bool
-> ALBRequestContext
-> Maybe body
-> ALBRequest body
ALBRequest
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"path"
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"httpMethod"
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"headers"
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"queryStringParameters"
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"isBase64Encoded"
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"requestContext"
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser (Maybe body)
`bodyParser` Key
"body"
parseALBRequest Object -> Key -> Parser (Maybe body)
_ Value
_ = forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Expected ALBRequest to be an object."

newtype ALBRequestContext = ALBRequestContext
  {ALBRequestContext -> ALBELB
albRequestContextElb :: ALBELB}
  deriving (Int -> ALBRequestContext -> ShowS
[ALBRequestContext] -> ShowS
ALBRequestContext -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ALBRequestContext] -> ShowS
$cshowList :: [ALBRequestContext] -> ShowS
show :: ALBRequestContext -> String
$cshow :: ALBRequestContext -> String
showsPrec :: Int -> ALBRequestContext -> ShowS
$cshowsPrec :: Int -> ALBRequestContext -> ShowS
Show)

instance FromJSON ALBRequestContext where
  parseJSON :: Value -> Parser ALBRequestContext
parseJSON (Object Object
v) =
    ALBELB -> ALBRequestContext
ALBRequestContext
      forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"elb"
  parseJSON Value
_ = forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Expected ALBRequestContext to be an object."

newtype ALBELB = ALBELB
  {ALBELB -> Text
albElbTargetGroupArn :: Text}
  deriving (Int -> ALBELB -> ShowS
[ALBELB] -> ShowS
ALBELB -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ALBELB] -> ShowS
$cshowList :: [ALBELB] -> ShowS
show :: ALBELB -> String
$cshow :: ALBELB -> String
showsPrec :: Int -> ALBELB -> ShowS
$cshowsPrec :: Int -> ALBELB -> ShowS
Show)

instance FromJSON ALBELB where
  parseJSON :: Value -> Parser ALBELB
parseJSON (Object Object
v) =
    Text -> ALBELB
ALBELB
      forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"targetGroupArn"
  parseJSON Value
_ = forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Expected ALBELB to be an object."

newtype ALBResponseBody
  = ALBResponseBody Text
  deriving newtype ([ALBResponseBody] -> Encoding
[ALBResponseBody] -> Value
ALBResponseBody -> Encoding
ALBResponseBody -> Value
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [ALBResponseBody] -> Encoding
$ctoEncodingList :: [ALBResponseBody] -> Encoding
toJSONList :: [ALBResponseBody] -> Value
$ctoJSONList :: [ALBResponseBody] -> Value
toEncoding :: ALBResponseBody -> Encoding
$ctoEncoding :: ALBResponseBody -> Encoding
toJSON :: ALBResponseBody -> Value
$ctoJSON :: ALBResponseBody -> Value
ToJSON, Value -> Parser [ALBResponseBody]
Value -> Parser ALBResponseBody
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [ALBResponseBody]
$cparseJSONList :: Value -> Parser [ALBResponseBody]
parseJSON :: Value -> Parser ALBResponseBody
$cparseJSON :: Value -> Parser ALBResponseBody
FromJSON)

class ToALBResponseBody a where
  toALBResponseBody :: a -> ALBResponseBody

-- We special case Text and String to avoid unneeded encoding which will wrap them in quotes
instance {-# OVERLAPPING #-} ToALBResponseBody Text where
  toALBResponseBody :: Text -> ALBResponseBody
toALBResponseBody = Text -> ALBResponseBody
ALBResponseBody

instance {-# OVERLAPPING #-} ToALBResponseBody String where
  toALBResponseBody :: String -> ALBResponseBody
toALBResponseBody = Text -> ALBResponseBody
ALBResponseBody forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack

instance ToJSON a => ToALBResponseBody a where
  toALBResponseBody :: a -> ALBResponseBody
toALBResponseBody = Text -> ALBResponseBody
ALBResponseBody forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. ToJSON a => a -> Text
toJSONText

data ALBResponse body = ALBResponse
  { forall body. ALBResponse body -> Int
albResponseStatusCode :: !Int,
    forall body. ALBResponse body -> Text
albResponseStatusDescription :: !Text,
    forall body. ALBResponse body -> ResponseHeaders
albResponseHeaders :: !ResponseHeaders,
    forall body. ALBResponse body -> ResponseHeaders
albResponseMultiValueHeaders :: !ResponseHeaders,
    forall body. ALBResponse body -> body
albResponseBody :: !body,
    forall body. ALBResponse body -> Bool
albResponseIsBase64Encoded :: !Bool
  }
  deriving (forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall body x. Rep (ALBResponse body) x -> ALBResponse body
forall body x. ALBResponse body -> Rep (ALBResponse body) x
$cto :: forall body x. Rep (ALBResponse body) x -> ALBResponse body
$cfrom :: forall body x. ALBResponse body -> Rep (ALBResponse body) x
Generic, Int -> ALBResponse body -> ShowS
forall body. Show body => Int -> ALBResponse body -> ShowS
forall body. Show body => [ALBResponse body] -> ShowS
forall body. Show body => ALBResponse body -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ALBResponse body] -> ShowS
$cshowList :: forall body. Show body => [ALBResponse body] -> ShowS
show :: ALBResponse body -> String
$cshow :: forall body. Show body => ALBResponse body -> String
showsPrec :: Int -> ALBResponse body -> ShowS
$cshowsPrec :: forall body. Show body => Int -> ALBResponse body -> ShowS
Show)

instance Functor ALBResponse where
  fmap :: forall a b. (a -> b) -> ALBResponse a -> ALBResponse b
fmap a -> b
f ALBResponse a
v = ALBResponse a
v {$sel:albResponseBody:ALBResponse :: b
albResponseBody = a -> b
f (forall body. ALBResponse body -> body
albResponseBody ALBResponse a
v)}

instance ToJSON body => ToJSON (ALBResponse body) where
  toJSON :: ALBResponse body -> Value
toJSON = forall body. (body -> Value) -> ALBResponse body -> Value
albResponseToJSON forall a. ToJSON a => a -> Value
toJSON

albResponseToJSON :: (body -> Value) -> ALBResponse body -> Value
albResponseToJSON :: forall body. (body -> Value) -> ALBResponse body -> Value
albResponseToJSON body -> Value
bodyTransformer ALBResponse {body
Bool
Int
ResponseHeaders
Text
albResponseIsBase64Encoded :: Bool
albResponseBody :: body
albResponseMultiValueHeaders :: ResponseHeaders
albResponseHeaders :: ResponseHeaders
albResponseStatusDescription :: Text
albResponseStatusCode :: Int
$sel:albResponseIsBase64Encoded:ALBResponse :: forall body. ALBResponse body -> Bool
$sel:albResponseBody:ALBResponse :: forall body. ALBResponse body -> body
$sel:albResponseMultiValueHeaders:ALBResponse :: forall body. ALBResponse body -> ResponseHeaders
$sel:albResponseHeaders:ALBResponse :: forall body. ALBResponse body -> ResponseHeaders
$sel:albResponseStatusDescription:ALBResponse :: forall body. ALBResponse body -> Text
$sel:albResponseStatusCode:ALBResponse :: forall body. ALBResponse body -> Int
..} =
  [Pair] -> Value
object
    [ Key
"statusCode" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Int
albResponseStatusCode,
      Key
"body" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= body -> Value
bodyTransformer body
albResponseBody,
      Key
"headers" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= [Pair] -> Value
object (forall a b. (a -> b) -> [a] -> [b]
map (HeaderName, ByteString) -> Pair
headerToPair ResponseHeaders
albResponseHeaders),
      Key
"multiValueHeaders" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= [Pair] -> Value
object (forall a b. (a -> b) -> [a] -> [b]
map (HeaderName, ByteString) -> Pair
headerToPair ResponseHeaders
albResponseHeaders),
      Key
"isBase64Encoded" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Bool
albResponseIsBase64Encoded
    ]

mkALBResponse :: Int -> ResponseHeaders -> payload -> ALBResponse payload
mkALBResponse :: forall payload.
Int -> ResponseHeaders -> payload -> ALBResponse payload
mkALBResponse Int
code ResponseHeaders
headers payload
payload =
  forall body.
Int
-> Text
-> ResponseHeaders
-> ResponseHeaders
-> body
-> Bool
-> ALBResponse body
ALBResponse Int
code (forall {a}. (Eq a, Num a, Show a) => a -> Text
codeDescription Int
code) ResponseHeaders
headers ResponseHeaders
headers payload
payload Bool
False
  where
    codeDescription :: a -> Text
codeDescription a
200 = Text
"200 OK"
    codeDescription a
201 = Text
"201 Created"
    codeDescription a
202 = Text
"202 Accepted"
    codeDescription a
203 = Text
"203 Non-Authoritative Information"
    codeDescription a
204 = Text
"204 No Content"
    codeDescription a
400 = Text
"400 Bad Request"
    codeDescription a
401 = Text
"401 Unauthorized"
    codeDescription a
402 = Text
"402 Payment Required"
    codeDescription a
403 = Text
"403 Forbidden"
    codeDescription a
404 = Text
"404 Not Found"
    codeDescription a
405 = Text
"405 Method Not Allowed"
    codeDescription a
406 = Text
"406 Not Acceptable"
    codeDescription a
500 = Text
"500 Internal Server Error"
    codeDescription a
other = forall a. Show a => a -> Text
tshow a
other

headerToPair :: Header -> T.Pair
headerToPair :: (HeaderName, ByteString) -> Pair
headerToPair (HeaderName
cibyte, ByteString
bstr) = Key
k forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Text
v
  where
    k :: Key
k = (Text -> Key
K.fromText forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Text
T.decodeUtf8 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s. CI s -> s
CI.original) HeaderName
cibyte
    v :: Text
v = ByteString -> Text
T.decodeUtf8 ByteString
bstr