From e1de397b2a77c6c8cdee3c7f0843400906b5c114 Mon Sep 17 00:00:00 2001 From: Yada Clintjens Date: Mon, 4 Mar 2024 14:07:36 +0100 Subject: [PATCH] Improve documentation and examples --- README.md | 18 +++++++++++++++--- examples/01-transform.php | 13 +++++-------- examples/02-transform-all.php | 10 ++++------ examples/03-transform-any.php | 10 ++++------ 4 files changed, 28 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 7d93e01..6f19f9f 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ operations, but keeping thousands of jobs in memory at once may easily take up all resources on your side. Instead, you can use this library to stream your arbitrarily large input list as individual records to a non-blocking (async) transformation handler. It uses -[ReactPHP](https://reactphp.org) to enable you to concurrently process multiple +[ReactPHP](https://reactphp.org/) to enable you to concurrently process multiple records at once. You can control the concurrency limit, so that by allowing it to process 10 operations at the same time, you can thus process this large input list around 10 times faster and at the same time you're no longer limited @@ -72,13 +72,17 @@ Once [installed](#install), you can use the following code to process an example user lists by sending a (RESTful) HTTP API request for each user record: ```php +get("https://ipapi.co/$user[ip]/country_name/")->then( - function (ResponseInterface $response) use ($user) { + function (Psr\Http\Message\ResponseInterface $response) use ($user) { // response successfully received // add country to user array and return updated user $user['country'] = (string)$response->getBody(); return $user; + }, function (Exception $e) { + echo 'Error: ' . $e->getMessage() . PHP_EOL; } ); }); @@ -406,6 +412,8 @@ $transformer = new Transformer(10, function ($data) use ($http) { return $http->post('https://example.com/?id=' . $data['id'])->then( function ($response) use ($data) { return array('done' => $data['id']); + }, function (Exception $e) { + echo 'Error: ' . $e->getMessage() . PHP_EOL; } ); }); @@ -456,6 +464,8 @@ $promise = Transformer::all($input, 3, function ($data) use ($browser, $url) { $promise->then(function ($count) { echo 'All ' . $count . ' jobs successful!' . PHP_EOL; +}, function (Exception $e) { + echo 'Error: ' . $e->getMessage() . PHP_EOL; }); ``` @@ -561,6 +571,8 @@ $promise = Transformer::any($input, 3, function ($data) use ($browser, $url) { $promise->then(function (ResponseInterface $response) { echo 'First successful job: ' . $response->getBody() . PHP_EOL; +}, function (Exception $e) { + echo 'Error: ' . $e->getMessage() . PHP_EOL; }); ``` diff --git a/examples/01-transform.php b/examples/01-transform.php index 1d50b0b..f08b3ab 100644 --- a/examples/01-transform.php +++ b/examples/01-transform.php @@ -1,8 +1,5 @@ get("https://ipapi.co/$user[ip]/country_name/")->then( - function (ResponseInterface $response) use ($user) { + function (Psr\Http\Message\ResponseInterface $response) use ($user) { // response successfully received // add country to user array and return updated user $user['country'] = (string)$response->getBody(); return $user; - } - ); + }, function (Exception $e) { + echo 'Error: ' . $e->getMessage() . PHP_EOL; + }); }); // load a huge number of users to process from NDJSON file @@ -50,4 +48,3 @@ function (ResponseInterface $response) use ($user) { echo '[DONE]' . PHP_EOL; }); $transformer->on('error', 'printf'); - diff --git a/examples/02-transform-all.php b/examples/02-transform-all.php index c62f21e..c69370f 100644 --- a/examples/02-transform-all.php +++ b/examples/02-transform-all.php @@ -1,8 +1,5 @@ post( $url, array('Content-Type' => 'application/json'), json_encode($user) - )->then(function (ResponseInterface $response) { + )->then(function (Psr\Http\Message\ResponseInterface $response) { // demo HTTP response validation $body = json_decode($response->getBody()); if (!isset($body->json)) { throw new RuntimeException('Unexpected response'); } + }, function (Exception $e) { + echo 'Error: ' . $e->getMessage() . PHP_EOL; }); }); @@ -46,4 +45,3 @@ function (Exception $e) { } } ); - diff --git a/examples/03-transform-any.php b/examples/03-transform-any.php index a1e5134..0523c9a 100644 --- a/examples/03-transform-any.php +++ b/examples/03-transform-any.php @@ -1,8 +1,5 @@ post( $url, array('Content-Type' => 'application/json'), json_encode($user) - )->then(function (ResponseInterface $response) use ($user) { + )->then(function (Psr\Http\Message\ResponseInterface $response) use ($user) { // demo HTTP response validation $body = json_decode($response->getBody()); if (!isset($body->json)) { @@ -36,6 +33,8 @@ // demo result includes full user from NDJSON with additional properties $user['result'] = $body; return $user; + }, function (Exception $e) { + echo 'Error: ' . $e->getMessage() . PHP_EOL; }); }); @@ -50,4 +49,3 @@ function (Exception $e) { } } ); -