-
Notifications
You must be signed in to change notification settings - Fork 48
/
ApiGatewayInfo.hs
191 lines (168 loc) · 7.79 KB
/
ApiGatewayInfo.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralisedNewtypeDeriving #-}
{-# LANGUAGE UndecidableInstances #-}
module Aws.Lambda.Runtime.ApiGatewayInfo
( ApiGatewayRequest(..)
, ApiGatewayRequestContext(..)
, ApiGatewayRequestContextIdentity(..)
, ApiGatewayResponse(..)
, ApiGatewayResponseBody(..)
, ToApiGatewayResponseBody(..)
, mkApiGatewayResponse ) where
import Aws.Lambda.Utilities
import Data.Aeson
import Data.Aeson.Types (Parser)
import qualified Data.Aeson.Types as T
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
data ApiGatewayRequest body = ApiGatewayRequest
{ apiGatewayRequestResource :: !Text
, apiGatewayRequestPath :: !Text
, apiGatewayRequestHttpMethod :: !Text
, apiGatewayRequestHeaders :: !(HashMap Text Text)
, apiGatewayRequestQueryStringParameters :: !(Maybe [(Text, Maybe Text)])
, apiGatewayRequestPathParameters :: !(Maybe (HashMap Text Text))
, apiGatewayRequestStageVariables :: !(Maybe (HashMap Text Text))
, apiGatewayRequestIsBase64Encoded :: !Bool
, apiGatewayRequestRequestContext :: !ApiGatewayRequestContext
, apiGatewayRequestBody :: !(Maybe body)
} deriving (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 (ApiGatewayRequest Text) where
parseJSON = parseApiGatewayRequest (.:)
instance {-# OVERLAPPING #-} FromJSON (ApiGatewayRequest String) where
parseJSON = parseApiGatewayRequest (.:)
instance FromJSON body => FromJSON (ApiGatewayRequest body) where
parseJSON = parseApiGatewayRequest parseObjectFromStringField
-- We need this because API Gateway is going to send us the payload as a JSON string
parseObjectFromStringField :: FromJSON a => Object -> Text -> Parser (Maybe a)
parseObjectFromStringField obj fieldName = do
fieldContents <- obj .: fieldName
case fieldContents of
String stringContents ->
case eitherDecodeStrict (T.encodeUtf8 stringContents) of
Right success -> pure success
Left err -> fail err
Null -> pure Nothing
other -> T.typeMismatch "String or Null" other
parseApiGatewayRequest :: (Object -> Text -> Parser (Maybe body)) -> Value -> Parser (ApiGatewayRequest body)
parseApiGatewayRequest bodyParser (Object v) = ApiGatewayRequest <$>
v .: "resource" <*>
v .: "path" <*>
v .: "httpMethod" <*>
v .: "headers" <*>
v .: "queryStringParameters" <*>
v .: "pathParameters" <*>
v .: "stageVariables" <*>
v .: "isBase64Encoded" <*>
v .: "requestContext" <*>
v `bodyParser` "body"
parseApiGatewayRequest _ _ = fail "Expected ApiGatewayRequest to be an object."
data ApiGatewayRequestContext = ApiGatewayRequestContext
{ apiGatewayRequestContextResourceId :: !Text
, apiGatewayRequestContextResourcePath :: !Text
, apiGatewayRequestContextHttpMethod :: !Text
, apiGatewayRequestContextExtendedRequestId :: !Text
, apiGatewayRequestContextRequestTime :: !Text
, apiGatewayRequestContextPath :: !Text
, apiGatewayRequestContextAccountId :: !Text
, apiGatewayRequestContextProtocol :: !Text
, apiGatewayRequestContextStage :: !Text
, apiGatewayRequestContextDomainPrefix :: !Text
, apiGatewayRequestContextRequestId :: !Text
, apiGatewayRequestContextDomainName :: !Text
, apiGatewayRequestContextApiId :: !Text
, apiGatewayRequestContextIdentity :: !ApiGatewayRequestContextIdentity
} deriving (Show)
instance FromJSON ApiGatewayRequestContext where
parseJSON (Object v) = ApiGatewayRequestContext <$>
v .: "resourceId" <*>
v .: "path" <*>
v .: "httpMethod" <*>
v .: "extendedRequestId" <*>
v .: "requestTime" <*>
v .: "path" <*>
v .: "accountId" <*>
v .: "protocol" <*>
v .: "stage" <*>
v .: "domainPrefix" <*>
v .: "requestId" <*>
v .: "domainName" <*>
v .: "apiId" <*>
v .: "identity"
parseJSON _ = fail "Expected ApiGatewayRequestContext to be an object."
data ApiGatewayRequestContextIdentity = ApiGatewayRequestContextIdentity
{ apiGatewayRequestContextIdentityCognitoIdentityPoolId :: !(Maybe Text)
, apiGatewayRequestContextIdentityAccountId :: !(Maybe Text)
, apiGatewayRequestContextIdentityCognitoIdentityId :: !(Maybe Text)
, apiGatewayRequestContextIdentityCaller :: !(Maybe Text)
, apiGatewayRequestContextIdentitySourceIp :: !(Maybe Text)
, apiGatewayRequestContextIdentityPrincipalOrgId :: !(Maybe Text)
, apiGatewayRequestContextIdentityAccesskey :: !(Maybe Text)
, apiGatewayRequestContextIdentityCognitoAuthenticationType :: !(Maybe Text)
, apiGatewayRequestContextIdentityCognitoAuthenticationProvider :: !(Maybe Value)
, apiGatewayRequestContextIdentityUserArn :: !(Maybe Text)
, apiGatewayRequestContextIdentityUserAgent :: !(Maybe Text)
, apiGatewayRequestContextIdentityUser :: !(Maybe Text)
} deriving (Show)
instance FromJSON ApiGatewayRequestContextIdentity where
parseJSON (Object v) = ApiGatewayRequestContextIdentity <$>
v .: "cognitoIdentityPoolId" <*>
v .: "accountId" <*>
v .: "cognitoIdentityId" <*>
v .: "caller" <*>
v .: "sourceIp" <*>
v .: "principalOrgId" <*>
v .: "accessKey" <*>
v .: "cognitoAuthenticationType" <*>
v .: "cognitoAuthenticationProvider" <*>
v .: "userArn" <*>
v .: "userAgent" <*>
v .: "user"
parseJSON _ = fail "Expected ApiGatewayRequestContextIdentity to be an object."
newtype ApiGatewayResponseBody =
ApiGatewayResponseBody Text
deriving newtype (ToJSON, FromJSON)
class ToApiGatewayResponseBody a where
toApiGatewayResponseBody :: a -> ApiGatewayResponseBody
-- We special case Text and String to avoid unneeded encoding which will wrap them in quotes
instance {-# OVERLAPPING #-} ToApiGatewayResponseBody Text where
toApiGatewayResponseBody = ApiGatewayResponseBody
instance {-# OVERLAPPING #-} ToApiGatewayResponseBody String where
toApiGatewayResponseBody = ApiGatewayResponseBody . T.pack
instance ToJSON a => ToApiGatewayResponseBody a where
toApiGatewayResponseBody = ApiGatewayResponseBody . toJSONText
data ApiGatewayResponse body = ApiGatewayResponse
{ apiGatewayResponseStatusCode :: !Int
, apiGatewayResponseHeaders :: !ResponseHeaders
, apiGatewayResponseBody :: !body
, apiGatewayResponseIsBase64Encoded :: !Bool
} deriving (Generic, Show)
instance Functor ApiGatewayResponse where
fmap f v = v { apiGatewayResponseBody = f (apiGatewayResponseBody v) }
instance ToJSON body => ToJSON (ApiGatewayResponse body) where
toJSON = apiGatewayResponseToJSON toJSON
apiGatewayResponseToJSON :: (body -> Value) -> ApiGatewayResponse body -> Value
apiGatewayResponseToJSON bodyTransformer ApiGatewayResponse {..} = object
[ "statusCode" .= apiGatewayResponseStatusCode
, "body" .= bodyTransformer apiGatewayResponseBody
, "headers" .= object (map headerToPair apiGatewayResponseHeaders)
, "isBase64Encoded" .= apiGatewayResponseIsBase64Encoded
]
mkApiGatewayResponse :: Int -> payload -> ApiGatewayResponse payload
mkApiGatewayResponse code payload =
ApiGatewayResponse code [] payload False
headerToPair :: Header -> T.Pair
headerToPair (cibyte, bstr) = k .= v
where
k = (T.decodeUtf8 . CI.original) cibyte
v = T.decodeUtf8 bstr