|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace WyriHaximus\ApiClient\Transport; |
|
5
|
|
|
|
|
6
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
|
7
|
|
|
use GuzzleHttp\Psr7\Request; |
|
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
9
|
|
|
use React\Cache\CacheInterface; |
|
10
|
|
|
use React\EventLoop\LoopInterface; |
|
11
|
|
|
use React\Promise\Deferred; |
|
12
|
|
|
use React\Promise\FulfilledPromise; |
|
13
|
|
|
use React\Promise\PromiseInterface; |
|
14
|
|
|
use function React\Promise\reject; |
|
15
|
|
|
use function React\Promise\resolve; |
|
16
|
|
|
use function WyriHaximus\React\futureFunctionPromise; |
|
17
|
|
|
|
|
18
|
|
|
class Client |
|
19
|
|
|
{ |
|
20
|
|
|
const DEFAULT_OPTIONS = [ |
|
21
|
|
|
'schema' => 'https', |
|
22
|
|
|
'path' => '/', |
|
23
|
|
|
'user_agent' => 'WyriHaximus/php-api-client', |
|
24
|
|
|
'headers' => [], |
|
25
|
|
|
]; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var GuzzleClient |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $handler; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var LoopInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $loop; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var array |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $options = []; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var Hydrator |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $hydrator; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var CacheInterface |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $cache; |
|
51
|
|
|
|
|
52
|
|
|
public function __construct(LoopInterface $loop, GuzzleClient $handler, $options = []) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->loop = $loop; |
|
55
|
|
|
$this->handler = $handler; |
|
56
|
|
|
$this->options = self::DEFAULT_OPTIONS + $options; |
|
57
|
|
|
if (isset($this->options['cache']) && $this->options['cache'] instanceof CacheInterface) { |
|
58
|
|
|
$this->cache = $this->options['cache']; |
|
59
|
|
|
} |
|
60
|
|
|
$this->hydrator = new Hydrator($this, $options); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function request(string $path, bool $refresh = false): PromiseInterface |
|
64
|
|
|
{ |
|
65
|
|
|
if ($refresh) { |
|
66
|
|
|
return $this->sendRequest($path); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $this->checkCache($path)->otherwise(function () use ($path) { |
|
70
|
|
|
return $this->sendRequest($path); |
|
71
|
|
|
}); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
protected function checkCache(string $path) |
|
75
|
|
|
{ |
|
76
|
|
|
if ($this->cache instanceof CacheInterface) { |
|
77
|
|
|
return $this->cache->get($path)->then(function ($json) use ($path) { |
|
78
|
|
|
return $this->jsonDecode($json); |
|
79
|
|
|
}); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return reject(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
protected function sendRequest(string $path, string $method = 'GET'): PromiseInterface |
|
86
|
|
|
{ |
|
87
|
|
|
$deferred = new Deferred(); |
|
88
|
|
|
|
|
89
|
|
|
$this->handler->sendAsync($this->createRequest($method, $path))->then(function (ResponseInterface $response) use ($deferred) { |
|
90
|
|
|
$contents = $response->getBody()->getContents(); |
|
91
|
|
|
$deferred->resolve($contents); |
|
92
|
|
|
}, function ($error) use ($deferred) { |
|
93
|
|
|
$deferred->reject($error); |
|
94
|
|
|
}); |
|
95
|
|
|
|
|
96
|
|
|
return $deferred->promise()->then(function ($json) use ($path) { |
|
97
|
|
|
$this->cache->set($path, $json); |
|
98
|
|
|
return $this->jsonDecode($json); |
|
99
|
|
|
}); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
protected function createRequest(string $method, string $path) |
|
103
|
|
|
{ |
|
104
|
|
|
$url = $this->options['schema'] . '://' . $this->options['host'] . $this->options['path'] . $path; |
|
105
|
|
|
$headers = [ |
|
106
|
|
|
'User-Agent' => $this->options['user_agent'], |
|
107
|
|
|
]; |
|
108
|
|
|
$headers += $this->options['headers']; |
|
109
|
|
|
return new Request($method, $url, $headers); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
protected function jsonDecode($json): PromiseInterface |
|
113
|
|
|
{ |
|
114
|
|
|
return futureFunctionPromise($this->loop, $json, function ($json) { |
|
115
|
|
|
return json_decode($json, true); |
|
116
|
|
|
}); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function getHydrator(): Hydrator |
|
120
|
|
|
{ |
|
121
|
|
|
return $this->hydrator; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function getLoop(): LoopInterface |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->loop; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|