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