Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added spec query parameter #522

Merged
merged 2 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion splitio/api/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from splitio.api import APIException
from splitio.api.commons import headers_from_metadata, record_telemetry
from splitio.spec import SPEC_VERSION
from splitio.util.time import get_current_epoch_time_ms
from splitio.api.client import HttpClientException
from splitio.models.token import from_raw
Expand Down Expand Up @@ -43,7 +44,7 @@ def authenticate(self):
try:
response = self._client.get(
'auth',
'/v2/auth',
'/v2/auth?s=' + SPEC_VERSION,
self._sdk_key,
extra_headers=self._metadata,
)
Expand Down
15 changes: 12 additions & 3 deletions splitio/api/commons.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""Commons module."""
from splitio.util.time import get_current_epoch_time_ms
from splitio.spec import SPEC_VERSION

_CACHE_CONTROL = 'Cache-Control'
_CACHE_CONTROL_NO_CACHE = 'no-cache'


def headers_from_metadata(sdk_metadata, client_key=None):
"""
Generate a dict with headers required by data-recording API endpoints.
Expand Down Expand Up @@ -57,7 +57,7 @@ def record_telemetry(status_code, elapsed, metric_name, telemetry_runtime_produc
class FetchOptions(object):
"""Fetch Options object."""

def __init__(self, cache_control_headers=False, change_number=None, sets=None):
def __init__(self, cache_control_headers=False, change_number=None, sets=None, spec=SPEC_VERSION):
"""
Class constructor.

Expand All @@ -73,6 +73,7 @@ def __init__(self, cache_control_headers=False, change_number=None, sets=None):
self._cache_control_headers = cache_control_headers
self._change_number = change_number
self._sets = sets
self._spec = spec

@property
def cache_control_headers(self):
Expand All @@ -89,6 +90,11 @@ def sets(self):
"""Return sets."""
return self._sets

@property
def spec(self):
"""Return sets."""
return self._spec

def __eq__(self, other):
"""Match between other options."""
if self._cache_control_headers != other._cache_control_headers:
Expand All @@ -97,6 +103,8 @@ def __eq__(self, other):
return False
if self._sets != other._sets:
return False
if self._spec != other._spec:
return False
return True


Expand All @@ -116,7 +124,8 @@ def build_fetch(change_number, fetch_options, metadata):
:return: Objects for fetch
:rtype: dict, dict
"""
query = {'since': change_number}
query = {'s': fetch_options.spec} if fetch_options.spec is not None else {}
query['since'] = change_number
extra_headers = metadata
if fetch_options is None:
return query, extra_headers
Expand Down
1 change: 1 addition & 0 deletions splitio/spec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SPEC_VERSION = '1.1'
4 changes: 2 additions & 2 deletions splitio/sync/segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,13 @@ def synchronize_segment(self, segment_name, till=None):
:return: True if no error occurs. False otherwise.
:rtype: bool
"""
fetch_options = FetchOptions(True) # Set Cache-Control to no-cache
fetch_options = FetchOptions(True, None, None, None) # Set Cache-Control to no-cache
successful_sync, remaining_attempts, change_number = self._attempt_segment_sync(segment_name, fetch_options, till)
attempts = _ON_DEMAND_FETCH_BACKOFF_MAX_RETRIES - remaining_attempts
if successful_sync: # succedeed sync
_LOGGER.debug('Refresh completed in %d attempts.', attempts)
return True
with_cdn_bypass = FetchOptions(True, change_number) # Set flag for bypassing CDN
with_cdn_bypass = FetchOptions(True, change_number, None, None) # Set flag for bypassing CDN
without_cdn_successful_sync, remaining_attempts, change_number = self._attempt_segment_sync(segment_name, with_cdn_bypass, till)
without_cdn_attempts = _ON_DEMAND_FETCH_BACKOFF_MAX_RETRIES - remaining_attempts
if without_cdn_successful_sync:
Expand Down
2 changes: 1 addition & 1 deletion tests/api/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_auth(self, mocker):
call_made = httpclient.get.mock_calls[0]

# validate positional arguments
assert call_made[1] == ('auth', '/v2/auth', 'some_api_key')
assert call_made[1] == ('auth', '/v2/auth?s=1.1', 'some_api_key')

# validate key-value args (headers)
assert call_made[2]['extra_headers'] == {
Expand Down
6 changes: 3 additions & 3 deletions tests/api/test_segments_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def test_fetch_segment_changes(self, mocker):
httpclient.get.return_value = client.HttpResponse(200, '{"prop1": "value1"}')
segment_api = segments.SegmentsAPI(httpclient, 'some_api_key', SdkMetadata('1.0', 'some', '1.2.3.4'), mocker.Mock())

response = segment_api.fetch_segment('some_segment', 123, FetchOptions())
response = segment_api.fetch_segment('some_segment', 123, FetchOptions(None, None, None, None))
assert response['prop1'] == 'value1'
assert httpclient.get.mock_calls == [mocker.call('sdk', '/segmentChanges/some_segment', 'some_api_key',
extra_headers={
Expand All @@ -29,7 +29,7 @@ def test_fetch_segment_changes(self, mocker):
query={'since': 123})]

httpclient.reset_mock()
response = segment_api.fetch_segment('some_segment', 123, FetchOptions(True))
response = segment_api.fetch_segment('some_segment', 123, FetchOptions(True, None, None, None))
assert response['prop1'] == 'value1'
assert httpclient.get.mock_calls == [mocker.call('sdk', '/segmentChanges/some_segment', 'some_api_key',
extra_headers={
Expand All @@ -41,7 +41,7 @@ def test_fetch_segment_changes(self, mocker):
query={'since': 123})]

httpclient.reset_mock()
response = segment_api.fetch_segment('some_segment', 123, FetchOptions(True, 123))
response = segment_api.fetch_segment('some_segment', 123, FetchOptions(True, 123, None, None))
assert response['prop1'] == 'value1'
assert httpclient.get.mock_calls == [mocker.call('sdk', '/segmentChanges/some_segment', 'some_api_key',
extra_headers={
Expand Down
6 changes: 3 additions & 3 deletions tests/api/test_splits_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_fetch_split_changes(self, mocker):
'SplitSDKMachineIP': '1.2.3.4',
'SplitSDKMachineName': 'some'
},
query={'since': 123, 'sets': 'set1,set2'})]
query={'s': '1.1', 'since': 123, 'sets': 'set1,set2'})]

httpclient.reset_mock()
response = split_api.fetch_splits(123, FetchOptions(True))
Expand All @@ -39,7 +39,7 @@ def test_fetch_split_changes(self, mocker):
'SplitSDKMachineName': 'some',
'Cache-Control': 'no-cache'
},
query={'since': 123})]
query={'s': '1.1', 'since': 123})]

httpclient.reset_mock()
response = split_api.fetch_splits(123, FetchOptions(True, 123, 'set3'))
Expand All @@ -51,7 +51,7 @@ def test_fetch_split_changes(self, mocker):
'SplitSDKMachineName': 'some',
'Cache-Control': 'no-cache'
},
query={'since': 123, 'till': 123, 'sets': 'set3'})]
query={'s': '1.1', 'since': 123, 'till': 123, 'sets': 'set3'})]

httpclient.reset_mock()
def raise_exception(*args, **kwargs):
Expand Down
Loading