php sdk-v3-async
TRANSCRIPT
![Page 1: Php sdk-v3-async](https://reader031.vdocuments.us/reader031/viewer/2022021500/5a6ec6e47f8b9a40118b4de9/html5/thumbnails/1.jpg)
AWS PHP SDK v3Asynchronous Requests
Michael Dowling @mtdowling
![Page 2: Php sdk-v3-async](https://reader031.vdocuments.us/reader031/viewer/2022021500/5a6ec6e47f8b9a40118b4de9/html5/thumbnails/2.jpg)
AWS PHP SDK v3Asynchronous Requests
Michael Dowling @mtdowling
![Page 3: Php sdk-v3-async](https://reader031.vdocuments.us/reader031/viewer/2022021500/5a6ec6e47f8b9a40118b4de9/html5/thumbnails/3.jpg)
What we’ll cover
• Promises
• Handlers and Middleware
• ReactPHP in the SDK
![Page 4: Php sdk-v3-async](https://reader031.vdocuments.us/reader031/viewer/2022021500/5a6ec6e47f8b9a40118b4de9/html5/thumbnails/4.jpg)
• A promise represents the eventual result of an asynchronous operation.
• Promises are values and are composable
https://promisesaplus.com/
Promises/A+
![Page 5: Php sdk-v3-async](https://reader031.vdocuments.us/reader031/viewer/2022021500/5a6ec6e47f8b9a40118b4de9/html5/thumbnails/5.jpg)
Promises• A promise has a then() method, which allows
access to its current or eventual value or reason.
• Promises are resolved with a value or rejected with a reason.
• State transitions: Pending -> (Fulfilled | Rejected)
• Fulfilled and rejected promises are immutable.
https://promisesaplus.com/
![Page 6: Php sdk-v3-async](https://reader031.vdocuments.us/reader031/viewer/2022021500/5a6ec6e47f8b9a40118b4de9/html5/thumbnails/6.jpg)
Promises
![Page 7: Php sdk-v3-async](https://reader031.vdocuments.us/reader031/viewer/2022021500/5a6ec6e47f8b9a40118b4de9/html5/thumbnails/7.jpg)
then()• Calling then() on a promise will return a new promise
that is fulfilled or rejected based on the result of invoking the provided then($onFullfilled, $onRejected) callbacks.
• If a callback throws, the promise is rejected and downstream promises are rejected.
• If a rejected callback does not throw, downstream promises are fulfilled.
• If a promise is fulfilled or rejected with a promise, any callbacks registered on the promise will not be invoked until the promise is resolved.
![Page 8: Php sdk-v3-async](https://reader031.vdocuments.us/reader031/viewer/2022021500/5a6ec6e47f8b9a40118b4de9/html5/thumbnails/8.jpg)
then()
![Page 9: Php sdk-v3-async](https://reader031.vdocuments.us/reader031/viewer/2022021500/5a6ec6e47f8b9a40118b4de9/html5/thumbnails/9.jpg)
Promises in the SDK
1.
2.
![Page 10: Php sdk-v3-async](https://reader031.vdocuments.us/reader031/viewer/2022021500/5a6ec6e47f8b9a40118b4de9/html5/thumbnails/10.jpg)
• Multiple promises can be composed together into a single promise using combinators
• Guzzle ships with several combinators
Promise Combinators
![Page 11: Php sdk-v3-async](https://reader031.vdocuments.us/reader031/viewer/2022021500/5a6ec6e47f8b9a40118b4de9/html5/thumbnails/11.jpg)
Promise Combinator Examples
![Page 12: Php sdk-v3-async](https://reader031.vdocuments.us/reader031/viewer/2022021500/5a6ec6e47f8b9a40118b4de9/html5/thumbnails/12.jpg)
all($promises) // usage
![Page 13: Php sdk-v3-async](https://reader031.vdocuments.us/reader031/viewer/2022021500/5a6ec6e47f8b9a40118b4de9/html5/thumbnails/13.jpg)
some($promises) // usage
![Page 14: Php sdk-v3-async](https://reader031.vdocuments.us/reader031/viewer/2022021500/5a6ec6e47f8b9a40118b4de9/html5/thumbnails/14.jpg)
any($promises) // usage
![Page 15: Php sdk-v3-async](https://reader031.vdocuments.us/reader031/viewer/2022021500/5a6ec6e47f8b9a40118b4de9/html5/thumbnails/15.jpg)
each_limit($iterable, $concurrency, …)
![Page 16: Php sdk-v3-async](https://reader031.vdocuments.us/reader031/viewer/2022021500/5a6ec6e47f8b9a40118b4de9/html5/thumbnails/16.jpg)
Promise Coroutines
![Page 17: Php sdk-v3-async](https://reader031.vdocuments.us/reader031/viewer/2022021500/5a6ec6e47f8b9a40118b4de9/html5/thumbnails/17.jpg)
SDK Pool
![Page 18: Php sdk-v3-async](https://reader031.vdocuments.us/reader031/viewer/2022021500/5a6ec6e47f8b9a40118b4de9/html5/thumbnails/18.jpg)
Handlers and Middleware• A handler is a function that performs the actual
transformation of a command and request into a result (e.g., Guzzle is a handler).
• A middleware is a higher-order function that modifies a command, request, or result as it passes through the middleware.
• A HandlerList is used by clients and commands to compose middleware on a handler.
![Page 19: Php sdk-v3-async](https://reader031.vdocuments.us/reader031/viewer/2022021500/5a6ec6e47f8b9a40118b4de9/html5/thumbnails/19.jpg)
Handlers
![Page 20: Php sdk-v3-async](https://reader031.vdocuments.us/reader031/viewer/2022021500/5a6ec6e47f8b9a40118b4de9/html5/thumbnails/20.jpg)
HandlerList• A client has a HandlerList that contains a
handler and a list of middleware.
• Each command created by a client get a clone of the client’s HandlerList that allows per/command customizations.
![Page 21: Php sdk-v3-async](https://reader031.vdocuments.us/reader031/viewer/2022021500/5a6ec6e47f8b9a40118b4de9/html5/thumbnails/21.jpg)
HandlerList• Request lifecycle in the HandlerList
init -> validate -> build -> sign
sign -> build -> validate -> init
execute
returns Promise<Result, Exception>
Command
![Page 22: Php sdk-v3-async](https://reader031.vdocuments.us/reader031/viewer/2022021500/5a6ec6e47f8b9a40118b4de9/html5/thumbnails/22.jpg)
Adding middleware to HandlerList
![Page 23: Php sdk-v3-async](https://reader031.vdocuments.us/reader031/viewer/2022021500/5a6ec6e47f8b9a40118b4de9/html5/thumbnails/23.jpg)
Middleware
![Page 24: Php sdk-v3-async](https://reader031.vdocuments.us/reader031/viewer/2022021500/5a6ec6e47f8b9a40118b4de9/html5/thumbnails/24.jpg)
Anatomy of a middlewareExample: Validation middleware.
2) Shared state
3) Return acomposablefunction
4) Return anew composedhandler
5) Call thenext handlerand returna promise
1) Wrappingfunction
![Page 25: Php sdk-v3-async](https://reader031.vdocuments.us/reader031/viewer/2022021500/5a6ec6e47f8b9a40118b4de9/html5/thumbnails/25.jpg)
Higher-order middleware
![Page 26: Php sdk-v3-async](https://reader031.vdocuments.us/reader031/viewer/2022021500/5a6ec6e47f8b9a40118b4de9/html5/thumbnails/26.jpg)
• Some handlers support async, some emulate.
• cURL handlers support async but must use wait or you must tick the curl event loop explicitly.
• Some handlers support async interop with event loops…
Custom Handlers
![Page 27: Php sdk-v3-async](https://reader031.vdocuments.us/reader031/viewer/2022021500/5a6ec6e47f8b9a40118b4de9/html5/thumbnails/27.jpg)
Using a custom handler
• http_handler: works at the PSR-7 layer. handler($request, $options): Promise<Response, Error>
• handler: works at the SDK layer. handler($command, $request): Promise<Result, Error>
![Page 28: Php sdk-v3-async](https://reader031.vdocuments.us/reader031/viewer/2022021500/5a6ec6e47f8b9a40118b4de9/html5/thumbnails/28.jpg)
Using a custom handler
![Page 29: Php sdk-v3-async](https://reader031.vdocuments.us/reader031/viewer/2022021500/5a6ec6e47f8b9a40118b4de9/html5/thumbnails/29.jpg)
React Guzzle Handler• The SDK supports Guzzle.
• Cees-Jan Kiewiet created a React Guzzle handler.
• Therefore you can use React with the SDK!
composer require wyrihaximus/react-‐guzzle-‐psr7
https://github.com/WyriHaximus/react-guzzle-psr7
![Page 30: Php sdk-v3-async](https://reader031.vdocuments.us/reader031/viewer/2022021500/5a6ec6e47f8b9a40118b4de9/html5/thumbnails/30.jpg)
Questions?