module AWS.Lambda.Events.ApiGateway.ProxyResponse
( ProxyResponse(..)
, response
, addHeader
, setHeader
, ProxyBody(..)
, textPlain
, applicationJson
, genericBinary
, module Network.HTTP.Types.Status
) where
import Data.Aeson (ToJSON, encode, object, toJSON,
(.=))
import Data.ByteString (ByteString)
import qualified Data.ByteString.Base64 as B64
import Data.CaseInsensitive (CI, mk, original)
import Data.HashMap.Strict (HashMap, foldrWithKey, insert,
insertWith)
import Data.Semigroup ((<>))
import qualified Data.Text as T
import qualified Data.Text.Encoding as TE
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Encoding as TLE
import Network.HTTP.Types.Status (Status (..), accepted202,
badGateway502, badRequest400,
conflict409, continue100,
created201, expectationFailed417,
forbidden403, found302,
gatewayTimeout504, gone410,
httpVersionNotSupported505,
imATeapot418,
internalServerError500,
lengthRequired411,
methodNotAllowed405,
movedPermanently301,
multipleChoices300,
networkAuthenticationRequired511,
noContent204, nonAuthoritative203,
notAcceptable406, notFound404,
notImplemented501, notModified304,
ok200, partialContent206,
paymentRequired402,
permanentRedirect308,
preconditionFailed412,
preconditionRequired428,
proxyAuthenticationRequired407,
requestEntityTooLarge413,
requestHeaderFieldsTooLarge431,
requestTimeout408,
requestURITooLong414,
requestedRangeNotSatisfiable416,
resetContent205, seeOther303,
serviceUnavailable503, status100,
status101, status200, status201,
status202, status203, status204,
status205, status206, status300,
status301, status302, status303,
status304, status305, status307,
status308, status400, status401,
status402, status403, status404,
status405, status406, status407,
status408, status409, status410,
status411, status412, status413,
status414, status415, status416,
status417, status418, status422,
status426, status428, status429,
status431, status500, status501,
status502, status503, status504,
status505, status511,
switchingProtocols101,
temporaryRedirect307,
tooManyRequests429, unauthorized401,
unprocessableEntity422,
unsupportedMediaType415,
upgradeRequired426, useProxy305)
data ProxyBody = ProxyBody
{ ProxyBody -> Text
contentType :: T.Text
, ProxyBody -> Text
serialized :: T.Text
, ProxyBody -> Bool
isBase64Encoded :: Bool
} deriving (Int -> ProxyBody -> ShowS
[ProxyBody] -> ShowS
ProxyBody -> String
(Int -> ProxyBody -> ShowS)
-> (ProxyBody -> String)
-> ([ProxyBody] -> ShowS)
-> Show ProxyBody
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ProxyBody] -> ShowS
$cshowList :: [ProxyBody] -> ShowS
show :: ProxyBody -> String
$cshow :: ProxyBody -> String
showsPrec :: Int -> ProxyBody -> ShowS
$cshowsPrec :: Int -> ProxyBody -> ShowS
Show)
data ProxyResponse = ProxyResponse
{ ProxyResponse -> Status
status :: Status
, :: HashMap (CI T.Text) [T.Text]
, ProxyResponse -> ProxyBody
body :: ProxyBody
} deriving (Int -> ProxyResponse -> ShowS
[ProxyResponse] -> ShowS
ProxyResponse -> String
(Int -> ProxyResponse -> ShowS)
-> (ProxyResponse -> String)
-> ([ProxyResponse] -> ShowS)
-> Show ProxyResponse
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ProxyResponse] -> ShowS
$cshowList :: [ProxyResponse] -> ShowS
show :: ProxyResponse -> String
$cshow :: ProxyResponse -> String
showsPrec :: Int -> ProxyResponse -> ShowS
$cshowsPrec :: Int -> ProxyResponse -> ShowS
Show)
response :: Status -> ProxyBody -> ProxyResponse
response :: Status -> ProxyBody -> ProxyResponse
response =
(Status -> HashMap (CI Text) [Text] -> ProxyBody -> ProxyResponse)
-> HashMap (CI Text) [Text] -> Status -> ProxyBody -> ProxyResponse
forall a b c. (a -> b -> c) -> b -> a -> c
flip Status -> HashMap (CI Text) [Text] -> ProxyBody -> ProxyResponse
ProxyResponse HashMap (CI Text) [Text]
forall a. Monoid a => a
mempty
addHeader :: T.Text -> T.Text -> ProxyResponse -> ProxyResponse
Text
header Text
value (ProxyResponse Status
s HashMap (CI Text) [Text]
mvh ProxyBody
b) =
Status -> HashMap (CI Text) [Text] -> ProxyBody -> ProxyResponse
ProxyResponse Status
s (([Text] -> [Text] -> [Text])
-> CI Text
-> [Text]
-> HashMap (CI Text) [Text]
-> HashMap (CI Text) [Text]
forall k v.
(Eq k, Hashable k) =>
(v -> v -> v) -> k -> v -> HashMap k v -> HashMap k v
insertWith [Text] -> [Text] -> [Text]
forall a. Semigroup a => a -> a -> a
(<>) (Text -> CI Text
forall s. FoldCase s => s -> CI s
mk Text
header) [Text
value] HashMap (CI Text) [Text]
mvh) ProxyBody
b
setHeader :: T.Text -> T.Text -> ProxyResponse -> ProxyResponse
Text
header Text
value (ProxyResponse Status
s HashMap (CI Text) [Text]
mvh ProxyBody
b) =
Status -> HashMap (CI Text) [Text] -> ProxyBody -> ProxyResponse
ProxyResponse Status
s (CI Text
-> [Text] -> HashMap (CI Text) [Text] -> HashMap (CI Text) [Text]
forall k v.
(Eq k, Hashable k) =>
k -> v -> HashMap k v -> HashMap k v
insert (Text -> CI Text
forall s. FoldCase s => s -> CI s
mk Text
header) [Text
value] HashMap (CI Text) [Text]
mvh) ProxyBody
b
genericBinary :: T.Text -> ByteString -> ProxyBody
genericBinary :: Text -> ByteString -> ProxyBody
genericBinary Text
contentType ByteString
x =
Text -> Text -> Bool -> ProxyBody
ProxyBody Text
contentType (ByteString -> Text
TE.decodeUtf8 (ByteString -> Text) -> ByteString -> Text
forall a b. (a -> b) -> a -> b
$ ByteString -> ByteString
B64.encode ByteString
x) Bool
True
textPlain :: T.Text -> ProxyBody
textPlain :: Text -> ProxyBody
textPlain Text
x = Text -> Text -> Bool -> ProxyBody
ProxyBody Text
"text/plain; charset=utf-8" Text
x Bool
False
applicationJson :: ToJSON a => a -> ProxyBody
applicationJson :: a -> ProxyBody
applicationJson a
x =
Text -> Text -> Bool -> ProxyBody
ProxyBody
Text
"application/json; charset=utf-8"
(Text -> Text
TL.toStrict (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ ByteString -> Text
TLE.decodeUtf8 (ByteString -> Text) -> ByteString -> Text
forall a b. (a -> b) -> a -> b
$ a -> ByteString
forall a. ToJSON a => a -> ByteString
encode a
x)
Bool
False
instance ToJSON ProxyResponse where
toJSON :: ProxyResponse -> Value
toJSON (ProxyResponse Status
status HashMap (CI Text) [Text]
mvh (ProxyBody Text
contentType Text
body Bool
isBase64Encoded)) =
let unCI :: HashMap (CI Text) v -> HashMap Text v
unCI = (CI Text -> v -> HashMap Text v -> HashMap Text v)
-> HashMap Text v -> HashMap (CI Text) v -> HashMap Text v
forall k v a. (k -> v -> a -> a) -> a -> HashMap k v -> a
foldrWithKey (Text -> v -> HashMap Text v -> HashMap Text v
forall k v.
(Eq k, Hashable k) =>
k -> v -> HashMap k v -> HashMap k v
insert (Text -> v -> HashMap Text v -> HashMap Text v)
-> (CI Text -> Text)
-> CI Text
-> v
-> HashMap Text v
-> HashMap Text v
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CI Text -> Text
forall s. CI s -> s
original) HashMap Text v
forall a. Monoid a => a
mempty
in [Pair] -> Value
object
[ Text
"statusCode" Text -> Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Status -> Int
statusCode Status
status
, Text
"multiValueHeaders" Text -> HashMap Text [Text] -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.=
([Text] -> [Text] -> [Text])
-> Text -> [Text] -> HashMap Text [Text] -> HashMap Text [Text]
forall k v.
(Eq k, Hashable k) =>
(v -> v -> v) -> k -> v -> HashMap k v -> HashMap k v
insertWith
(\[Text]
_ [Text]
old -> [Text]
old)
(Text
"Content-Type" :: T.Text)
[Text
contentType]
(HashMap (CI Text) [Text] -> HashMap Text [Text]
forall v. HashMap (CI Text) v -> HashMap Text v
unCI HashMap (CI Text) [Text]
mvh)
, Text
"body" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Text
body
, Text
"isBase64Encoded" Text -> Bool -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Bool
isBase64Encoded
]