Skip to content

Commit 6492ac8

Browse files
authored
Merge pull request #19 from xp-forge/refactor/extract-to-aws-core
Extract AWS core library
2 parents 8b8b092 + 98bea8a commit 6492ac8

File tree

5 files changed

+21
-79
lines changed

5 files changed

+21
-79
lines changed

README.md

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -142,32 +142,35 @@ $ aws lambda update-function-configuration \
142142

143143
Using other AWS services
144144
------------------------
145-
In order to programmatically use other AWS services such as SQS with the [AWS SDK for PHP](https://docs.aws.amazon.com/sdk-for-php/index.html), pass the credentials via environment:
145+
In order to programmatically use other AWS services use the *ServiceEndpoint* class:
146146

147147
```php
148-
use Aws\Sqs\SqsClient;
149-
use Aws\Credentials\CredentialProvider;
148+
use com\amazon\aws\{Credentials, ServiceEndpoint};
150149
use com\amazon\aws\lambda\Handler;
151150

152-
class Trigger extends Handler {
151+
class Streaming extends Handler {
153152

154153
/** @return com.amazon.aws.lambda.Lambda|callable */
155154
public function target() {
156-
$queue= new SqsClient([
157-
'version' => '2012-11-05',
158-
'region' => $this->environment->variable('AWS_REGION'),
159-
'credentials' => CredentialProvider::env(),
160-
]);
161-
162-
// ...omitted for brevity...
155+
return function($event, $context) {
156+
157+
// Send message to WebSocket connection
158+
(new ServiceEndpoint('execute-api', $this->environment->credentials()))
159+
->in($context->region)
160+
->using($event['requestContext']['apiId'])
161+
->resource('/{stage}/@connections/{connectionId}', $event['requestContext'])
162+
->transmit(['message' => 'Reply'])
163+
;
164+
return ['statusCode' => 200];
165+
};
163166
}
164167
}
165168
```
166169

167170
To test this locally, pass the necessary environment variables via *-e* on the command line:
168171

169172
```bash
170-
$ xp lambda test -e AWS_ACCESS_KEY_ID=... -e AWS_SECRET_ACCESS_KEY=... Trigger
173+
$ xp lambda test -e AWS_ACCESS_KEY_ID=... -e AWS_SECRET_ACCESS_KEY=... Streaming '{"requestContext":...}'
171174
# ...
172175
```
173176

@@ -216,7 +219,7 @@ public class com.amazon.aws.lambda.Environment {
216219
public function path(string $path): io.Path
217220
public function tempDir(): io.Path
218221
public function variable(string $name): ?string
219-
public function credentials(): com.amazon.aws.lambda.Credentials
222+
public function credentials(): com.amazon.aws.Credentials
220223
public function trace(var... $args): void
221224
public function properties(string $name): util.PropertyAccess
222225
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"xp-framework/core": "^11.0 | ^10.14",
1010
"xp-framework/http": "^10.0 | ^9.0",
1111
"xp-framework/zip": "^10.0 | ^9.0",
12+
"xp-forge/aws": "^1.0",
1213
"xp-forge/json": "^5.0 | ^4.0",
1314
"php": ">=7.0.0"
1415
},

src/main/php/com/amazon/aws/lambda/Credentials.class.php

Lines changed: 0 additions & 64 deletions
This file was deleted.

src/main/php/com/amazon/aws/lambda/Environment.class.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php namespace com\amazon\aws\lambda;
22

3+
use com\amazon\aws\Credentials;
34
use io\Path;
45
use io\streams\StringWriter;
56
use lang\{ElementNotFoundException, Environment as System};
@@ -50,7 +51,7 @@ public function variable($name) {
5051
/**
5152
* Returns credentials from this environment
5253
*
53-
* @return com.amazon.aws.lambda.Credentials
54+
* @return com.amazon.aws.Credentials
5455
*/
5556
public function credentials() {
5657
return new Credentials(

src/test/php/com/amazon/aws/lambda/unittest/EnvironmentTest.class.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php namespace com\amazon\aws\lambda\unittest;
22

3-
use com\amazon\aws\lambda\{Environment, Credentials};
3+
use com\amazon\aws\Credentials;
4+
use com\amazon\aws\lambda\Environment;
45
use io\streams\{MemoryOutputStream, StringWriter};
56
use io\{File, Files, Path};
67
use lang\ElementNotFoundException;

0 commit comments

Comments
 (0)