@@ -50,7 +50,7 @@ type api_gateway_request_identity =
5050 ; user_agent : string option [@ key "userAgent" ]
5151 ; user : string option
5252 }
53- [@@ deriving of_yojson ]
53+ [@@ deriving yojson ]
5454
5555(* APIGatewayProxyRequestContext contains the information to identify the AWS
5656 * account and resources invoking the Lambda function. It also includes Cognito
@@ -69,7 +69,7 @@ type api_gateway_proxy_request_context =
6969 ; path : string option [@ default None ]
7070 ; api_id : string [@ key "apiId" ] (* The API Gateway REST API ID *)
7171 }
72- [@@ deriving of_yojson { strict = false }]
72+ [@@ deriving yojson { strict = false }]
7373
7474type api_gateway_proxy_request =
7575 { resource : string
@@ -86,7 +86,7 @@ type api_gateway_proxy_request =
8686 ; body : string option
8787 ; is_base64_encoded : bool [@ key "isBase64Encoded" ]
8888 }
89- [@@ deriving of_yojson { strict = false }]
89+ [@@ deriving yojson { strict = false }]
9090
9191type api_gateway_proxy_response =
9292 { status_code : int [@ key "statusCode" ]
@@ -97,13 +97,103 @@ type api_gateway_proxy_response =
9797[@@ deriving to_yojson ]
9898
9999module API_gateway_request = struct
100- type t = api_gateway_proxy_request [@@ deriving of_yojson ]
100+ type t = api_gateway_proxy_request [@@ deriving yojson ]
101+
102+ let mock (ctx : 'a Piaf.Server.ctx ) =
103+ let piaf_headers_to_string_map headers =
104+ headers
105+ |> Piaf.Headers. to_list
106+ |> List. to_seq
107+ |> StringMap. of_seq
108+ in
109+ let identity = { cognito_identity_pool_id = None
110+ ; account_id = None
111+ ; cognito_identity_id = None
112+ ; caller = None
113+ ; access_key = None
114+ ; api_key = None
115+ (* TODO: Use the actual IP address from Unix.sockaddr *)
116+ ; source_ip = " 127.0.0.1"
117+ ; cognito_authentication_type = None
118+ ; cognito_authentication_provider = None
119+ ; user_arn = None
120+ ; user_agent = Piaf.Headers. get ctx.request.headers " User-Agent"
121+ ; user = None }
122+ in
123+ let uri = Piaf.Request. uri ctx.request in
124+ let request_id =
125+ (* TODO: Not sure if this is equivalent to aws_request_id, but probably. Need to confirm. *)
126+ Random. self_init () ;
127+ Uuidm. to_string @@ Uuidm. v4_gen (Random. get_state () ) ()
128+ in
129+ let request_context = { account_id = " 123456789012"
130+ ; resource_id = " 123456"
131+ ; stage = " dev"
132+ ; request_id
133+ ; identity
134+ ; resource_path = " /{proxy+}"
135+ ; authorizer = None
136+ ; http_method = Piaf.Method. to_string ctx.request.meth
137+ ; protocol = Some (Piaf.Versions.HTTP. to_string ctx.request.version)
138+ ; path = Some (Uri. path uri)
139+ ; api_id = " 1234567890" }
140+ in
141+ let body =
142+ Lwt_result. map_err Piaf.Error. to_string (Piaf.Body. to_string ctx.request.body)
143+ in
144+ let query_string_parameters =
145+ Uri. query uri
146+ |> List. map (fun (key , values ) ->
147+ match List. length values with
148+ | 0 -> (key, " " )
149+ | 1 -> (key, List. hd values)
150+ (* TODO: Property handle this one *)
151+ | _ -> failwith " Multiple values not supported for query strings for now" )
152+ |> List. to_seq
153+ |> StringMap. of_seq
154+ in
155+ Lwt_result. map (fun body ->
156+ (to_yojson { resource = (Uri. path uri)
157+ ; path = " /{proxy+}"
158+ ; http_method = request_context.http_method
159+ ; headers = piaf_headers_to_string_map ctx.request.headers
160+ ; query_string_parameters
161+ ; path_parameters = StringMap. empty
162+ ; stage_variables = StringMap. empty
163+ ; request_context
164+ ; body = if body = String. empty then None else Some body
165+ ; is_base64_encoded = false }))
166+ body
167+
168+ let response _ =
169+ Error " Not implemented"
101170end
102171
103172module API_gateway_response = struct
104173 type t = api_gateway_proxy_response [@@ deriving to_yojson ]
105174
106175 let to_yojson t = Lwt. return (to_yojson t)
176+
177+ let mock _ =
178+ Lwt_result. fail " Not implemented"
179+
180+ let response response =
181+ let string_map_to_piaf_headers headers =
182+ headers
183+ |> StringMap. to_seq
184+ |> List. of_seq
185+ |> Piaf.Headers. of_list
186+ in
187+ (* TODO: Support base64? *)
188+ assert (not response.is_base64_encoded);
189+
190+ let response =
191+ Piaf.Response. of_string
192+ ~headers: (string_map_to_piaf_headers response.headers)
193+ ~body: (response.body)
194+ (Piaf.Status. of_code response.status_code)
195+ in
196+ Ok response
107197end
108198
109199include Runtime. Make (API_gateway_request ) (API_gateway_response )
0 commit comments