Skip to content

Commit 30c4aa9

Browse files
author
Nikita Tchayka
committed
Format ApiGatewayInfo
1 parent 1a7cf1e commit 30c4aa9

File tree

1 file changed

+129
-101
lines changed

1 file changed

+129
-101
lines changed
+129-101
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
{-# LANGUAGE DefaultSignatures #-}
2-
{-# LANGUAGE DerivingStrategies #-}
3-
{-# LANGUAGE DuplicateRecordFields #-}
4-
{-# LANGUAGE FlexibleInstances #-}
1+
{-# LANGUAGE DefaultSignatures #-}
2+
{-# LANGUAGE DerivingStrategies #-}
3+
{-# LANGUAGE DuplicateRecordFields #-}
4+
{-# LANGUAGE FlexibleInstances #-}
55
{-# LANGUAGE GeneralisedNewtypeDeriving #-}
6-
{-# LANGUAGE UndecidableInstances #-}
6+
{-# LANGUAGE UndecidableInstances #-}
77

88
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
1618

1719
import Aws.Lambda.Utilities
1820
import Data.Aeson
@@ -39,6 +41,22 @@ data ApiGatewayRequest body = ApiGatewayRequest
3941
, apiGatewayRequestBody :: !(Maybe body)
4042
} deriving (Show)
4143

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+
>>>>>>> 50cd385... Format ApiGatewayInfo
59+
4260
-- We special case String and Text in order
4361
-- to avoid unneeded encoding which will wrap them in quotes and break parsing
4462
instance {-# OVERLAPPING #-} FromJSON (ApiGatewayRequest Text) where
@@ -58,92 +76,99 @@ parseObjectFromStringField obj fieldName = do
5876
String stringContents ->
5977
case eitherDecodeStrict (T.encodeUtf8 stringContents) of
6078
Right success -> pure success
61-
Left err -> fail err
79+
Left err -> fail err
6280
Null -> pure Nothing
6381
other -> T.typeMismatch "String or Null" other
6482

6583
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"
7796
parseApiGatewayRequest _ _ = fail "Expected ApiGatewayRequest to be an object."
7897

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)
95116

96117
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"
112134
parseJSON _ = fail "Expected ApiGatewayRequestContext to be an object."
113135

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)
128152

129153
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"
143168
parseJSON _ = fail "Expected ApiGatewayRequestContextIdentity to be an object."
144169

145-
newtype ApiGatewayResponseBody =
146-
ApiGatewayResponseBody Text
170+
newtype ApiGatewayResponseBody
171+
= ApiGatewayResponseBody Text
147172
deriving newtype (ToJSON, FromJSON)
148173

149174
class ToApiGatewayResponseBody a where
@@ -159,33 +184,36 @@ instance {-# OVERLAPPING #-} ToApiGatewayResponseBody String where
159184
instance ToJSON a => ToApiGatewayResponseBody a where
160185
toApiGatewayResponseBody = ApiGatewayResponseBody . toJSONText
161186

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)
168195

169196
instance Functor ApiGatewayResponse where
170-
fmap f v = v { apiGatewayResponseBody = f (apiGatewayResponseBody v) }
197+
fmap f v = v {apiGatewayResponseBody = f (apiGatewayResponseBody v)}
171198

172-
instance ToJSON body => ToJSON (ApiGatewayResponse body) where
199+
instance ToJSON body => ToJSON (ApiGatewayResponse body) where
173200
toJSON = apiGatewayResponseToJSON toJSON
174201

175202
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+
]
182210

183211
mkApiGatewayResponse :: Int -> payload -> ApiGatewayResponse payload
184212
mkApiGatewayResponse code payload =
185213
ApiGatewayResponse code [] payload False
186214

187215
headerToPair :: Header -> T.Pair
188216
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

Comments
 (0)