-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathHTTPProjectConfigManager.php
276 lines (232 loc) · 8.49 KB
/
HTTPProjectConfigManager.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
/**
* Copyright 2019-2020, 2022-2023 Optimizely Inc and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Optimizely\ProjectConfigManager;
use Exception;
use GuzzleHttp\Client as HttpClient;
use Monolog\Logger;
use Optimizely\Config\DatafileProjectConfig;
use Optimizely\Enums\ProjectConfigManagerConstants;
use Optimizely\ErrorHandler\ErrorHandlerInterface;
use Optimizely\ErrorHandler\NoOpErrorHandler;
use Optimizely\Logger\LoggerInterface;
use Optimizely\Logger\NoOpLogger;
use Optimizely\Notification\NotificationCenter;
use Optimizely\Notification\NotificationType;
use Optimizely\Utils\Validator;
class HTTPProjectConfigManager implements ProjectConfigManagerInterface
{
/**
* @var \GuzzleHttp\Client Guzzle HTTP client to send requests.
*/
private $httpClient;
/**
* @var DatafileProjectConfig
*/
private $_config;
/**
* @var String Datafile URL.
*/
private $_url;
/**
* @var boolean Flag indicates that skip JSON validation of datafile.
*/
private $_skipJsonValidation;
/**
* @var String datafile last modified time.
*/
private $_lastModifiedSince;
/**
* @var LoggerInterface Logger instance.
*/
private $_logger;
/**
* @var ErrorHandlerInterface ErrorHandler instance.
*/
private $_errorHandler;
/**
* @var NotificationCenter NotificationCenter instance.
*/
private $_notificationCenter;
/**
* @var String datafile access token.
*/
private $_datafileAccessToken;
/**
* @var boolean Flag indicates that the datafile access token is valid.
*/
private $_isDatafileAccessTokenValid;
public function __construct(
$sdkKey = null,
$url = null,
$urlTemplate = null,
$fetchOnInit = true,
$datafile = null,
$skipJsonValidation = false,
LoggerInterface $logger = null,
ErrorHandlerInterface $errorHandler = null,
NotificationCenter $notificationCenter = null,
$datafileAccessToken = null
) {
$this->_skipJsonValidation = $skipJsonValidation;
$this->_logger = $logger ?: new NoOpLogger();
$this->_errorHandler = $errorHandler ?: new NoOpErrorHandler();
$this->_notificationCenter = $notificationCenter ?: new NotificationCenter($this->_logger, $this->_errorHandler);
$this->_datafileAccessToken = $datafileAccessToken;
$this->_isDatafileAccessTokenValid = Validator::validateNonEmptyString($this->_datafileAccessToken);
$this->httpClient = new HttpClient();
$this->_url = $this->getUrl($sdkKey, $url, $urlTemplate);
if ($datafile !== null) {
$this->_config = DatafileProjectConfig::createProjectConfigFromDatafile(
$datafile,
$skipJsonValidation,
$this->_logger,
$this->_errorHandler
);
}
// Update config on initialization.
if ($fetchOnInit === true) {
$this->fetch();
}
}
/**
* Helper function to return URL based on params passed.
*
* @param $sdkKey string SDK key.
* @param $url string URL for datafile.
* @param $urlTemplate string Template to be used with SDK key to fetch datafile.
*
* @return string URL for datafile.
*/
protected function getUrl($sdkKey, $url, $urlTemplate)
{
if (Validator::validateNonEmptyString($url)) {
return $url;
}
if (!Validator::validateNonEmptyString($sdkKey)) {
$exception = new Exception("One of the SDK key or URL must be provided.");
$this->_errorHandler->handleError($exception);
throw $exception;
}
if (!Validator::validateNonEmptyString($urlTemplate)) {
if ($this->_isDatafileAccessTokenValid) {
$urlTemplate = ProjectConfigManagerConstants::AUTHENTICATED_DATAFILE_URL_TEMPLATE;
} else {
$urlTemplate = ProjectConfigManagerConstants::DEFAULT_DATAFILE_URL_TEMPLATE;
}
}
$url = sprintf($urlTemplate, $sdkKey);
return $url;
}
/**
* Function to fetch latest datafile.
*
* @return boolean flag to indicate if datafile is updated.
*/
public function fetch()
{
$datafile = $this->fetchDatafile();
if ($datafile === null) {
return false;
}
return true;
}
/**
* Helper function to fetch datafile and handle response if datafile is modified.
*
* @return null|datafile.
*/
protected function fetchDatafile()
{
$headers = [];
// Add If-Modified-Since header.
if (Validator::validateNonEmptyString($this->_lastModifiedSince)) {
$headers[ProjectConfigManagerConstants::IF_MODIFIED_SINCE] = $this->_lastModifiedSince;
}
// Add Authorization header if access token available.
if ($this->_isDatafileAccessTokenValid) {
$headers['Authorization'] = "Bearer {$this->_datafileAccessToken}";
}
$options = [
'headers' => $headers,
'timeout' => ProjectConfigManagerConstants::TIMEOUT,
'connect_timeout' => ProjectConfigManagerConstants::TIMEOUT
];
try {
$response = $this->httpClient->get($this->_url, $options);
} catch (Exception $exception) {
$this->_logger->log(Logger::ERROR, 'Unexpected response when trying to fetch datafile, status code: ' . $exception->getCode(). '. ' .
'Please check your SDK key and/or datafile access token.');
return null;
}
$status = $response->getStatusCode();
// Datafile not updated.
if ($status === 304) {
$this->_logger->log(Logger::DEBUG, 'Not updating ProjectConfig as datafile has not updated since ' . $this->_lastModifiedSince);
return null;
}
// Datafile retrieved successfully.
if ($status >= 200 && $status < 300) {
if ($response->hasHeader(ProjectConfigManagerConstants::LAST_MODIFIED)) {
$this->_lastModifiedSince = $response->getHeader(ProjectConfigManagerConstants::LAST_MODIFIED)[0];
}
$datafile = $response->getBody()->getContents();
if ($this->handleResponse($datafile) === true) {
return $datafile;
}
return null;
}
// Failed to retrieve datafile from Url.
$this->_logger->log(Logger::ERROR, 'Unexpected response when trying to fetch datafile, status code: ' . $status . '. ' .
'Please check your SDK key and/or datafile access token.');
return null;
}
/**
* Helper function to create config from datafile.
*
* @param string $datafile
* @return boolean flag to indicate if config is updated.
*/
protected function handleResponse($datafile)
{
if ($datafile === null) {
return false;
}
$config = DatafileProjectConfig::createProjectConfigFromDatafile($datafile, $this->_skipJsonValidation, $this->_logger, $this->_errorHandler);
if ($config === null) {
return false;
}
$previousRevision = null;
if ($this->_config !== null) {
$previousRevision = $this->_config->getRevision();
}
if ($previousRevision === $config->getRevision()) {
return false;
}
$this->_config = $config;
$this->_notificationCenter->sendNotifications(NotificationType::OPTIMIZELY_CONFIG_UPDATE);
$this->_logger->log(Logger::DEBUG, sprintf('Received new datafile and updated config. Old revision number: "%s". New revision number: "%s".', $previousRevision, $this->_config->getRevision()));
return true;
}
/**
* Returns instance of DatafileProjectConfig.
* @return null|DatafileProjectConfig DatafileProjectConfig instance.
*/
public function getConfig()
{
return $this->_config;
}
}