This repository has been archived by the owner on Dec 25, 2018. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathHttpClientAdapter.php
167 lines (145 loc) · 4.13 KB
/
HttpClientAdapter.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
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
<?php
/**
* This file is part of ReactGuzzle.
*
** (c) 2014 Cees-Jan Kiewiet
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace WyriHaximus\React\Guzzle;
use GuzzleHttp\Message\Response;
use GuzzleHttp\Adapter\AdapterInterface;
use GuzzleHttp\Adapter\TransactionInterface;
use GuzzleHttp\Psr7\Request;
use Psr\Http\Message\ResponseInterface;
use React\Dns\Resolver\Factory as DnsFactory;
use React\Dns\Resolver\Resolver as DnsResolver;
use React\EventLoop\LoopInterface;
use React\HttpClient\Client as HttpClient;
use React\HttpClient\Factory as HttpClientFactory;
use WyriHaximus\React\Guzzle\HttpClient\RequestFactory;
/**
* Class HttpClientAdapter
*
* @package WyriHaximus\React\Guzzle
*/
class HttpClientAdapter implements AdapterInterface
{
/**
* @var LoopInterface
*/
protected $loop;
/**
* @var DnsResolver
*/
protected $dnsResolver;
/**
* @var HttpClient
*/
protected $httpClient;
/**
* @var RequestFactory
*/
protected $requestFactory;
/**
* @param LoopInterface $loop
* @param HttpClient $httpClient
* @param DnsResolver $dnsResolver
*/
public function __construct(
LoopInterface $loop,
HttpClient $httpClient = null,
DnsResolver $dnsResolver = null,
RequestFactory $requestFactory = null
) {
$this->loop = $loop;
$this->setDnsResolver($dnsResolver);
$this->setHttpClient($httpClient);
$this->setRequestFactory($requestFactory);
}
/**
* @param HttpClient $httpClient
*/
public function setHttpClient(HttpClient $httpClient = null)
{
if (!($httpClient instanceof HttpClient)) {
$this->setDnsResolver($this->dnsResolver);
$factory = new HttpClientFactory();
$httpClient = $factory->create($this->loop, $this->dnsResolver);
}
$this->httpClient = $httpClient;
}
/**
* @return HttpClient
*/
public function getHttpClient()
{
return $this->httpClient;
}
/**
* @param DnsResolver $dnsResolver
*/
public function setDnsResolver(DnsResolver $dnsResolver = null)
{
if (!($dnsResolver instanceof DnsResolver)) {
$dnsResolverFactory = new DnsFactory();
$dnsResolver = $dnsResolverFactory->createCached('8.8.8.8', $this->loop);
}
$this->dnsResolver = $dnsResolver;
}
/**
* @return DnsResolver
*/
public function getDnsResolver()
{
return $this->dnsResolver;
}
/**
* @param RequestFactory $requestFactory
*/
public function setRequestFactory(RequestFactory $requestFactory = null)
{
if (!($requestFactory instanceof RequestFactory)) {
$requestFactory = new RequestFactory();
}
$this->requestFactory = $requestFactory;
}
/**
* @return RequestFactory
*/
public function getRequestFactory()
{
return $this->requestFactory;
}
/**
* @param TransactionInterface $transaction
*
* @return \React\Promise\Promise
*/
public function send(TransactionInterface $transaction)
{
return $this->requestFactory->create(
static::transformRequest($transaction),
[],
$this->httpClient,
$this->loop
)->then(function (ResponseInterface $response) {
return \React\Promise\resolve(static::transformResponse($response));
});
}
protected static function transformRequest(TransactionInterface $transaction)
{
return new Request(
$transaction->getRequest()->getMethod(),
$transaction->getRequest()->getUrl(),
$transaction->getRequest()->getHeaders(),
$transaction->getRequest()->getBody(),
$transaction->getRequest()->getProtocolVersion()
);
}
protected static function transformResponse(ResponseInterface $response)
{
return new Response($response->getStatusCode(), $response->getHeaders(), $response->getBody());
}
}