|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Cerbero\LazyJsonPages\Concerns; |
|
4
|
|
|
|
|
5
|
|
|
use Cerbero\LazyJsonPages\Outcome; |
|
6
|
|
|
use GuzzleHttp\Client; |
|
7
|
|
|
use GuzzleHttp\Pool; |
|
8
|
|
|
use GuzzleHttp\Psr7\Uri; |
|
9
|
|
|
use Illuminate\Support\LazyCollection; |
|
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
11
|
|
|
use Throwable; |
|
12
|
|
|
use Traversable; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* The trait to handle APIs with the number of total pages. |
|
16
|
|
|
* |
|
17
|
|
|
*/ |
|
18
|
|
|
trait HandlesTotalPages |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Handle APIs with the given number of total pages |
|
22
|
|
|
* |
|
23
|
|
|
* @param int $pages |
|
24
|
|
|
* @param Uri|null $uri |
|
25
|
|
|
* @return Traversable |
|
26
|
|
|
*/ |
|
27
|
11 |
|
protected function handleByTotalPages(int $pages, Uri $uri = null): Traversable |
|
28
|
|
|
{ |
|
29
|
11 |
|
$uri = $uri ?: $this->config->source->request->getUri(); |
|
30
|
11 |
|
$firstPageAlreadyFetched = strval($uri) == strval($this->config->source->request->getUri()); |
|
31
|
11 |
|
$chunkedPages = $this->chunkPages($pages, $firstPageAlreadyFetched); |
|
32
|
11 |
|
$items = $this->fetchItemsAsynchronously($chunkedPages, $uri); |
|
33
|
|
|
|
|
34
|
11 |
|
if ($firstPageAlreadyFetched) { |
|
35
|
7 |
|
yield from $this->config->source->json($this->config->path); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
11 |
|
yield from $items; |
|
39
|
10 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Retrieve the given pages in chunks |
|
43
|
|
|
* |
|
44
|
|
|
* @param int $pages |
|
45
|
|
|
* @param bool $skipFirstPage |
|
46
|
|
|
* @return iterable |
|
47
|
|
|
*/ |
|
48
|
11 |
|
protected function chunkPages(int $pages, bool $skipFirstPage): iterable |
|
49
|
|
|
{ |
|
50
|
11 |
|
$firstPage = $skipFirstPage ? $this->config->firstPage + 1 : $this->config->firstPage; |
|
51
|
11 |
|
$lastPage = $this->config->firstPage == 0 ? $pages - 1 : $pages; |
|
52
|
|
|
|
|
53
|
11 |
|
return LazyCollection::range($firstPage, $lastPage)->chunk($this->config->chunk ?: INF); |
|
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Fetch items by performing asynchronous HTTP calls |
|
58
|
|
|
* |
|
59
|
|
|
* @param iterable $chunkedPages |
|
60
|
|
|
* @param Uri $uri |
|
61
|
|
|
* @return Traversable |
|
62
|
|
|
*/ |
|
63
|
11 |
|
protected function fetchItemsAsynchronously(iterable $chunkedPages, Uri $uri): Traversable |
|
64
|
|
|
{ |
|
65
|
11 |
|
$client = new Client(['timeout' => $this->config->timeout]); |
|
66
|
|
|
|
|
67
|
11 |
|
foreach ($chunkedPages as $pages) { |
|
68
|
11 |
|
$outcome = $this->retry(function (Outcome $outcome) use ($uri, $client, $pages) { |
|
|
|
|
|
|
69
|
11 |
|
$pages = $outcome->pullFailedPages() ?: $pages; |
|
70
|
|
|
|
|
71
|
11 |
|
return $this->pool($client, $outcome, function () use ($uri, $pages) { |
|
72
|
11 |
|
$request = clone $this->config->source->request; |
|
73
|
|
|
|
|
74
|
11 |
|
foreach ($pages as $page) { |
|
75
|
11 |
|
yield $page => $request->withUri(Uri::withQueryValue($uri, $this->config->pageName, $page)); |
|
76
|
|
|
} |
|
77
|
11 |
|
}); |
|
78
|
11 |
|
}); |
|
79
|
|
|
|
|
80
|
10 |
|
yield from $outcome->pullItems(); |
|
81
|
|
|
} |
|
82
|
10 |
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Retrieve the outcome of a pool of asynchronous requests |
|
86
|
|
|
* |
|
87
|
|
|
* @param Client $client |
|
88
|
|
|
* @param Outcome $outcome |
|
89
|
|
|
* @param callable $getRequests |
|
90
|
|
|
* @return Outcome |
|
91
|
|
|
*/ |
|
92
|
11 |
|
protected function pool(Client $client, Outcome $outcome, callable $getRequests): Outcome |
|
93
|
|
|
{ |
|
94
|
11 |
|
$pool = new Pool($client, $getRequests(), [ |
|
95
|
11 |
|
'concurrency' => $this->config->concurrency, |
|
96
|
11 |
|
'fulfilled' => function (ResponseInterface $response, int $page) use ($outcome) { |
|
97
|
11 |
|
$outcome->addItemsFromPage($page, $response, $this->config->path); |
|
98
|
11 |
|
}, |
|
99
|
11 |
|
'rejected' => function (Throwable $e, int $page) use ($outcome) { |
|
100
|
1 |
|
$outcome->addFailedPage($page); |
|
101
|
1 |
|
throw $e; |
|
102
|
11 |
|
} |
|
103
|
|
|
]); |
|
104
|
|
|
|
|
105
|
11 |
|
$pool->promise()->wait(); |
|
106
|
|
|
|
|
107
|
10 |
|
return $outcome; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|