From c5a113175a30e8817e34dae965a2dd50591aad89 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sun, 26 Jan 2025 11:05:01 +0100 Subject: [PATCH] Adapt to changes in web API --- .github/workflows/ci.yml | 2 +- ChangeLog.md | 3 +++ composer.json | 4 ++-- .../amazon/aws/lambda/FromApiGateway.class.php | 2 +- .../php/com/amazon/aws/lambda/HttpApi.class.php | 7 +++---- .../amazon/aws/lambda/HttpStreaming.class.php | 6 +++--- .../php/com/amazon/aws/lambda/Tracing.class.php | 16 +++++++++------- .../lambda/unittest/FromApiGatewayTest.class.php | 8 ++++---- .../aws/lambda/unittest/InvocationTest.class.php | 2 +- 9 files changed, 27 insertions(+), 23 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 774a61c..95f34f6 100755 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,7 +15,7 @@ jobs: strategy: fail-fast: false matrix: - php-versions: ['7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3', '8.4'] + php-versions: ['7.4', '8.0', '8.1', '8.2', '8.3', '8.4'] os: [ubuntu-latest, windows-latest] steps: diff --git a/ChangeLog.md b/ChangeLog.md index 95f2668..6064063 100755 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -3,6 +3,9 @@ AWS Lambda Webservices change log ## ?.?.? / ????-??-?? +* **Heads up:** Dropped support for PHP < 7.4, see xp-framework/rfc#343 + (@thekid) + ## 2.3.0 / 2024-05-20 * Added support for dispatching on application level, introduced in diff --git a/composer.json b/composer.json index bfcd4aa..a203ba2 100755 --- a/composer.json +++ b/composer.json @@ -8,8 +8,8 @@ "require" : { "xp-framework/core": "^12.0 | ^11.0 | ^10.0", "xp-forge/lambda": "^5.0", - "xp-forge/web": "^4.0 | ^3.0", - "php": ">=7.0.0" + "xp-forge/web": "dev-feature/websockets as 5.0.0", + "php": ">=7.4.0" }, "require-dev" : { "xp-framework/test": "^2.0 | ^1.0" diff --git a/src/main/php/com/amazon/aws/lambda/FromApiGateway.class.php b/src/main/php/com/amazon/aws/lambda/FromApiGateway.class.php index 4e420a9..b580eda 100755 --- a/src/main/php/com/amazon/aws/lambda/FromApiGateway.class.php +++ b/src/main/php/com/amazon/aws/lambda/FromApiGateway.class.php @@ -54,7 +54,7 @@ public function scheme() { return $this->event['headers']['x-forwarded-proto'] ? public function method() { return $this->event['requestContext']['http']['method'] ?? 'GET'; } /** @return string */ - public function uri() { + public function resource() { return $this->event['rawQueryString'] ? $this->event['rawPath'].'?'.$this->event['rawQueryString'] : $this->event['rawPath'] diff --git a/src/main/php/com/amazon/aws/lambda/HttpApi.class.php b/src/main/php/com/amazon/aws/lambda/HttpApi.class.php index 89bef3b..12ff052 100755 --- a/src/main/php/com/amazon/aws/lambda/HttpApi.class.php +++ b/src/main/php/com/amazon/aws/lambda/HttpApi.class.php @@ -24,14 +24,13 @@ public function target() { try { foreach ($app->service($req->pass('context', $context)->pass('request', $in->context()), $res) ?? [] as $_) { } - $this->tracing->log($req, $res); + $this->tracing->exchange($req, $res, $res->trace + ['traceId' => $context->traceId]); $res->end(); return $res->output()->document; } catch (Throwable $t) { - $e= $t instanceof Error ? $t : new InternalServerError($t); - $this->tracing->log($req, $res, $e); - return $res->output()->error($e); + $this->tracing->exchange($req, $res, $res->trace + ['traceId' => $context->traceId, 'error' => $t]); + return $res->output()->error($t instanceof Error ? $t : new InternalServerError($t)); } }; } diff --git a/src/main/php/com/amazon/aws/lambda/HttpStreaming.class.php b/src/main/php/com/amazon/aws/lambda/HttpStreaming.class.php index 8cb5895..22c7c26 100755 --- a/src/main/php/com/amazon/aws/lambda/HttpStreaming.class.php +++ b/src/main/php/com/amazon/aws/lambda/HttpStreaming.class.php @@ -24,13 +24,13 @@ public function target() { try { foreach ($app->service($req->pass('context', $context)->pass('request', $in->context()), $res) ?? [] as $_) { } - $this->tracing->log($req, $res); + $this->tracing->exchange($req, $res, $res->trace + ['traceId' => $context->traceId]); $res->end(); } catch (Throwable $t) { - $e= $t instanceof Error ? $t : new InternalServerError($t); - $this->tracing->log($req, $res, $e); + $this->tracing->exchange($req, $res, $res->trace + ['traceId' => $context->traceId, 'error' => $t]); + $e= $t instanceof Error ? $t : new InternalServerError($t); $res->answer($e->status(), $e->getMessage()); $res->header('x-amzn-ErrorType', nameof($e)); $res->send($e->compoundMessage(), 'text/plain'); diff --git a/src/main/php/com/amazon/aws/lambda/Tracing.class.php b/src/main/php/com/amazon/aws/lambda/Tracing.class.php index 2841520..291b3dd 100755 --- a/src/main/php/com/amazon/aws/lambda/Tracing.class.php +++ b/src/main/php/com/amazon/aws/lambda/Tracing.class.php @@ -1,20 +1,22 @@ uri()->query(); + parent::__construct(new ToFunction(function($status, $method, $resource, $hints) use($environment) { + $traceId= $hints['traceId']; + unset($hints['traceId']); $environment->trace(sprintf( 'TRACE [%s] %d %s %s %s', - $request->value('context')->traceId, - $response->status(), - $request->method(), - $request->uri()->path().($query ? '?'.$query : ''), - $error ? $error->toString() : '' + $traceId, + $status, + $method, + $resource, + Objects::stringOf($hints) )); })); } diff --git a/src/test/php/com/amazon/aws/lambda/unittest/FromApiGatewayTest.class.php b/src/test/php/com/amazon/aws/lambda/unittest/FromApiGatewayTest.class.php index 31c4259..e80bdcd 100755 --- a/src/test/php/com/amazon/aws/lambda/unittest/FromApiGatewayTest.class.php +++ b/src/test/php/com/amazon/aws/lambda/unittest/FromApiGatewayTest.class.php @@ -79,13 +79,13 @@ public function method($name) { } #[Test] - public function uri() { - Assert::equals('/default/test', $this->fixture('GET')->uri()); + public function resource() { + Assert::equals('/default/test', $this->fixture('GET')->resource()); } #[Test] - public function uri_including_query() { - Assert::equals('/default/test?name=Test', $this->fixture('GET', 'name=Test')->uri()); + public function resource_including_query() { + Assert::equals('/default/test?name=Test', $this->fixture('GET', 'name=Test')->resource()); } #[Test, Values([[[]], [['accept' => '*/*', 'user-agent' => 'test']]])] diff --git a/src/test/php/com/amazon/aws/lambda/unittest/InvocationTest.class.php b/src/test/php/com/amazon/aws/lambda/unittest/InvocationTest.class.php index 0a1c3fe..c478219 100755 --- a/src/test/php/com/amazon/aws/lambda/unittest/InvocationTest.class.php +++ b/src/test/php/com/amazon/aws/lambda/unittest/InvocationTest.class.php @@ -297,7 +297,7 @@ public function writes_trace_message() { $this->invoke($fixture, 'GET', 'name=Test'); Assert::equals( - "TRACE [Root=1-dc99d00f-c079a84d433534434534ef0d;Parent=91ed514f1e5c03b2;Sampled=1] 200 GET /default/test?name=Test \n", + "TRACE [Root=1-dc99d00f-c079a84d433534434534ef0d;Parent=91ed514f1e5c03b2;Sampled=1] 200 GET /default/test?name=Test []\n", $this->trace->bytes() ); }