-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathClientFactory.php
More file actions
210 lines (172 loc) · 6.1 KB
/
ClientFactory.php
File metadata and controls
210 lines (172 loc) · 6.1 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
declare(strict_types=1);
namespace Shapecode\FUT\Client\Http;
use Closure;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Request as Psr7Request;
use GuzzleHttp\Psr7\Response as Psr7Response;
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;
use Http\Client\Common\Plugin\ContentLengthPlugin;
use Http\Client\Common\Plugin\HeaderSetPlugin;
use Http\Client\Common\Plugin\LoggerPlugin;
use Http\Client\Common\Plugin\RedirectPlugin;
use Http\Client\Common\Plugin\StopwatchPlugin;
use Http\Client\Common\PluginClient;
use Http\Client\HttpClient;
use Http\Discovery\Psr17FactoryDiscovery;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\UriFactoryInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Shapecode\FUT\Client\Api\CoreInterface;
use Shapecode\FUT\Client\Authentication\AccountInterface;
use Shapecode\FUT\Client\Config\ConfigInterface;
use Shapecode\FUT\Client\Http\Plugin\ClientCallPlugin;
use Symfony\Component\Stopwatch\Stopwatch;
use function count;
class ClientFactory implements ClientFactoryInterface
{
/** @var RequestFactoryInterface */
protected $requestFactory;
/** @var StreamFactoryInterface */
protected $streamFactory;
/** @var UriFactoryInterface */
protected $urlFactory;
/** @var ConfigInterface */
protected $config;
/** @var CookieJarBuilderInterface */
protected $cookieJarBuilder;
/** @var LoggerInterface */
protected $logger;
public const MAX_RETRIES = 4;
public function __construct(
ConfigInterface $config,
?CookieJarBuilderInterface $cookieJarBuilder = null,
?LoggerInterface $logger = null,
?RequestFactoryInterface $requestFactory = null,
?StreamFactoryInterface $streamFactory = null,
?UriFactoryInterface $urlFactory = null
) {
$this->config = $config;
$this->cookieJarBuilder = $cookieJarBuilder ?: new CookieJarBuilder();
$this->logger = $logger ?: new NullLogger();
$this->requestFactory = $requestFactory ?: Psr17FactoryDiscovery::findRequestFactory();
$this->streamFactory = $streamFactory ?: Psr17FactoryDiscovery::findStreamFactory();
$this->urlFactory = $urlFactory ?: Psr17FactoryDiscovery::findUrlFactory();
}
/**
* @inheritdoc
*/
public function request(
AccountInterface $account,
string $method,
string $url,
array $options = [],
array $plugins = []
) : ClientCall {
$headers = [];
if (isset($options['headers'])) {
/** @var mixed[] $headers */
$headers = $options['headers'];
unset($options['headers']);
}
$call = new ClientCall();
$plugins[] = new HeaderSetPlugin(CoreInterface::REQUEST_HEADERS);
$plugins[] = new HeaderSetPlugin([
'User-Agent' => $this->getConfig()->getUserAgent(),
]);
if (count($headers) > 0) {
$plugins[] = new HeaderSetPlugin($headers);
}
$plugins[] = new ContentLengthPlugin();
$plugins[] = new LoggerPlugin($this->logger);
$stopwatch = new Stopwatch();
$plugins[] = new StopwatchPlugin($stopwatch);
$plugins[] = new ClientCallPlugin($call);
$plugins[] = new RedirectPlugin();
$guzzle = $this->createAccountClient($account, $options);
$client = $this->createPluginClient($guzzle, $plugins);
$request = $this->createRequest($method, $url);
$client->sendRequest($request);
return $call;
}
/**
* @inheritdoc
*/
protected function createPluginClient(HttpClient $client, array $plugins = []) : PluginClient
{
return new PluginClient($client, $plugins);
}
/**
* @inheritdoc
*/
protected function createAccountClient(
AccountInterface $account,
array $options = []
) : GuzzleAdapter {
$options['http_errors'] = false;
$options['allow_redirects'] = true;
if ($account->getProxy() !== null) {
$options['proxy'] = $account->getProxy()->getProxyProtocol();
}
$options['cookies'] = $this->cookieJarBuilder->createCookieJar($account);
$stack = HandlerStack::create(new CurlHandler());
$stack->push(Middleware::retry($this->createRetryHandler()));
$options['stack'] = $stack;
$options['timeout'] = 5;
$guzzle = new Client($options);
return new GuzzleAdapter($guzzle);
}
/**
* @inheritdoc
*/
protected function createRequest(
string $method,
string $uri,
?string $body = null,
array $headers = []
) : RequestInterface {
$url = $this->urlFactory->createUri($uri);
$request = $this->requestFactory->createRequest($method, $url);
if ($body !== null) {
$stream = $this->streamFactory->createStream($body);
$request = $request->withBody($stream);
}
if (count($headers) > 0) {
foreach ($headers as $name => $header) {
$request = $request->withHeader($name, $header);
}
}
return $request;
}
protected function getConfig() : ConfigInterface
{
return $this->config;
}
protected function createRetryHandler() : Closure
{
return static function (
$retries,
Psr7Request $request,
?Psr7Response $response = null,
?RequestException $exception = null
) {
return $retries < self::MAX_RETRIES;
};
}
protected function isServerError(?Psr7Response $response = null) : bool
{
return $response !== null && $response->getStatusCode() >= 500;
}
protected function isConnectError(?RequestException $exception = null) : bool
{
return $exception instanceof ConnectException;
}
}