|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of JSON RPC Client. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Igor Lazarev <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Strider2038\JsonRpcClient\Transport\Http; |
|
12
|
|
|
|
|
13
|
|
|
use GuzzleHttp\Client; |
|
14
|
|
|
use Strider2038\JsonRpcClient\Configuration\GeneralOptions; |
|
15
|
|
|
use Strider2038\JsonRpcClient\Exception\InvalidConfigException; |
|
16
|
|
|
use Strider2038\JsonRpcClient\Transport\TransportFactoryInterface; |
|
17
|
|
|
use Strider2038\JsonRpcClient\Transport\TransportInterface; |
|
18
|
|
|
use Symfony\Component\HttpClient\HttpClient; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @internal |
|
22
|
|
|
* |
|
23
|
|
|
* @author Igor Lazarev <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class HttpTransportFactory implements TransportFactoryInterface |
|
26
|
|
|
{ |
|
27
|
22 |
|
public function createTransport(string $url, GeneralOptions $options): TransportInterface |
|
28
|
|
|
{ |
|
29
|
22 |
|
$client = $this->detectHttpClientType($options); |
|
30
|
|
|
|
|
31
|
22 |
|
if (HttpTransportTypeInterface::SYMFONY === $client) { |
|
32
|
16 |
|
$transport = $this->createSymfonyTransport($url, $options); |
|
33
|
|
|
} else { |
|
34
|
6 |
|
$transport = $this->createGuzzleTransport($url, $options); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
22 |
|
return $transport; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @throws InvalidConfigException |
|
42
|
|
|
*/ |
|
43
|
22 |
|
private function detectHttpClientType(GeneralOptions $options): string |
|
44
|
|
|
{ |
|
45
|
22 |
|
$client = $options->getHttpClientType(); |
|
46
|
|
|
|
|
47
|
22 |
|
if (HttpTransportTypeInterface::AUTODETECT === $client) { |
|
48
|
5 |
|
if (class_exists(HttpClient::class)) { |
|
49
|
5 |
|
$client = HttpTransportTypeInterface::SYMFONY; |
|
50
|
|
|
} elseif (class_exists(Client::class)) { |
|
51
|
|
|
$client = HttpTransportTypeInterface::GUZZLE; |
|
52
|
|
|
} else { |
|
53
|
|
|
throw new InvalidConfigException( |
|
54
|
|
|
'Cannot create HTTP client: one of packages "symfony/http-client" or "guzzlehttp/guzzle" is required.' |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
22 |
|
return $client; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @throws InvalidConfigException |
|
64
|
|
|
*/ |
|
65
|
16 |
|
private function createSymfonyTransport(string $connection, GeneralOptions $options): SymfonyTransport |
|
66
|
|
|
{ |
|
67
|
16 |
|
if (!class_exists(HttpClient::class)) { |
|
68
|
|
|
throw new InvalidConfigException( |
|
69
|
|
|
'Cannot create Symfony HTTP client: package "symfony/http-client" is required.' |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
16 |
|
if (method_exists(HttpClient::class, 'createForBaseUri')) { |
|
74
|
16 |
|
$config = array_merge( |
|
75
|
16 |
|
$options->getTransportConfiguration(), |
|
76
|
|
|
[ |
|
77
|
16 |
|
'timeout' => (float) $options->getRequestTimeoutUs() / 1000000, |
|
78
|
|
|
] |
|
79
|
|
|
); |
|
80
|
|
|
|
|
81
|
16 |
|
$client = HttpClient::createForBaseUri($connection, $config); |
|
82
|
|
|
} else { |
|
83
|
|
|
$config = array_merge( |
|
84
|
|
|
$options->getTransportConfiguration(), |
|
85
|
|
|
[ |
|
86
|
|
|
'base_uri' => $connection, |
|
87
|
|
|
'timeout' => (float) $options->getRequestTimeoutUs() / 1000000, |
|
88
|
|
|
] |
|
89
|
|
|
); |
|
90
|
|
|
|
|
91
|
|
|
$client = HttpClient::create($config); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
16 |
|
return new SymfonyTransport($client); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @throws InvalidConfigException |
|
99
|
|
|
*/ |
|
100
|
6 |
|
private function createGuzzleTransport(string $connection, GeneralOptions $options): GuzzleTransport |
|
101
|
|
|
{ |
|
102
|
6 |
|
if (!class_exists(Client::class)) { |
|
103
|
|
|
throw new InvalidConfigException( |
|
104
|
|
|
'Cannot create Guzzle HTTP client: package "guzzlehttp/guzzle" is required.' |
|
105
|
|
|
); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
6 |
|
$config = array_merge( |
|
109
|
6 |
|
$options->getTransportConfiguration(), |
|
110
|
|
|
[ |
|
111
|
6 |
|
'base_uri' => $connection, |
|
112
|
6 |
|
'timeout' => (float) $options->getRequestTimeoutUs() / 1000000, |
|
113
|
|
|
] |
|
114
|
|
|
); |
|
115
|
|
|
|
|
116
|
6 |
|
$client = new Client($config); |
|
117
|
|
|
|
|
118
|
6 |
|
return new GuzzleTransport($client); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|