Skip to content

Commit 7cb210f

Browse files
Adding update_sync_token functionality to Appconfig Clients (Azure#17421)
* breaking off into a second PR for just update_sync_token functionality * adding newline * updating based on xiangs comments, updating changelog * changes to sync token test, upgrade to 1.2.0
1 parent 340e41d commit 7cb210f

13 files changed

+2223
-16
lines changed

sdk/appconfiguration/azure-appconfiguration/CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33

44
-------------------
55

6-
## 1.1.2 (Unreleased)
6+
## 1.2.0 (Unreleased)
77

8+
### Features
9+
10+
- Adds method `update_sync_token` to include sync tokens from EventGrid notifications.
811

912
## 1.1.1 (2020-10-05)
1013

sdk/appconfiguration/azure-appconfiguration/azure/appconfiguration/_azure_appconfiguration_client.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,12 @@ def __init__(self, base_url, credential, **kwargs):
8181
base_user_agent=USER_AGENT, **kwargs
8282
)
8383

84+
self._sync_token_policy = None
85+
8486
pipeline = kwargs.get("pipeline")
8587

8688
if pipeline is None:
89+
self._sync_token_policy = SyncTokenPolicy()
8790
aad_mode = not isinstance(credential, AppConfigConnectionStringCredential)
8891
pipeline = self._create_appconfig_pipeline(
8992
credential=credential, aad_mode=aad_mode, base_url=base_url, **kwargs
@@ -140,7 +143,7 @@ def _create_appconfig_pipeline(
140143
self._config.headers_policy,
141144
self._config.user_agent_policy,
142145
self._config.retry_policy,
143-
SyncTokenPolicy(),
146+
self._sync_token_policy,
144147
credential_policy,
145148
self._config.logging_policy, # HTTP request/response log
146149
DistributedTracingPolicy(**kwargs),
@@ -578,3 +581,22 @@ def set_read_only(
578581
raise HttpResponseError(message=error.message, response=error.response)
579582
except binascii.Error:
580583
raise binascii.Error("Connection string secret has incorrect padding")
584+
585+
def update_sync_token(self, token):
586+
# type: (str) -> None
587+
588+
"""Add a sync token to the internal list of tokens.
589+
:param token: The sync token to be added to the internal list of tokens
590+
:type token: str
591+
"""
592+
if not self._sync_token_policy:
593+
raise AttributeError(
594+
"Client has no sync token policy, possibly because it was not provided during instantiation."
595+
)
596+
self._sync_token_policy.add_token(token)
597+
598+
def close(self):
599+
# type: (...) -> None
600+
601+
"""Close all connections made by the client"""
602+
self._impl._client.close()

sdk/appconfiguration/azure-appconfiguration/azure/appconfiguration/_sync_token.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ def from_sync_token_string(cls, sync_token):
5555
class SyncTokenPolicy(SansIOHTTPPolicy):
5656
"""A simple policy that enable the given callback
5757
with the response.
58-
5958
:keyword callback raw_response_hook: Callback function. Will be invoked on response.
6059
"""
6160

@@ -66,7 +65,6 @@ def __init__(self, **kwargs): # pylint: disable=unused-argument
6665
def on_request(self, request): # type: ignore # pylint: disable=arguments-differ
6766
# type: (PipelineRequest) -> None
6867
"""This is executed before sending the request to the next policy.
69-
7068
:param request: The PipelineRequest object.
7169
:type request: ~azure.core.pipeline.PipelineRequest
7270
"""
@@ -79,7 +77,6 @@ def on_request(self, request): # type: ignore # pylint: disable=arguments-diffe
7977
def on_response(self, request, response): # type: ignore # pylint: disable=arguments-differ
8078
# type: (PipelineRequest, PipelineResponse) -> None
8179
"""This is executed after the request comes back from the policy.
82-
8380
:param request: The PipelineRequest object.
8481
:type request: ~azure.core.pipeline.PipelineRequest
8582
:param response: The PipelineResponse object.
@@ -93,11 +90,22 @@ def on_response(self, request, response): # type: ignore # pylint: disable=argu
9390
return
9491
for sync_token_string in sync_token_strings:
9592
sync_token = SyncToken.from_sync_token_string(sync_token_string)
96-
if not sync_token:
97-
continue
98-
existing_token = self._sync_tokens.get(sync_token.token_id, None)
99-
if not existing_token:
100-
self._sync_tokens[sync_token.token_id] = sync_token
101-
continue
102-
if existing_token.sequence_number < sync_token.sequence_number:
103-
self._sync_tokens[sync_token.token_id] = sync_token
93+
self._update_sync_token(sync_token)
94+
95+
def add_token(self, full_raw_tokens):
96+
# type: (str) -> None
97+
raw_tokens = full_raw_tokens.split(",")
98+
for raw_token in raw_tokens:
99+
sync_token = SyncToken.from_sync_token_string(raw_token)
100+
self._update_sync_token(sync_token)
101+
102+
def _update_sync_token(self, sync_token):
103+
# type: (SyncToken) -> None
104+
if not sync_token:
105+
return
106+
existing_token = self._sync_tokens.get(sync_token.token_id, None)
107+
if not existing_token:
108+
self._sync_tokens[sync_token.token_id] = sync_token
109+
return
110+
if existing_token.sequence_number < sync_token.sequence_number:
111+
self._sync_tokens[sync_token.token_id] = sync_token

sdk/appconfiguration/azure-appconfiguration/azure/appconfiguration/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
# Licensed under the MIT License.
44
# ------------------------------------
55

6-
VERSION = "1.1.2"
6+
VERSION = "1.2.0"

sdk/appconfiguration/azure-appconfiguration/azure/appconfiguration/aio/_azure_configuration_client_async.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,12 @@ def __init__(self, base_url, credential, **kwargs):
8484
base_user_agent=USER_AGENT, **kwargs
8585
)
8686

87+
self._sync_token_policy = None
88+
8789
pipeline = kwargs.get("pipeline")
8890

8991
if pipeline is None:
92+
self._sync_token_policy = SyncTokenPolicy()
9093
aad_mode = not isinstance(credential, AppConfigConnectionStringCredential)
9194
pipeline = self._create_appconfig_pipeline(
9295
credential=credential, aad_mode=aad_mode, base_url=base_url, **kwargs
@@ -148,8 +151,8 @@ def _create_appconfig_pipeline(
148151
self._config.headers_policy,
149152
self._config.user_agent_policy,
150153
self._config.retry_policy,
154+
self._sync_token_policy,
151155
credential_policy,
152-
SyncTokenPolicy(),
153156
self._config.logging_policy, # HTTP request/response log
154157
DistributedTracingPolicy(**kwargs),
155158
HttpLoggingPolicy(**kwargs),
@@ -597,3 +600,19 @@ async def set_read_only(
597600
raise HttpResponseError(message=error.message, response=error.response)
598601
except binascii.Error:
599602
raise binascii.Error("Connection string secret has incorrect padding")
603+
604+
def update_sync_token(self, token):
605+
# type: (str) -> None
606+
607+
"""Add a sync token to the internal list of tokens.
608+
:param token: The sync token to be added to the internal list of tokens
609+
:type token: str
610+
"""
611+
612+
self._sync_token_policy.add_token(token)
613+
614+
async def close(self):
615+
# type: (...) -> None
616+
617+
"""Close all connections made by the client"""
618+
await self._impl._client.close()

0 commit comments

Comments
 (0)