-
Notifications
You must be signed in to change notification settings - Fork 0
/
API.php
42 lines (36 loc) · 1.15 KB
/
API.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
use Rx\Observable;
use Rxnet\Http\Http;
use Rxnet\Operator\RetryWithDelay;
class API
{
private $http;
private $endpoint;
public function __construct($endpoint = "https://api.cryptokitties.co")
{
$this->http = new Http();
$this->endpoint = $endpoint;
}
public function getKitties(int $limit = 20, int $offset = 0): Observable
{
return $this->call("/kitties?limit={$limit}&offset={$offset}");
}
public function getKitten(int $id): Observable
{
return $this->call("/kitties/{$id}");
}
private function call(string $uri, int $timeout = 10000, int $retry = 5): Observable
{
return $this->http->get($this->endpoint . $uri)
->timeout($timeout)
->retryWhen(new RetryWithDelay($retry, random_int(1000, $timeout)))
->map(function(Psr\Http\Message\ResponseInterface $response) {
return json_decode((string) $response->getBody(), true);
})
->doOnError(function(\Exception $e) use ($uri) {
echo "\nError on {$uri} \n";
echo $e->getMessage();
})
;
}
}