|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Skobkin\Bundle\PointToolsBundle\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Guzzle\Service\Client; |
|
6
|
|
|
use Guzzle\Http\Message\Request as GuzzleRequest; |
|
7
|
|
|
use Guzzle\Http\Message\Response as GuzzleResponse; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @todo Refactor to Guzzle and DTO |
|
11
|
|
|
* @see https://github.com/misd-service-development/guzzle-bundle/blob/master/Resources/doc/serialization.md |
|
12
|
|
|
* @see https://github.com/misd-service-development/guzzle-bundle/blob/master/Resources/doc/clients.md |
|
13
|
|
|
* @see https://github.com/misd-service-development/guzzle-bundle/blob/master/Resources/doc/param_converter.md |
|
14
|
|
|
*/ |
|
15
|
|
|
class AbstractApi |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var Client HTTP-client from Guzzle |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $client; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var bool Use HTTPS instead of HTTP |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $useHttps; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string Authentication token for API |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $authToken; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var string CSRF-token for API |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $csRfToken; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param Client $httpClient HTTP-client from Guzzle |
|
39
|
|
|
* @param bool $https Use HTTPS instead of HTTP |
|
40
|
|
|
* @param string $baseUrl Base URL for API |
|
41
|
|
|
*/ |
|
42
|
12 |
|
public function __construct(Client $httpClient, $https = true, $baseUrl = null) |
|
43
|
|
|
{ |
|
44
|
12 |
|
$this->client = $httpClient; |
|
45
|
12 |
|
$this->useHttps = ($https) ? true : false; |
|
46
|
|
|
|
|
47
|
12 |
|
if (null !== $baseUrl) { |
|
48
|
12 |
|
$this->setBaseUrl($baseUrl); |
|
49
|
|
|
} |
|
50
|
12 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Make GET request and return Response object |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $path Request path |
|
56
|
|
|
* @param array $parameters Key => Value array of query parameters |
|
57
|
|
|
* @return GuzzleResponse |
|
58
|
|
|
*/ |
|
59
|
|
|
public function sendGetRequest($path, array $parameters = []) |
|
60
|
|
|
{ |
|
61
|
|
|
/** @var GuzzleRequest $request */ |
|
62
|
|
|
$request = $this->client->get($path); |
|
63
|
|
|
|
|
64
|
|
|
$query = $request->getQuery(); |
|
65
|
|
|
|
|
66
|
|
|
foreach ($parameters as $parameter => $value) { |
|
67
|
|
|
$query->set($parameter, $value); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return $request->send(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Make POST request and return Response object |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $path Request path |
|
77
|
|
|
* @param array $parameters Key => Value array of request data |
|
78
|
|
|
* @return GuzzleResponse |
|
79
|
|
|
*/ |
|
80
|
|
|
public function sendPostRequest($path, array $parameters = []) |
|
81
|
|
|
{ |
|
82
|
|
|
/** @var GuzzleRequest $request */ |
|
83
|
|
|
$request = $this->client->post($path, null, $parameters); |
|
84
|
|
|
|
|
85
|
|
|
return $request->send(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Make GET request and return data from response |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $path Path template |
|
92
|
|
|
* @param array $parameters Parameters array used to fill path template |
|
93
|
|
|
* @param bool $decodeJsonResponse Decode JSON or return plaintext |
|
94
|
|
|
* @param bool $decodeJsonToObjects Decode JSON objects to PHP objects instead of arrays |
|
95
|
|
|
* @return mixed |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getGetRequestData($path, array $parameters = [], $decodeJsonResponse = false, $decodeJsonToObjects = false) |
|
98
|
|
|
{ |
|
99
|
|
|
$response = $this->sendGetRequest($path, $parameters); |
|
100
|
|
|
|
|
101
|
|
|
if ($decodeJsonResponse) { |
|
102
|
|
|
if ($decodeJsonToObjects) { |
|
103
|
|
|
return json_decode($response->getBody(true)); |
|
104
|
|
|
} else { |
|
105
|
|
|
return $response->json(); |
|
106
|
|
|
} |
|
107
|
|
|
} else { |
|
108
|
|
|
return $response->getBody(true); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Make POST request and return data from response |
|
114
|
|
|
* |
|
115
|
|
|
* @param string $path Path template |
|
116
|
|
|
* @param array $parameters Parameters array used to fill path template |
|
117
|
|
|
* @param bool $decodeJsonResponse Decode JSON or return plaintext |
|
118
|
|
|
* @param bool $decodeJsonToObjects Decode JSON objects to PHP objects instead of arrays |
|
119
|
|
|
* @return mixed |
|
120
|
|
|
*/ |
|
121
|
|
|
public function getPostRequestData($path, array $parameters = [], $decodeJsonResponse = false, $decodeJsonToObjects = false) |
|
122
|
|
|
{ |
|
123
|
|
|
$response = $this->sendPostRequest($path, $parameters); |
|
124
|
|
|
|
|
125
|
|
|
if ($decodeJsonResponse) { |
|
126
|
|
|
if ($decodeJsonToObjects) { |
|
127
|
|
|
return json_decode($response->getBody(true)); |
|
128
|
|
|
} else { |
|
129
|
|
|
return $response->json(); |
|
130
|
|
|
} |
|
131
|
|
|
} else { |
|
132
|
|
|
return $response->getBody(true); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Get HTTP client base URL |
|
138
|
|
|
* |
|
139
|
|
|
* @return string Base URL of client |
|
140
|
|
|
*/ |
|
141
|
|
|
public function getBaseUrl() |
|
142
|
|
|
{ |
|
143
|
|
|
return $this->client->getBaseUrl(); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Set HTTP client base URL |
|
148
|
|
|
* |
|
149
|
|
|
* @param string $baseUrl Base URL of API |
|
150
|
|
|
* @param bool $useProtocol Do not change URL scheme (http/https) defined in $baseUrl |
|
151
|
|
|
* @return $this |
|
152
|
|
|
*/ |
|
153
|
12 |
|
public function setBaseUrl($baseUrl, $useProtocol = false) |
|
154
|
|
|
{ |
|
155
|
|
|
// Overriding protocol |
|
156
|
12 |
|
if (!$useProtocol) { |
|
157
|
12 |
|
$baseUrl = str_replace(['http://', 'https://',], ($this->useHttps) ? 'https://' : 'http://', $baseUrl); |
|
158
|
|
|
} |
|
159
|
|
|
// Adding missing protocol |
|
160
|
12 |
|
if ((false === strpos(strtolower($baseUrl), 'http://')) && (false === strpos(strtolower($baseUrl), 'https://'))) { |
|
161
|
|
|
$baseUrl = (($this->useHttps) ? 'https://' : 'http://') . $baseUrl; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
12 |
|
$this->client->setBaseUrl($baseUrl); |
|
165
|
|
|
|
|
166
|
12 |
|
return $this; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Check if API service uses HTTPS |
|
171
|
|
|
* |
|
172
|
|
|
* @return bool |
|
173
|
|
|
*/ |
|
174
|
|
|
public function isHttps() |
|
175
|
|
|
{ |
|
176
|
|
|
return $this->useHttps; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Enable HTTPS |
|
181
|
|
|
* |
|
182
|
|
|
* @return $this |
|
183
|
|
|
*/ |
|
184
|
|
|
public function enableHttps() |
|
185
|
|
|
{ |
|
186
|
|
|
$this->useHttps = true; |
|
187
|
|
|
$this->setBaseUrl($this->getBaseUrl()); |
|
|
|
|
|
|
188
|
|
|
|
|
189
|
|
|
return $this; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Disable HTTPS |
|
194
|
|
|
* |
|
195
|
|
|
* @return $this |
|
196
|
|
|
*/ |
|
197
|
|
|
public function disableHttps() |
|
198
|
|
|
{ |
|
199
|
|
|
$this->useHttps = false; |
|
200
|
|
|
$this->setBaseUrl($this->getBaseUrl()); |
|
|
|
|
|
|
201
|
|
|
|
|
202
|
|
|
return $this; |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.