Skip to content

Commit a9217c4

Browse files
pthompson127aliabbasrizvi
authored andcommitted
refactor: replace all instances of access_token w/ datafile_access_token (#279)
* refactor: replace all instances of access_token w/ datafile_access_token * style: fix small linting issue * style: remove trailing whitespace (linting)
1 parent 1bf5a89 commit a9217c4

File tree

6 files changed

+45
-41
lines changed

6 files changed

+45
-41
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,15 @@ implements `PollingConfigManager` and asynchronously polls for authenticated dat
127127
by making HTTP requests.
128128

129129
auth_datafile_polling_config_manager = AuthDatafilePollingConfigManager(
130-
access_token,
130+
datafile_access_token,
131131
*args,
132132
**kwargs
133133
)
134134

135135
**Note**: To use [AuthDatafilePollingConfigManager](#authdatafilepollingconfigmanager), you must create a secure environment for
136136
your project and generate an access token for your datafile.
137137

138-
**access_token** The access_token is attached to the outbound HTTP request header to authorize the request and fetch the datafile.
138+
**datafile_access_token** The datafile_access_token is attached to the outbound HTTP request header to authorize the request and fetch the datafile.
139139

140140
#### Advanced configuration
141141

optimizely/config_manager.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ def __init__(
9797
def _set_config(self, datafile):
9898
""" Looks up and sets datafile and config based on response body.
9999
100-
Args:
101-
datafile: JSON string representing the Optimizely project.
102-
"""
100+
Args:
101+
datafile: JSON string representing the Optimizely project.
102+
"""
103103

104104
if self.validate_schema:
105105
if not validator.is_datafile_valid(datafile):
@@ -239,9 +239,9 @@ def get_datafile_url(sdk_key, url, url_template):
239239
def _set_config(self, datafile):
240240
""" Looks up and sets datafile and config based on response body.
241241
242-
Args:
243-
datafile: JSON string representing the Optimizely project.
244-
"""
242+
Args:
243+
datafile: JSON string representing the Optimizely project.
244+
"""
245245
if datafile or self._config_ready_event.is_set():
246246
super(PollingConfigManager, self)._set_config(datafile=datafile)
247247
self._config_ready_event.set()
@@ -261,7 +261,7 @@ def set_update_interval(self, update_interval):
261261
""" Helper method to set frequency at which datafile has to be polled and ProjectConfig updated.
262262
263263
Args:
264-
update_interval: Time in seconds after which to update datafile.
264+
update_interval: Time in seconds after which to update datafile.
265265
"""
266266
if update_interval is None:
267267
update_interval = enums.ConfigManager.DEFAULT_UPDATE_INTERVAL
@@ -287,7 +287,7 @@ def set_blocking_timeout(self, blocking_timeout):
287287
""" Helper method to set time in seconds to block the config call until config has been initialized.
288288
289289
Args:
290-
blocking_timeout: Time in seconds to block the config call.
290+
blocking_timeout: Time in seconds to block the config call.
291291
"""
292292
if blocking_timeout is None:
293293
blocking_timeout = enums.ConfigManager.DEFAULT_BLOCKING_TIMEOUT
@@ -312,9 +312,9 @@ def set_blocking_timeout(self, blocking_timeout):
312312
def set_last_modified(self, response_headers):
313313
""" Looks up and sets last modified time based on Last-Modified header in the response.
314314
315-
Args:
316-
response_headers: requests.Response.headers
317-
"""
315+
Args:
316+
response_headers: requests.Response.headers
317+
"""
318318
self.last_modified = response_headers.get(enums.HTTPHeaders.LAST_MODIFIED)
319319

320320
def _handle_response(self, response):
@@ -379,32 +379,32 @@ class AuthDatafilePollingConfigManager(PollingConfigManager):
379379

380380
def __init__(
381381
self,
382-
access_token,
382+
datafile_access_token,
383383
*args,
384384
**kwargs
385385
):
386386
""" Initialize config manager. One of sdk_key or url has to be set to be able to use.
387387
388388
Args:
389-
access_token: String to be attached to the request header to fetch the authenticated datafile.
389+
datafile_access_token: String to be attached to the request header to fetch the authenticated datafile.
390390
*args: Refer to arguments descriptions in PollingConfigManager.
391391
**kwargs: Refer to keyword arguments descriptions in PollingConfigManager.
392392
"""
393-
self._set_access_token(access_token)
393+
self._set_datafile_access_token(datafile_access_token)
394394
super(AuthDatafilePollingConfigManager, self).__init__(*args, **kwargs)
395395

396-
def _set_access_token(self, access_token):
396+
def _set_datafile_access_token(self, datafile_access_token):
397397
""" Checks for valid access token input and sets it. """
398-
if not access_token:
398+
if not datafile_access_token:
399399
raise optimizely_exceptions.InvalidInputException(
400-
'access_token cannot be empty or None.')
401-
self.access_token = access_token
400+
'datafile_access_token cannot be empty or None.')
401+
self.datafile_access_token = datafile_access_token
402402

403403
def fetch_datafile(self):
404404
""" Fetch authenticated datafile and set ProjectConfig. """
405405
request_headers = {
406406
enums.HTTPHeaders.AUTHORIZATION: enums.ConfigManager.AUTHORIZATION_HEADER_DATA_TEMPLATE.format(
407-
access_token=self.access_token
407+
datafile_access_token=self.datafile_access_token
408408
)
409409
}
410410

optimizely/helpers/enums.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class RolloutRuleAudienceEvaluationLogs(CommonAudienceEvaluationLogs):
5858

5959
class ConfigManager(object):
6060
AUTHENTICATED_DATAFILE_URL_TEMPLATE = 'https://config.optimizely.com/datafiles/auth/{sdk_key}.json'
61-
AUTHORIZATION_HEADER_DATA_TEMPLATE = 'Bearer {access_token}'
61+
AUTHORIZATION_HEADER_DATA_TEMPLATE = 'Bearer {datafile_access_token}'
6262
DATAFILE_URL_TEMPLATE = 'https://cdn.optimizely.com/datafiles/{sdk_key}.json'
6363
# Default time in seconds to block the 'get_config' method call until 'config' instance has been initialized.
6464
DEFAULT_BLOCKING_TIMEOUT = 10

optimizely/optimizely.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def __init__(
4444
config_manager=None,
4545
notification_center=None,
4646
event_processor=None,
47-
access_token=None,
47+
datafile_access_token=None,
4848
):
4949
""" Optimizely init method for managing Custom projects.
5050
@@ -67,7 +67,7 @@ def __init__(
6767
By default optimizely.event.event_processor.ForwardingEventProcessor is used
6868
which simply forwards events to the event dispatcher.
6969
To enable event batching configure and use optimizely.event.event_processor.BatchEventProcessor.
70-
access_token: Optional string used to fetch authenticated datafile for a secure project environment.
70+
datafile_access_token: Optional string used to fetch authenticated datafile for a secure project environment.
7171
"""
7272
self.logger_name = '.'.join([__name__, self.__class__.__name__])
7373
self.is_valid = True
@@ -101,8 +101,8 @@ def __init__(
101101
if not self.config_manager:
102102
if sdk_key:
103103
config_manager_options['sdk_key'] = sdk_key
104-
if access_token:
105-
config_manager_options['access_token'] = access_token
104+
if datafile_access_token:
105+
config_manager_options['datafile_access_token'] = datafile_access_token
106106
self.config_manager = AuthDatafilePollingConfigManager(**config_manager_options)
107107
else:
108108
self.config_manager = PollingConfigManager(**config_manager_options)

tests/test_config_manager.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -402,34 +402,34 @@ def test_is_running(self, _):
402402

403403
@mock.patch('requests.get')
404404
class AuthDatafilePollingConfigManagerTest(base.BaseTest):
405-
def test_init__access_token_none__fails(self, _):
406-
""" Test that initialization fails if access_token is None. """
405+
def test_init__datafile_access_token_none__fails(self, _):
406+
""" Test that initialization fails if datafile_access_token is None. """
407407
self.assertRaisesRegexp(
408408
optimizely_exceptions.InvalidInputException,
409-
'access_token cannot be empty or None.',
409+
'datafile_access_token cannot be empty or None.',
410410
config_manager.AuthDatafilePollingConfigManager,
411-
access_token=None
411+
datafile_access_token=None
412412
)
413413

414-
def test_set_access_token(self, _):
415-
""" Test that access_token is properly set as instance variable. """
416-
access_token = 'some_token'
414+
def test_set_datafile_access_token(self, _):
415+
""" Test that datafile_access_token is properly set as instance variable. """
416+
datafile_access_token = 'some_token'
417417
sdk_key = 'some_key'
418418
with mock.patch('optimizely.config_manager.AuthDatafilePollingConfigManager.fetch_datafile'):
419419
project_config_manager = config_manager.AuthDatafilePollingConfigManager(
420-
access_token=access_token, sdk_key=sdk_key)
420+
datafile_access_token=datafile_access_token, sdk_key=sdk_key)
421421

422-
self.assertEqual(access_token, project_config_manager.access_token)
422+
self.assertEqual(datafile_access_token, project_config_manager.datafile_access_token)
423423

424424
def test_fetch_datafile(self, _):
425425
""" Test that fetch_datafile sets authorization header in request header and sets config based on response. """
426-
access_token = 'some_token'
426+
datafile_access_token = 'some_token'
427427
sdk_key = 'some_key'
428428
with mock.patch('optimizely.config_manager.AuthDatafilePollingConfigManager.fetch_datafile'), mock.patch(
429429
'optimizely.config_manager.AuthDatafilePollingConfigManager._run'
430430
):
431431
project_config_manager = config_manager.AuthDatafilePollingConfigManager(
432-
access_token=access_token, sdk_key=sdk_key)
432+
datafile_access_token=datafile_access_token, sdk_key=sdk_key)
433433
expected_datafile_url = enums.ConfigManager.AUTHENTICATED_DATAFILE_URL_TEMPLATE.format(sdk_key=sdk_key)
434434
test_headers = {'Last-Modified': 'New Time'}
435435
test_datafile = json.dumps(self.config_dict_with_features)
@@ -445,7 +445,8 @@ def test_fetch_datafile(self, _):
445445

446446
mock_request.assert_called_once_with(
447447
expected_datafile_url,
448-
headers={'Authorization': 'Bearer {access_token}'.format(access_token=access_token)},
448+
headers={'Authorization': 'Bearer {datafile_access_token}'.format(
449+
datafile_access_token=datafile_access_token)},
449450
timeout=enums.ConfigManager.REQUEST_TIMEOUT,
450451
)
451452

tests/test_optimizely.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,13 +252,16 @@ def test_init__sdk_key_and_datafile(self):
252252

253253
self.assertIs(type(opt_obj.config_manager), config_manager.PollingConfigManager)
254254

255-
def test_init__sdk_key_and_access_token(self):
256-
""" Test that if both sdk_key and access_token is provided then AuthDatafilePollingConfigManager is used. """
255+
def test_init__sdk_key_and_datafile_access_token(self):
256+
"""
257+
Test that if both sdk_key and datafile_access_token is provided then AuthDatafilePollingConfigManager
258+
is used.
259+
"""
257260

258261
with mock.patch('optimizely.config_manager.AuthDatafilePollingConfigManager._set_config'), mock.patch(
259262
'threading.Thread.start'
260263
):
261-
opt_obj = optimizely.Optimizely(access_token='test_access_token', sdk_key='test_sdk_key')
264+
opt_obj = optimizely.Optimizely(datafile_access_token='test_datafile_access_token', sdk_key='test_sdk_key')
262265

263266
self.assertIs(type(opt_obj.config_manager), config_manager.AuthDatafilePollingConfigManager)
264267

0 commit comments

Comments
 (0)