Skip to content

Commit

Permalink
Add iterator examples
Browse files Browse the repository at this point in the history
  • Loading branch information
kelunik committed Mar 17, 2020
1 parent ac7cf5d commit 3a07c0d
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
"amphp/phpunit-util": "^1.1",
"amphp/php-cs-fixer-config": "dev-master",
"phpunit/phpunit": "^7 || ^8",
"amphp/http-server": "^2-rc4"
"amphp/http-server": "^2-rc4",
"kelunik/link-header-rfc5988": "^1.0"
},
"suggest": {
"ext-zlib": "Allows using compression for response bodies.",
Expand Down
78 changes: 78 additions & 0 deletions examples/pagination/iterator-batch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

use Amp\Http\Client\HttpClient;
use Amp\Http\Client\HttpClientBuilder;
use Amp\Http\Client\Request;
use Amp\Http\Client\Response;
use Amp\Iterator;
use Amp\Loop;
use Amp\Producer;
use function Amp\call;
use function Amp\delay;
use function Kelunik\LinkHeaderRfc5988\parseLinks;

require __DIR__ . '/../.helper/functions.php';

class GitHubApi
{
private $httpClient;

public function __construct(HttpClient $httpClient)
{
$this->httpClient = $httpClient;
}

public function getEvents(string $organization): Iterator
{
return new Producer(function (callable $emit) use ($organization) {
$url = 'https://api.github.com/orgs/' . \urlencode($organization) . '/events';

do {
$request = new Request($url);

/** @var Response $response */
$response = yield $this->httpClient->request($request);
$json = yield $response->getBody()->buffer();

if ($response->getStatus() !== 200) {
throw new \Exception('Failed to get events from GitHub: ' . $json);
}

$events = \json_decode($json);
yield $emit($events);

$links = parseLinks($response->getHeader('link'));
$next = $links->getByRel('next');

if ($next) {
print 'Waiting 1000 ms before next request...' . PHP_EOL;
yield delay(1000);

$url = $next->getUri();
}
} while ($url);
});
}
}

Loop::run(static function () {
$httpClient = HttpClientBuilder::buildDefault();
$github = new GitHubApi($httpClient);

$eventBatches = $github->getEvents('amphp');
while (yield $eventBatches->advance()) {
$events = $eventBatches->getCurrent();

$promises = [];
foreach ($events as $event) {
$promises[] = call(static function () use ($event) {
// do something with $event, we just fake some delay here
yield delay(\random_int(1, 100));

print $event->type . ': ' . $event->id . PHP_EOL;
});
}

yield $promises;
}
});
69 changes: 69 additions & 0 deletions examples/pagination/iterator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

use Amp\Http\Client\HttpClient;
use Amp\Http\Client\HttpClientBuilder;
use Amp\Http\Client\Request;
use Amp\Http\Client\Response;
use Amp\Iterator;
use Amp\Loop;
use Amp\Producer;
use function Amp\delay;
use function Kelunik\LinkHeaderRfc5988\parseLinks;

require __DIR__ . '/../.helper/functions.php';

class GitHubApi
{
private $httpClient;

public function __construct(HttpClient $httpClient)
{
$this->httpClient = $httpClient;
}

public function getEvents(string $organization): Iterator
{
return new Producer(function (callable $emit) use ($organization) {
$url = 'https://api.github.com/orgs/' . \urlencode($organization) . '/events';

do {
$request = new Request($url);

/** @var Response $response */
$response = yield $this->httpClient->request($request);
$json = yield $response->getBody()->buffer();

if ($response->getStatus() !== 200) {
throw new \Exception('Failed to get events from GitHub: ' . $json);
}

$events = \json_decode($json);
foreach ($events as $event) {
yield $emit($event);
}

$links = parseLinks($response->getHeader('link') ?? '');
$next = $links->getByRel('next');

if ($next) {
print 'Waiting 1000 ms before next request...' . PHP_EOL;
yield delay(1000);

$url = $next->getUri();
}
} while ($url);
});
}
}

Loop::run(static function () {
$httpClient = HttpClientBuilder::buildDefault();
$github = new GitHubApi($httpClient);

$events = $github->getEvents('amphp');
while (yield $events->advance()) {
$event = $events->getCurrent();

print $event->type . ': ' . $event->id . PHP_EOL;
}
});

0 comments on commit 3a07c0d

Please sign in to comment.