Skip to content

Commit 50cd385

Browse files
author
Nikita Tchayka
committed
Format ApiGatewayInfo
1 parent 3eabfac commit 50cd385

File tree

1 file changed

+127
-113
lines changed

1 file changed

+127
-113
lines changed
+127-113
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
@@ -26,18 +28,20 @@ import qualified Data.Text.Encoding as T
2628
import GHC.Generics (Generic)
2729
import Network.HTTP.Types
2830

29-
data ApiGatewayRequest body = ApiGatewayRequest
30-
{ apiGatewayRequestResource :: !Text
31-
, apiGatewayRequestPath :: !Text
32-
, apiGatewayRequestHttpMethod :: !Text
33-
, apiGatewayRequestHeaders :: !(HashMap Text Text)
34-
, apiGatewayRequestQueryStringParameters :: !(Maybe [(Text, Maybe Text)])
35-
, apiGatewayRequestPathParameters :: !(Maybe (HashMap Text Text))
36-
, apiGatewayRequestStageVariables :: !(Maybe (HashMap Text Text))
37-
, apiGatewayRequestIsBase64Encoded :: !Bool
38-
, apiGatewayRequestRequestContext :: !ApiGatewayRequestContext
39-
, apiGatewayRequestBody :: !(Maybe body)
40-
} deriving (Show)
31+
data ApiGatewayRequest body
32+
= ApiGatewayRequest
33+
{ apiGatewayRequestResource :: !Text,
34+
apiGatewayRequestPath :: !Text,
35+
apiGatewayRequestHttpMethod :: !Text,
36+
apiGatewayRequestHeaders :: !(Maybe (HashMap Text Text)),
37+
apiGatewayRequestQueryStringParameters :: !(Maybe [(Text, Maybe Text)]),
38+
apiGatewayRequestPathParameters :: !(Maybe (HashMap Text Text)),
39+
apiGatewayRequestStageVariables :: !(Maybe (HashMap Text Text)),
40+
apiGatewayRequestIsBase64Encoded :: !Bool,
41+
apiGatewayRequestRequestContext :: !ApiGatewayRequestContext,
42+
apiGatewayRequestBody :: !(Maybe body)
43+
}
44+
deriving (Show)
4145

4246
-- We special case String and Text in order
4347
-- to avoid unneeded encoding which will wrap them in quotes and break parsing
@@ -58,92 +62,99 @@ parseObjectFromStringField obj fieldName = do
5862
String stringContents ->
5963
case eitherDecodeStrict (T.encodeUtf8 stringContents) of
6064
Right success -> pure success
61-
Left err -> fail err
65+
Left err -> fail err
6266
Null -> pure Nothing
6367
other -> T.typeMismatch "String or Null" other
6468

6569
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"
70+
parseApiGatewayRequest bodyParser (Object v) =
71+
ApiGatewayRequest
72+
<$> v .: "resource"
73+
<*> v .: "path"
74+
<*> v .: "httpMethod"
75+
<*> v .: "headers"
76+
<*> v .: "queryStringParameters"
77+
<*> v .: "pathParameters"
78+
<*> v .: "stageVariables"
79+
<*> v .: "isBase64Encoded"
80+
<*> v .: "requestContext"
81+
<*> v `bodyParser` "body"
7782
parseApiGatewayRequest _ _ = fail "Expected ApiGatewayRequest to be an object."
7883

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)
84+
data ApiGatewayRequestContext
85+
= ApiGatewayRequestContext
86+
{ apiGatewayRequestContextResourceId :: !Text,
87+
apiGatewayRequestContextResourcePath :: !Text,
88+
apiGatewayRequestContextHttpMethod :: !Text,
89+
apiGatewayRequestContextExtendedRequestId :: !Text,
90+
apiGatewayRequestContextRequestTime :: !Text,
91+
apiGatewayRequestContextPath :: !Text,
92+
apiGatewayRequestContextAccountId :: !Text,
93+
apiGatewayRequestContextProtocol :: !Text,
94+
apiGatewayRequestContextStage :: !Text,
95+
apiGatewayRequestContextDomainPrefix :: !Text,
96+
apiGatewayRequestContextRequestId :: !Text,
97+
apiGatewayRequestContextDomainName :: !Text,
98+
apiGatewayRequestContextApiId :: !Text,
99+
apiGatewayRequestContextIdentity :: !ApiGatewayRequestContextIdentity
100+
}
101+
deriving (Show)
95102

