1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the XabbuhPandaClient package. |
5
|
|
|
* |
6
|
|
|
* (c) Christian Flothmann <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Xabbuh\PandaClient\Api; |
13
|
|
|
|
14
|
|
|
use Guzzle\Http\Client; |
15
|
|
|
use Xabbuh\PandaClient\Exception\ApiException; |
16
|
|
|
use Xabbuh\PandaClient\Exception\HttpException; |
17
|
|
|
use Xabbuh\PandaClient\Signer\PandaSigner; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Panda REST client implementation using the PHP cURL extension. |
21
|
|
|
* |
22
|
|
|
* Send signed requests (GET, POST, PUT or DELETE) to the Panda encoding |
23
|
|
|
* webservice. |
24
|
|
|
* |
25
|
|
|
* @author Christian Flothmann <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class HttpClient implements HttpClientInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var Client |
31
|
|
|
*/ |
32
|
|
|
private $guzzleClient; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Panda cloud id |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private $cloudId; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Panda Account |
43
|
|
|
* |
44
|
|
|
* @var Account |
45
|
|
|
*/ |
46
|
|
|
private $account; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritDoc} |
50
|
|
|
*/ |
51
|
10 |
|
public function setCloudId($cloudId) |
52
|
|
|
{ |
53
|
10 |
|
$this->cloudId = $cloudId; |
54
|
10 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritDoc} |
58
|
|
|
*/ |
59
|
1 |
|
public function getCloudId() |
60
|
|
|
{ |
61
|
1 |
|
return $this->cloudId; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritDoc} |
66
|
|
|
*/ |
67
|
10 |
|
public function setAccount(Account $account) |
68
|
|
|
{ |
69
|
10 |
|
$this->account = $account; |
70
|
10 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritDoc} |
74
|
|
|
*/ |
75
|
1 |
|
public function getAccount() |
76
|
|
|
{ |
77
|
1 |
|
return $this->account; |
78
|
|
|
} |
79
|
|
|
|
80
|
10 |
|
public function setGuzzleClient(Client $guzzleClient) |
81
|
|
|
{ |
82
|
10 |
|
$this->guzzleClient = $guzzleClient; |
83
|
10 |
|
} |
84
|
|
|
|
85
|
|
|
public function getGuzzleClient() |
86
|
|
|
{ |
87
|
|
|
return $this->guzzleClient; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritDoc} |
92
|
|
|
*/ |
93
|
2 |
|
public function get($path, array $params = array()) |
94
|
|
|
{ |
95
|
2 |
|
return $this->request('GET', $path, $params); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritDoc} |
100
|
|
|
*/ |
101
|
1 |
|
public function post($path, array $params = array()) |
102
|
|
|
{ |
103
|
1 |
|
return $this->request('POST', $path, $params); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritDoc} |
108
|
|
|
*/ |
109
|
1 |
|
public function put($path, array $params = array()) |
110
|
|
|
{ |
111
|
1 |
|
return $this->request('PUT', $path, $params); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritDoc} |
116
|
|
|
*/ |
117
|
1 |
|
public function delete($path, array $params = array()) |
118
|
|
|
{ |
119
|
1 |
|
return $this->request('DELETE', $path, $params); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Send signed HTTP requests to the API server. |
124
|
|
|
* |
125
|
|
|
* @param string $method HTTP method (GET, POST, PUT or DELETE) |
126
|
|
|
* @param string $path Request path |
127
|
|
|
* @param array $params Additional request parameters |
128
|
|
|
* |
129
|
|
|
* @return string The API server's response |
130
|
|
|
* |
131
|
|
|
* @throws ApiException if an API error occurs |
132
|
|
|
* @throws HttpException if the request fails |
133
|
|
|
*/ |
134
|
5 |
|
private function request($method, $path, array $params) |
135
|
|
|
{ |
136
|
|
|
// sign the request parameters |
137
|
5 |
|
$signer = PandaSigner::getInstance($this->cloudId, $this->account); |
138
|
5 |
|
$params = $signer->signParams($method, $path, $params); |
139
|
|
|
|
140
|
|
|
// ensure to use relative paths |
141
|
5 |
|
if (0 === strpos($path, '/')) { |
142
|
5 |
|
$path = substr($path, 1); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
// append request parameters to the URL |
146
|
5 |
|
if ('GET' === $method || 'DELETE' === $method) { |
147
|
3 |
|
$path .= '?'.http_build_query($params, '', '&'); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
// prepare the request |
151
|
5 |
|
$request = null; |
152
|
|
|
|
153
|
|
|
switch ($method) { |
154
|
5 |
|
case 'GET': |
155
|
2 |
|
$request = $this->guzzleClient->get($path); |
156
|
2 |
|
break; |
157
|
3 |
|
case 'DELETE': |
158
|
1 |
|
$request = $this->guzzleClient->delete($path); |
159
|
1 |
|
break; |
160
|
2 |
|
case 'PUT': |
161
|
1 |
|
$request = $this->guzzleClient->put($path, null, $params); |
|
|
|
|
162
|
1 |
|
break; |
163
|
1 |
|
case 'POST': |
164
|
1 |
|
$request = $this->guzzleClient->post($path, null, $params); |
165
|
1 |
|
break; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
// and execute it |
169
|
|
|
try { |
170
|
5 |
|
$response = $request->send(); |
171
|
|
|
} catch (\Exception $e) { |
172
|
|
|
// throw an exception if the http request failed |
173
|
|
|
throw new HttpException($e->getMessage(), $e->getCode()); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
// throw an API exception if the API response is not valid |
177
|
5 |
|
if ($response->getStatusCode() < 200 || $response->getStatusCode() > 207) { |
178
|
1 |
|
$decodedResponse = json_decode($response->getBody(true)); |
179
|
1 |
|
$message = $decodedResponse->error.': '.$decodedResponse->message; |
180
|
|
|
|
181
|
1 |
|
throw new ApiException($message, $response->getStatusCode()); |
182
|
|
|
} |
183
|
|
|
|
184
|
4 |
|
return $response->getBody(true); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: