1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @category Brownie/HttpClient |
4
|
|
|
* @author Brownie <[email protected]> |
5
|
|
|
* @license https://opensource.org/licenses/MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Brownie\HttpClient; |
9
|
|
|
|
10
|
|
|
use Brownie\HttpClient\Cookie\Cookie; |
11
|
|
|
use Brownie\HttpClient\Exception\ClientException; |
12
|
|
|
use Brownie\HttpClient\Header\Header; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* HTTP client. |
16
|
|
|
* Target API for HTTP request. |
17
|
|
|
*/ |
18
|
|
|
class HttpClient |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Client adapter. |
23
|
|
|
* |
24
|
|
|
* @var Client |
25
|
|
|
*/ |
26
|
|
|
private $client; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Sets incoming data. |
30
|
|
|
* |
31
|
|
|
* @param Client $client Client adapter. |
32
|
|
|
*/ |
33
|
6 |
|
public function __construct(Client $client) |
34
|
|
|
{ |
35
|
6 |
|
$this->setClient($client); |
36
|
6 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Performs a network request. |
40
|
|
|
* Returns the response. |
41
|
|
|
* |
42
|
|
|
* @param Request $request HTTP request params. |
43
|
|
|
* @param int $attempts Number of request retries. |
44
|
|
|
* @param int[] $successfulHttpCodes Successful Http codes. |
45
|
|
|
* |
46
|
|
|
* @return Response |
47
|
|
|
* |
48
|
|
|
* @throws ClientException |
49
|
|
|
* @throws Exception\ValidateException |
50
|
|
|
*/ |
51
|
3 |
|
public function request(Request $request, $attempts = 1, $successfulHttpCodes = []) |
52
|
|
|
{ |
53
|
3 |
|
$request->validate(); |
54
|
3 |
|
$response = null; |
55
|
3 |
|
while (1 <= $attempts) { |
56
|
2 |
|
$response = $this->getClient()->httpRequest($request); |
57
|
2 |
|
$attempts--; |
58
|
2 |
|
if (empty($successfulHttpCodes) || in_array($response->getHttpCode(), $successfulHttpCodes)) { |
59
|
1 |
|
$attempts = 0; |
60
|
1 |
|
} |
61
|
2 |
|
if (0 < $attempts) { |
62
|
1 |
|
usleep(1000000); |
63
|
1 |
|
} |
64
|
2 |
|
}; |
65
|
3 |
|
if (empty($response)) { |
66
|
1 |
|
throw new ClientException('No requests for execution.'); |
67
|
|
|
} |
68
|
2 |
|
return $response; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Returns HTTP client request. |
73
|
|
|
* |
74
|
|
|
* @return Request |
75
|
|
|
*/ |
76
|
1 |
|
public function createRequest() |
77
|
|
|
{ |
78
|
1 |
|
return new Request(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Returns HTTP cookie. |
83
|
|
|
* |
84
|
|
|
* @param string $name The name of the cookie. |
85
|
|
|
* @param string $value The value of the cookie. |
86
|
|
|
* |
87
|
|
|
* @return Cookie |
88
|
|
|
*/ |
89
|
1 |
|
public function createCookie($name, $value) |
90
|
|
|
{ |
91
|
1 |
|
return new Cookie(array( |
92
|
1 |
|
'name' => $name, |
93
|
|
|
'value' => $value |
94
|
1 |
|
)); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns HTTP header. |
99
|
|
|
* |
100
|
|
|
* @param string $name Header name. |
101
|
|
|
* @param string $value Header value. |
102
|
|
|
* |
103
|
|
|
* @return Header |
104
|
|
|
*/ |
105
|
1 |
|
public function createHeader($name, $value) |
106
|
|
|
{ |
107
|
1 |
|
return new Header(array( |
108
|
1 |
|
'name' => $name, |
109
|
|
|
'value' => $value |
110
|
1 |
|
)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Sets the request client. |
115
|
|
|
* Returns the current object. |
116
|
|
|
* |
117
|
|
|
* @param Client $client Client adapter. |
118
|
|
|
* |
119
|
|
|
* @return $this |
120
|
|
|
*/ |
121
|
6 |
|
private function setClient(Client $client) |
122
|
|
|
{ |
123
|
6 |
|
$this->client = $client; |
124
|
6 |
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Returns request client. |
129
|
|
|
* |
130
|
|
|
* @return Client |
131
|
|
|
*/ |
132
|
2 |
|
private function getClient() |
133
|
|
|
{ |
134
|
2 |
|
return $this->client; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|