-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathRestClient.php
183 lines (151 loc) · 4.43 KB
/
RestClient.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
namespace MailerLiteApi\Common;
use Http\Discovery\Psr17FactoryDiscovery;
use Http\Discovery\Psr18ClientDiscovery;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamFactoryInterface;
class RestClient {
public $httpClient;
public $apiKey;
public $baseUrl;
public $requestFactory;
public $streamFactory;
/**
* @param string $baseUrl
* @param string $apiKey
* @param ClientInterface|null $httpClient
*/
public function __construct($baseUrl, $apiKey, ClientInterface $httpClient = null)
{
$this->baseUrl = $baseUrl;
$this->apiKey = $apiKey;
$this->httpClient = $httpClient;
}
/**
* Execute GET request
*
* @param string $endpointUri
* @param array $queryString
* @return [type]
*/
public function get($endpointUri, $queryString = [])
{
return $this->send('GET', $endpointUri . '?' . http_build_query($queryString));
}
/**
* Execute POST request
*
* @param string $endpointUri
* @param array $data
* @return [type]
*/
public function post($endpointUri, $data = [])
{
return $this->send('POST', $endpointUri, $data);
}
/**
* Execute PUT request
*
* @param string $endpointUri
* @param array $putData
* @return [type]
*/
public function put($endpointUri, $putData = [])
{
return $this->send('PUT', $endpointUri, $putData);
}
/**
* Execute DELETE request
*
* @param string $endpointUri
* @return [type]
*/
public function delete($endpointUri)
{
return $this->send('DELETE', $endpointUri);
}
/**
* Execute HTTP request
*
* @param string $method
* @param string $endpointUri
* @param string $body
* @param array $headers
* @return [type]
*/
protected function send($method, $endpointUri, $body = null, array $headers = [])
{
$headers = array_merge($headers, self::getDefaultHeaders());
$endpointUrl = $this->baseUrl . $endpointUri;
$request = $this->getRequestFactory()->createRequest($method, $endpointUrl);
if ($body) {
$stream = $this->getStreamFactory()
->createStream(json_encode($body));
$request = $request->withBody($stream);
}
foreach ($headers as $name => $value) {
$request = $request->withAddedHeader($name, $value);
}
$response = $this->getHttpClient()->sendRequest(
$request
);
return $this->handleResponse($response);
}
/**
* Handle HTTP response
*
* @param ResponseInterface $response
* @return [type]
*/
protected function handleResponse(ResponseInterface $response)
{
$status = $response->getStatusCode();
$data = (string) $response->getBody();
$jsonResponseData = json_decode($data, false);
$body = $data && $jsonResponseData === null ? $data : $jsonResponseData;
return ['status_code' => $status, 'body' => $body];
}
/**
* @return ClientInterface
*/
protected function getHttpClient()
{
if (is_null($this->httpClient)) {
$this->httpClient = Psr18ClientDiscovery::find();
}
return $this->httpClient;
}
/**
* @return HttpClient
*/
protected function getRequestFactory(): RequestFactoryInterface
{
if (null === $this->requestFactory) {
$this->requestFactory = Psr17FactoryDiscovery::findRequestFactory();
}
return $this->requestFactory;
}
private function getStreamFactory(): StreamFactoryInterface
{
if (null === $this->streamFactory) {
$this->streamFactory = Psr17FactoryDiscovery::findStreamFactory();
}
return $this->streamFactory;
}
/**
* @return array
*/
protected function getDefaultHeaders() {
$headers = [
'User-Agent' => ApiConstants::SDK_USER_AGENT.'/'.ApiConstants::SDK_VERSION,
'Content-Type' => 'application/json',
];
// Only adding it when provided. Not required for RestClientTest
if ($this->apiKey) {
$headers['X-MailerLite-ApiKey'] = $this->apiKey;
}
return $headers;
}
}