96103
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"
104+
parseJSON (Object v) =
105+
ApiGatewayRequestContext
106+
<$> v .: "resourceId"
107+
<*> v .: "path"
108+
<*> v .: "httpMethod"
109+
<*> v .: "extendedRequestId"
110+
<*> v .: "requestTime"
111+
<*> v .: "path"
112+
<*> v .: "accountId"
113+
<*> v .: "protocol"
114+
<*> v .: "stage"
115+
<*> v .: "domainPrefix"
116+
<*> v .: "requestId"
117+
<*> v .: "domainName"
118+
<*> v .: "apiId"
119+
<*> v .: "identity"
112120
parseJSON _ = fail "Expected ApiGatewayRequestContext to be an object."
113121

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)
122+
data ApiGatewayRequestContextIdentity
123+
= ApiGatewayRequestContextIdentity
124+
{ apiGatewayRequestContextIdentityCognitoIdentityPoolId :: !(Maybe Text),
125+
apiGatewayRequestContextIdentityAccountId :: !(Maybe Text),
126+
apiGatewayRequestContextIdentityCognitoIdentityId :: !(Maybe Text),
127+
apiGatewayRequestContextIdentityCaller :: !(Maybe Text),
128+
apiGatewayRequestContextIdentitySourceIp :: !(Maybe Text),
129+
apiGatewayRequestContextIdentityPrincipalOrgId :: !(Maybe Text),
130+
apiGatewayRequestContextIdentityAccesskey :: !(Maybe Text),
131+
apiGatewayRequestContextIdentityCognitoAuthenticationType :: !(Maybe Text),
132+
apiGatewayRequestContextIdentityCognitoAuthenticationProvider :: !(Maybe Value),
133+
apiGatewayRequestContextIdentityUserArn :: !(Maybe Text),
134+
apiGatewayRequestContextIdentityUserAgent :: !(Maybe Text),
135+
apiGatewayRequestContextIdentityUser :: !(Maybe Text)
136+
}
137+
deriving (Show)
128138

129139
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"
140+
parseJSON (Object v) =
141+
ApiGatewayRequestContextIdentity
142+
<$> v .: "cognitoIdentityPoolId"
143+
<*> v .: "accountId"
144+
<*> v .: "cognitoIdentityId"
145+
<*> v .: "caller"
146+
<*> v .: "sourceIp"
147+
<*> v .: "principalOrgId"
148+
<*> v .: "accessKey"
149+
<*> v .: "cognitoAuthenticationType"
150+
<*> v .: "cognitoAuthenticationProvider"
151+
<*> v .: "userArn"
152+
<*> v .: "userAgent"
153+
<*> v .: "user"
143154
parseJSON _ = fail "Expected ApiGatewayRequestContextIdentity to be an object."
144155

145-
newtype ApiGatewayResponseBody =
146-
ApiGatewayResponseBody Text
156+
newtype ApiGatewayResponseBody
157+
= ApiGatewayResponseBody Text
147158
deriving newtype (ToJSON, FromJSON)
148159

149160
class ToApiGatewayResponseBody a where
@@ -159,33 +170,36 @@ instance {-# OVERLAPPING #-} ToApiGatewayResponseBody String where
159170
instance ToJSON a => ToApiGatewayResponseBody a where
160171
toApiGatewayResponseBody = ApiGatewayResponseBody . toJSONText
161172

162-
data ApiGatewayResponse body = ApiGatewayResponse
163-
{ apiGatewayResponseStatusCode :: !Int
164-
, apiGatewayResponseHeaders :: !ResponseHeaders
165-
, apiGatewayResponseBody :: !body
166-
, apiGatewayResponseIsBase64Encoded :: !Bool
167-
} deriving (Generic, Show)
173+
data ApiGatewayResponse body
174+
= ApiGatewayResponse
175+
{ apiGatewayResponseStatusCode :: !Int,
176+
apiGatewayResponseHeaders :: !ResponseHeaders,
177+
apiGatewayResponseBody :: !body,
178+
apiGatewayResponseIsBase64Encoded :: !Bool
179+
}
180+
deriving (Generic, Show)
168181

169182
instance Functor ApiGatewayResponse where
170-
fmap f v = v { apiGatewayResponseBody = f (apiGatewayResponseBody v) }
183+
fmap f v = v {apiGatewayResponseBody = f (apiGatewayResponseBody v)}
171184

172-
instance ToJSON body => ToJSON (ApiGatewayResponse body) where
185+
instance ToJSON body => ToJSON (ApiGatewayResponse body) where
173186
toJSON = apiGatewayResponseToJSON toJSON
174187

175188
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-
]
189+
apiGatewayResponseToJSON bodyTransformer ApiGatewayResponse {..} =
190+
object
191+
[ "statusCode" .= apiGatewayResponseStatusCode,
192+
"body" .= bodyTransformer apiGatewayResponseBody,
193+
"headers" .= object (map headerToPair apiGatewayResponseHeaders),
194+
"isBase64Encoded" .= apiGatewayResponseIsBase64Encoded
195+
]
182196

183197
mkApiGatewayResponse :: Int -> payload -> ApiGatewayResponse payload
184198
mkApiGatewayResponse code payload =
185199
ApiGatewayResponse code [] payload False
186200

187201
headerToPair :: Header -> T.Pair
188202
headerToPair (cibyte, bstr) = k .= v
189-
where
190-
k = (T.decodeUtf8 . CI.original) cibyte
191-
v = T.decodeUtf8 bstr
203+
where
204+
k = (T.decodeUtf8 . CI.original) cibyte
205+
v = T.decodeUtf8 bstr

0 commit comments

Comments
 (0)