-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpApi.class.php
More file actions
executable file
·37 lines (31 loc) · 1.31 KB
/
HttpApi.class.php
File metadata and controls
executable file
·37 lines (31 loc) · 1.31 KB
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
<?php namespace com\amazon\aws\lambda;
use Throwable;
use web\{Error, InternalServerError, Request, Response};
/**
* AWS Lambda with Amazon HTTP API Gateway. Uses buffering as streamed responses
* are not supported by API Gateway's LAMBDA_PROXY integration
*
* @test com.amazon.aws.lambda.unittest.HttpApiTest
* @see https://docs.aws.amazon.com/lambda/latest/dg/services-apigateway.html
*/
abstract class HttpApi extends HttpIntegration {
/** @return callable|com.amazon.aws.lambda.Lambda|com.amazon.aws.lambda.Streaming */
public function target() {
$app= $this->application();
// Return event handler
return function($event, $context) use($app) {
$in= new FromApiGateway($event);
$req= new Request($in);
$res= new Response(new ResponseDocument());
try {
foreach ($app->service($req->pass('context', $context)->pass('request', $in->context()), $res) ?? [] as $_) { }
$this->tracing->exchange($req, $res, $res->trace + ['traceId' => $context->traceId]);
$res->end();
return $res->output()->document;
} catch (Throwable $t) {
$this->tracing->exchange($req, $res, $res->trace + ['traceId' => $context->traceId, 'error' => $t]);
return $res->output()->error($t instanceof Error ? $t : new InternalServerError($t));
}
};
}
}