1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Skobkin\Bundle\PointToolsBundle\Service; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\ClientInterface; |
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
7
|
|
|
use Psr\Log\LoggerInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @todo Refactor DTO deserialization |
11
|
|
|
*/ |
12
|
|
|
class AbstractApi |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var ClientInterface HTTP-client from Guzzle |
16
|
|
|
*/ |
17
|
|
|
protected $client; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var LoggerInterface |
21
|
|
|
*/ |
22
|
|
|
protected $logger; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string Authentication token for API |
26
|
|
|
*/ |
27
|
|
|
protected $authToken; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string CSRF-token for API |
31
|
|
|
*/ |
32
|
|
|
protected $csRfToken; |
33
|
|
|
|
34
|
|
|
|
35
|
12 |
|
public function __construct(ClientInterface $httpClient, LoggerInterface $logger) |
36
|
|
|
{ |
37
|
12 |
|
$this->client = $httpClient; |
38
|
12 |
|
$this->logger = $logger; |
39
|
12 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $path Request path |
43
|
|
|
* @param array $parameters Key => Value array of query parameters |
44
|
|
|
* |
45
|
|
|
* @return ResponseInterface |
46
|
|
|
*/ |
47
|
|
|
public function sendGetRequest(string $path, array $parameters = []): ResponseInterface |
48
|
|
|
{ |
49
|
|
|
$this->logger->debug('Sending GET request', ['path' => $path, 'parameters' => $parameters]); |
50
|
|
|
|
51
|
|
|
return $this->client->request('GET', $path, ['query' => $parameters]); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $path Request path |
56
|
|
|
* @param array $parameters Key => Value array of request data |
57
|
|
|
* |
58
|
|
|
* @return ResponseInterface |
59
|
|
|
*/ |
60
|
|
|
public function sendPostRequest(string $path, array $parameters = []): ResponseInterface |
61
|
|
|
{ |
62
|
|
|
$this->logger->debug('Sending POST request', ['path' => $path, 'parameters' => $parameters]); |
63
|
|
|
|
64
|
|
|
return $this->client->request('POST', $path, ['form_params' => $parameters]); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Make GET request and return data from response |
69
|
|
|
* |
70
|
|
|
* @param string $path Path template |
71
|
|
|
* @param array $parameters Parameters array used to fill path template |
72
|
|
|
* @param bool $decodeJsonResponse Decode JSON or return plaintext |
73
|
|
|
* @param bool $decodeJsonToObjects Decode JSON objects to PHP objects instead of arrays |
74
|
|
|
* |
75
|
|
|
* @return mixed |
76
|
|
|
*/ |
77
|
|
|
public function getGetRequestData($path, array $parameters = [], bool $decodeJsonResponse = false, bool $decodeJsonToObjects = false) |
78
|
|
|
{ |
79
|
|
|
$response = $this->sendGetRequest($path, $parameters); |
80
|
|
|
|
81
|
|
|
return $this->processResponse($response, $decodeJsonResponse, $decodeJsonToObjects); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Make POST request and return data from response |
86
|
|
|
* |
87
|
|
|
* @param string $path Path template |
88
|
|
|
* @param array $parameters Parameters array used to fill path template |
89
|
|
|
* @param bool $decodeJson Decode JSON or return plaintext |
90
|
|
|
* @param bool $decodeToObjects Decode JSON objects to PHP objects instead of arrays |
91
|
|
|
* |
92
|
|
|
* @return mixed |
93
|
|
|
*/ |
94
|
|
|
public function getPostRequestData($path, array $parameters = [], bool $decodeJson = false, bool $decodeToObjects = false) |
95
|
|
|
{ |
96
|
|
|
$response = $this->sendPostRequest($path, $parameters); |
97
|
|
|
|
98
|
|
|
return $this->processResponse($response, $decodeJson, $decodeToObjects); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param ResponseInterface $response |
103
|
|
|
* @param bool $decodeJson |
104
|
|
|
* @param bool $decodeToObjects |
105
|
|
|
* |
106
|
|
|
* @return string|array|object |
107
|
|
|
*/ |
108
|
|
|
private function processResponse(ResponseInterface $response, bool $decodeJson = false, bool $decodeToObjects = false) |
109
|
|
|
{ |
110
|
|
|
if ($decodeJson) { |
111
|
|
|
if ($decodeToObjects) { |
112
|
|
|
return json_decode($response->getBody()); |
113
|
|
|
} else { |
114
|
|
|
return json_decode($response->getBody(), true); |
115
|
|
|
} |
116
|
|
|
} else { |
117
|
|
|
return $response->getBody(); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|