1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace keika299\ConohaAPI\Common\Network; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\Handler\MockHandler; |
7
|
|
|
use GuzzleHttp\HandlerStack; |
8
|
|
|
use GuzzleHttp\Psr7\Response as Psr7Response; |
9
|
|
|
use keika299\ConohaAPI\Common\Exceptions\Network\ExceptionFactory; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Request |
13
|
|
|
* |
14
|
|
|
* Create API request and exec it. |
15
|
|
|
* |
16
|
|
|
* @package keika299\ConohaAPI\Common\Network |
17
|
|
|
*/ |
18
|
|
|
class Request |
19
|
|
|
{ |
20
|
|
|
private $baseURI; |
21
|
|
|
private $method; |
22
|
|
|
private $uri; |
23
|
|
|
private $headers; |
24
|
|
|
private $options; |
25
|
|
|
private $query; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Request constructor. |
29
|
|
|
*/ |
30
|
82 |
|
public function __construct() |
31
|
|
|
{ |
32
|
82 |
|
$this->baseURI = ''; |
33
|
82 |
|
$this->method = 'GET'; |
34
|
82 |
|
$this->uri = ''; |
35
|
82 |
|
$this->headers = array(); |
36
|
82 |
|
$this->options = array(); |
37
|
82 |
|
$this->query = ''; |
38
|
82 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Exec API request. |
42
|
|
|
* |
43
|
|
|
* This function have to run after set all params. |
44
|
|
|
* If $_ENV['IS_TEST'] has value, this function return mock. |
45
|
|
|
* |
46
|
|
|
* @return Response |
47
|
|
|
* @throws \keika299\ConohaAPI\Common\Exceptions\IConohaAPIException |
48
|
|
|
*/ |
49
|
74 |
|
public function exec() |
50
|
|
|
{ |
51
|
74 |
|
if (0 < count($this->headers)) { |
52
|
74 |
|
$this->options['headers'] = $this->headers; |
53
|
74 |
|
} |
54
|
|
|
|
55
|
74 |
|
$client = $this->getClient(); |
56
|
|
|
|
57
|
|
|
try { |
58
|
74 |
|
$response = $client->request($this->method, $this->uri.$this->query, $this->options); |
59
|
74 |
|
return new Response($response); |
60
|
|
|
} |
61
|
|
|
catch (\Exception $e) { |
62
|
|
|
throw ExceptionFactory::build($e); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Set request method. |
68
|
|
|
* $method allow HTTP/1.1 request methods. |
69
|
|
|
* |
70
|
|
|
* @param string $method |
71
|
|
|
* @return $this |
72
|
|
|
*/ |
73
|
75 |
|
public function setMethod($method) |
74
|
|
|
{ |
75
|
75 |
|
$this->method = strtoupper($method); |
76
|
75 |
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Set base URI. |
81
|
|
|
* |
82
|
|
|
* $baseURI usually begin 'https://' or 'http://'. |
83
|
|
|
* $baseURI expect last character is not with '/'. |
84
|
|
|
* |
85
|
|
|
* @param string $baseURI |
86
|
|
|
* @return $this |
87
|
|
|
*/ |
88
|
75 |
|
public function setBaseURI($baseURI) |
89
|
|
|
{ |
90
|
75 |
|
$this->baseURI = $baseURI; |
91
|
75 |
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Set URI. |
96
|
|
|
* |
97
|
|
|
* $uri is NOT completely URI. |
98
|
|
|
* This param connect with base URI. |
99
|
|
|
* $uri expect to start with '/'. |
100
|
|
|
* |
101
|
|
|
* @param string $uri |
102
|
|
|
* @return $this |
103
|
|
|
*/ |
104
|
75 |
|
public function setURI($uri) |
105
|
|
|
{ |
106
|
75 |
|
$this->uri = $uri; |
107
|
75 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Add request header value. |
112
|
|
|
* |
113
|
|
|
* $key is parameter, and $value is value of that parameter. |
114
|
|
|
* You want to add 'X-Auth-Token', you can use setToken func. |
115
|
|
|
* You want to add 'Accept', you can use setAccept func. |
116
|
|
|
* |
117
|
|
|
* @param string $key |
118
|
|
|
* @param string $value |
119
|
|
|
* @return $this |
120
|
|
|
*/ |
121
|
1 |
|
public function addHeader($key, $value) |
122
|
|
|
{ |
123
|
1 |
|
$this->headers[$key] = $value; |
124
|
1 |
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Set 'Accept' request header. |
129
|
|
|
* |
130
|
|
|
* @param string $value |
131
|
|
|
* @return $this |
132
|
|
|
*/ |
133
|
71 |
|
public function setAccept($value) |
134
|
|
|
{ |
135
|
71 |
|
$this->headers['Accept'] = $value; |
136
|
71 |
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Set 'Content-Type' request header |
141
|
|
|
* |
142
|
|
|
* @param string $value |
143
|
|
|
* @return $this |
144
|
|
|
*/ |
145
|
6 |
|
public function setContentType($value) |
146
|
|
|
{ |
147
|
6 |
|
$this->headers['Content-Type'] = $value; |
148
|
6 |
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Set request body. |
153
|
|
|
* |
154
|
|
|
* You can use this function when request 'POST' or 'PUT' method. |
155
|
|
|
* |
156
|
|
|
* @param string $body |
157
|
|
|
* @return $this |
158
|
|
|
*/ |
159
|
3 |
|
public function setBody($body) |
160
|
|
|
{ |
161
|
3 |
|
$this->options['body'] = $body; |
162
|
3 |
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Set request Json. |
167
|
|
|
* |
168
|
|
|
* Array transform to json data, and set request body. |
169
|
|
|
* |
170
|
|
|
* @param array $array |
171
|
|
|
* @return $this |
172
|
|
|
*/ |
173
|
26 |
|
public function setJson(array $array) |
174
|
|
|
{ |
175
|
26 |
|
$this->options['json'] = $array; |
176
|
26 |
|
return $this; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Set request URI query. |
181
|
|
|
* |
182
|
|
|
* $array have to be associative array. |
183
|
|
|
* |
184
|
|
|
* @param array $array |
185
|
|
|
* @return $this |
186
|
|
|
*/ |
187
|
13 |
|
public function setQuery(array $array) |
188
|
|
|
{ |
189
|
13 |
|
if (sizeof($array) === 0) { |
190
|
12 |
|
return $this; |
191
|
|
|
} |
192
|
|
|
|
193
|
13 |
|
$query = '?'; |
194
|
|
|
|
195
|
13 |
|
foreach ($array as $item => $value) { |
196
|
13 |
|
$query .= $item . '=' . $value . '&'; |
197
|
13 |
|
} |
198
|
|
|
|
199
|
13 |
|
$this->query = mb_substr($query, 0, -1, "UTF-8"); |
200
|
|
|
|
201
|
13 |
|
return $this; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Set 'X-Auth-Token' request header. |
206
|
|
|
* |
207
|
|
|
* @param string $token |
208
|
|
|
* @return $this |
209
|
|
|
*/ |
210
|
66 |
|
public function setToken($token) |
211
|
|
|
{ |
212
|
66 |
|
$this->headers['X-Auth-Token'] = $token; |
213
|
66 |
|
return $this; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @return Client |
218
|
|
|
*/ |
219
|
74 |
|
private function getClient() |
220
|
|
|
{ |
221
|
74 |
|
return getenv('IS_TEST') ? $this->createMockClient() : new Client(['base_uri' => $this->baseURI]); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @return Client |
226
|
|
|
*/ |
227
|
74 |
|
private function createMockClient() |
228
|
|
|
{ |
229
|
74 |
|
$body = '{"checkKey": "checkValue", "access": {"token": {"id": "sample00d88246078f2bexample788f7"}}}'; |
230
|
|
|
|
231
|
|
|
$mockResponse = [ |
232
|
74 |
|
new Psr7Response(200, [], $body) |
233
|
74 |
|
]; |
234
|
74 |
|
$mockHandler = new MockHandler($mockResponse); |
235
|
74 |
|
$handlerStack = HandlerStack::create($mockHandler); |
236
|
74 |
|
return new Client(['handler' => $handlerStack]); |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|