1. AWS PHP SDK v3
Asynchronous Requests
Michael Dowling
@mtdowling
2. AWS PHP SDK v3
Asynchronous Requests
Michael Dowling
@mtdowling
3. What we’ll cover
• Promises
• Handlers and Middleware
• ReactPHP in the SDK
4. • A promise represents the eventual result of an
asynchronous operation.
• Promises are values and are composable
https://promisesaplus.com/
Promises/A+
5. 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/
7. 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.
18. 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.
20. 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.
24. Anatomy of a middleware
Example: Validation middleware.
2)
Shared
state
3)
Return
a
composable
function
4)
Return
a
new
composed
handler
5)
Call
the
next
handler
and
return
a
promise
1)
Wrapping
function
26. • 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
27. 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>
29. 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