-
Notifications
You must be signed in to change notification settings - Fork 115
Add option to set If-Match
eTag for objects write requests
#205
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9e55215
Add option to set `If-Match` eTag for objects write requests
seba-aln 445a695
Added tests and some fixes
seba-aln e83787a
examples adjustment
seba-aln 431c499
My bad
seba-aln 0ad33d9
How to get http status in example
seba-aln 56e195f
PubNub SDK 10.2.0 release.
pubnub-release-bot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import os | ||
|
||
from copy import deepcopy | ||
from pubnub.pubnub import PubNub | ||
from pubnub.pnconfiguration import PNConfiguration | ||
from pubnub.exceptions import PubNubException | ||
|
||
config = PNConfiguration() | ||
config.publish_key = os.getenv('PUBLISH_KEY', default='demo') | ||
config.subscribe_key = os.getenv('SUBSCRIBE_KEY', default='demo') | ||
config.user_id = "example" | ||
|
||
config_2 = deepcopy(config) | ||
config_2.user_id = "example_2" | ||
|
||
pubnub = PubNub(config) | ||
pubnub_2 = PubNub(config_2) | ||
|
||
sample_user = { | ||
"uuid": "SampleUser", | ||
"name": "John Doe", | ||
"email": "[email protected]", | ||
"custom": {"age": 42, "address": "123 Main St."}, | ||
} | ||
|
||
# One client creates a metada for the user "SampleUser" and successfully writes it to the server. | ||
set_result = pubnub.set_uuid_metadata( | ||
**sample_user, | ||
include_custom=True, | ||
include_status=True, | ||
include_type=True | ||
).sync() | ||
|
||
# We store the eTag for the user for further updates. | ||
original_e_tag = set_result.result.data.get('eTag') | ||
|
||
# Another client sets the user meta with the same UUID but different data. | ||
overwrite_result = pubnub_2.set_uuid_metadata(uuid="SampleUser", name="Jane Doe").sync() | ||
new_e_tag = overwrite_result.result.data.get('eTag') | ||
|
||
# We can verify that there is a new eTag for the user. | ||
print(f"{original_e_tag == new_e_tag=}") | ||
|
||
# We modify the user and try to update it. | ||
updated_user = {**sample_user, "custom": {"age": 43, "address": "321 Other St."}} | ||
|
||
try: | ||
update_result = pubnub.set_uuid_metadata( | ||
**updated_user, | ||
include_custom=True, | ||
include_status=True, | ||
include_type=True | ||
).if_matches_etag(original_e_tag).sync() | ||
except PubNubException as e: | ||
# We get an exception and after reading the error message we can see that the reason is that the eTag is outdated. | ||
print(f"Update failed: {e.get_error_message().get('message')}\nHTTP Status Code: {e.get_status_code()}") | ||
|
||
|
||
except Exception as e: | ||
print(f"Unexpected error: {e}") | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
import pytest | ||
from pubnub.endpoints.endpoint import Endpoint | ||
from pubnub.endpoints.objects_v2.channel.get_all_channels import GetAllChannels | ||
from pubnub.endpoints.objects_v2.channel.get_channel import GetChannel | ||
from pubnub.endpoints.objects_v2.channel.remove_channel import RemoveChannel | ||
from pubnub.endpoints.objects_v2.channel.set_channel import SetChannel | ||
from pubnub.exceptions import PubNubException | ||
from pubnub.models.consumer.common import PNStatus | ||
from pubnub.models.consumer.objects_v2.channel import PNSetChannelMetadataResult, PNGetChannelMetadataResult, \ | ||
PNRemoveChannelMetadataResult, PNGetAllChannelMetadataResult | ||
from pubnub.models.consumer.objects_v2.sort import PNSortKey, PNSortKeyValue | ||
from pubnub.pubnub_asyncio import PubNubAsyncio | ||
from pubnub.models.envelopes import AsyncioEnvelope | ||
from tests.helper import pnconf_env_copy | ||
from tests.integrational.vcr_helper import pn_vcr | ||
|
||
|
||
def _pubnub(): | ||
config = pnconf_env_copy() | ||
return PubNubAsyncio(config) | ||
|
||
|
||
class TestObjectsV2Channel: | ||
_some_channel_id = "somechannelid" | ||
_some_name = "Some name" | ||
_some_description = "Some description" | ||
_some_custom = { | ||
"key1": "val1", | ||
"key2": "val2" | ||
} | ||
|
||
def test_set_channel_endpoint_available(self): | ||
pn = _pubnub() | ||
set_channel = pn.set_channel_metadata() | ||
assert set_channel is not None | ||
assert isinstance(set_channel, SetChannel) | ||
assert isinstance(set_channel, Endpoint) | ||
|
||
def test_set_channel_is_endpoint(self): | ||
pn = _pubnub() | ||
set_channel = pn.set_channel_metadata() | ||
assert isinstance(set_channel, SetChannel) | ||
assert isinstance(set_channel, Endpoint) | ||
|
||
@pn_vcr.use_cassette('tests/integrational/fixtures/asyncio/objects_v2/channel/set_channel.json', | ||
filter_query_parameters=['uuid', 'pnsdk', 'l_obj'], serializer='pn_json') | ||
@pytest.mark.asyncio | ||
async def test_set_channel_happy_path(self): | ||
pn = _pubnub() | ||
|
||
set_channel_result = await pn.set_channel_metadata() \ | ||
.include_custom(True) \ | ||
.channel(TestObjectsV2Channel._some_channel_id) \ | ||
.set_name(TestObjectsV2Channel._some_name) \ | ||
.description(TestObjectsV2Channel._some_description) \ | ||
.custom(TestObjectsV2Channel._some_custom) \ | ||
.future() | ||
|
||
assert isinstance(set_channel_result, AsyncioEnvelope) | ||
assert isinstance(set_channel_result.result, PNSetChannelMetadataResult) | ||
assert isinstance(set_channel_result.status, PNStatus) | ||
assert not set_channel_result.status.is_error() | ||
data = set_channel_result.result.data | ||
assert data['id'] == TestObjectsV2Channel._some_channel_id | ||
assert data['name'] == TestObjectsV2Channel._some_name | ||
assert data['description'] == TestObjectsV2Channel._some_description | ||
assert data['custom'] == TestObjectsV2Channel._some_custom | ||
|
||
def test_get_channel_endpoint_available(self): | ||
pn = _pubnub() | ||
get_channel = pn.get_channel_metadata() | ||
assert get_channel is not None | ||
assert isinstance(get_channel, GetChannel) | ||
assert isinstance(get_channel, Endpoint) | ||
|
||
def test_get_channel_is_endpoint(self): | ||
pn = _pubnub() | ||
get_channel = pn.get_channel_metadata() | ||
assert isinstance(get_channel, GetChannel) | ||
assert isinstance(get_channel, Endpoint) | ||
|
||
@pn_vcr.use_cassette('tests/integrational/fixtures/asyncio/objects_v2/channel/get_channel.json', | ||
filter_query_parameters=['uuid', 'pnsdk', 'l_obj'], serializer='pn_json') | ||
@pytest.mark.asyncio | ||
async def test_get_channel_happy_path(self): | ||
pn = _pubnub() | ||
|
||
get_channel_result = await pn.get_channel_metadata() \ | ||
.include_custom(True) \ | ||
.channel(TestObjectsV2Channel._some_channel_id) \ | ||
.future() | ||
|
||
assert isinstance(get_channel_result, AsyncioEnvelope) | ||
assert isinstance(get_channel_result.result, PNGetChannelMetadataResult) | ||
assert isinstance(get_channel_result.status, PNStatus) | ||
assert not get_channel_result.status.is_error() | ||
data = get_channel_result.result.data | ||
assert data['id'] == TestObjectsV2Channel._some_channel_id | ||
assert data['name'] == TestObjectsV2Channel._some_name | ||
assert data['description'] == TestObjectsV2Channel._some_description | ||
assert data['custom'] == TestObjectsV2Channel._some_custom | ||
|
||
def test_remove_channel_endpoint_available(self): | ||
pn = _pubnub() | ||
remove_channel = pn.remove_channel_metadata() | ||
assert remove_channel is not None | ||
assert isinstance(remove_channel, RemoveChannel) | ||
assert isinstance(remove_channel, Endpoint) | ||
|
||
def test_remove_channel_is_endpoint(self): | ||
pn = _pubnub() | ||
remove_channel = pn.remove_channel_metadata() | ||
assert isinstance(remove_channel, RemoveChannel) | ||
assert isinstance(remove_channel, Endpoint) | ||
|
||
@pn_vcr.use_cassette('tests/integrational/fixtures/asyncio/objects_v2/channel/remove_channel.json', | ||
filter_query_parameters=['uuid', 'pnsdk', 'l_obj'], serializer='pn_json') | ||
@pytest.mark.asyncio | ||
async def test_remove_channel_happy_path(self): | ||
pn = _pubnub() | ||
|
||
remove_uid_result = await pn.remove_channel_metadata() \ | ||
.channel(TestObjectsV2Channel._some_channel_id) \ | ||
.future() | ||
|
||
assert isinstance(remove_uid_result, AsyncioEnvelope) | ||
assert isinstance(remove_uid_result.result, PNRemoveChannelMetadataResult) | ||
assert isinstance(remove_uid_result.status, PNStatus) | ||
assert not remove_uid_result.status.is_error() | ||
|
||
def test_get_all_channel_endpoint_available(self): | ||
pn = _pubnub() | ||
get_all_channel = pn.get_all_channel_metadata() | ||
assert get_all_channel is not None | ||
assert isinstance(get_all_channel, GetAllChannels) | ||
assert isinstance(get_all_channel, Endpoint) | ||
|
||
def test_get_all_channel_is_endpoint(self): | ||
pn = _pubnub() | ||
get_all_channel = pn.get_all_channel_metadata() | ||
assert isinstance(get_all_channel, GetAllChannels) | ||
assert isinstance(get_all_channel, Endpoint) | ||
|
||
@pn_vcr.use_cassette('tests/integrational/fixtures/asyncio/objects_v2/channel/get_all_channel.json', | ||
filter_query_parameters=['uuid', 'pnsdk', 'l_obj'], serializer='pn_json') | ||
@pytest.mark.asyncio | ||
async def test_get_all_channel_happy_path(self): | ||
pn = _pubnub() | ||
|
||
await pn.set_channel_metadata() \ | ||
.include_custom(True) \ | ||
.channel(TestObjectsV2Channel._some_channel_id) \ | ||
.set_name(TestObjectsV2Channel._some_name) \ | ||
.description(TestObjectsV2Channel._some_description) \ | ||
.custom(TestObjectsV2Channel._some_custom) \ | ||
.future() | ||
|
||
get_all_channel_result = await pn.get_all_channel_metadata() \ | ||
.include_custom(True) \ | ||
.limit(10) \ | ||
.include_total_count(True) \ | ||
.sort(PNSortKey.asc(PNSortKeyValue.ID), PNSortKey.desc(PNSortKeyValue.UPDATED)) \ | ||
.page(None) \ | ||
.future() | ||
|
||
assert isinstance(get_all_channel_result, AsyncioEnvelope) | ||
assert isinstance(get_all_channel_result.result, PNGetAllChannelMetadataResult) | ||
assert isinstance(get_all_channel_result.status, PNStatus) | ||
assert not get_all_channel_result.status.is_error() | ||
data = get_all_channel_result.result.data | ||
assert isinstance(data, list) | ||
assert get_all_channel_result.result.total_count != 0 | ||
assert get_all_channel_result.result.next is not None | ||
assert get_all_channel_result.result.prev is None | ||
|
||
@pn_vcr.use_cassette('tests/integrational/fixtures/asyncio/objects_v2/channel/if_matches_etag.json', | ||
filter_query_parameters=['uuid', 'pnsdk', 'l_obj'], serializer='pn_json') | ||
@pytest.mark.asyncio | ||
async def test_if_matches_etag(self): | ||
pubnub = _pubnub() | ||
|
||
set_channel = await pubnub.set_channel_metadata(channel=self._some_channel_id, name=self._some_name).future() | ||
original_etag = set_channel.result.data.get('eTag') | ||
get_channel = await pubnub.get_channel_metadata(channel=self._some_channel_id).future() | ||
assert original_etag == get_channel.result.data.get('eTag') | ||
|
||
# Update without eTag should be possible | ||
set_channel = await pubnub.set_channel_metadata(channel=self._some_channel_id, name=f"{self._some_name}-2") \ | ||
.future() | ||
|
||
# Response should contain new eTag | ||
new_etag = set_channel.result.data.get('eTag') | ||
assert original_etag != new_etag | ||
assert set_channel.result.data.get('name') == f"{self._some_name}-2" | ||
|
||
get_channel = await pubnub.get_channel_metadata(channel=self._some_channel_id).future() | ||
assert original_etag != get_channel.result.data.get('eTag') | ||
assert get_channel.result.data.get('name') == f"{self._some_name}-2" | ||
|
||
# Update with correct eTag should be possible | ||
set_channel = await pubnub.set_channel_metadata(channel=self._some_channel_id, name=f"{self._some_name}-3") \ | ||
.if_matches_etag(new_etag) \ | ||
.future() | ||
assert set_channel.result.data.get('name') == f"{self._some_name}-3" | ||
|
||
try: | ||
# Update with original - now outdated - eTag should fail | ||
set_channel = await pubnub.set_channel_metadata( | ||
channel=self._some_channel_id, | ||
name=f"{self._some_name}-3" | ||
).if_matches_etag(original_etag).future() | ||
|
||
except PubNubException as e: | ||
assert e.get_status_code() == 412 | ||
assert e.get_error_message().get('message') == 'Channel to update has been modified after it was read.' |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have the possibility to check that the HTTP status code is 412?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes. added to this example