|
| 1 | +# -------------------------------------------------------------------------- |
| 2 | +# |
| 3 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 4 | +# |
| 5 | +# The MIT License (MIT) |
| 6 | +# |
| 7 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +# of this software and associated documentation files (the ""Software""), to |
| 9 | +# deal in the Software without restriction, including without limitation the |
| 10 | +# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 11 | +# sell copies of the Software, and to permit persons to whom the Software is |
| 12 | +# furnished to do so, subject to the following conditions: |
| 13 | +# |
| 14 | +# The above copyright notice and this permission notice shall be included in |
| 15 | +# all copies or substantial portions of the Software. |
| 16 | +# |
| 17 | +# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 | +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 23 | +# IN THE SOFTWARE. |
| 24 | +# |
| 25 | +# -------------------------------------------------------------------------- |
| 26 | +from typing import Any, Dict |
| 27 | +from asyncio import Lock |
| 28 | +from azure.core.pipeline import PipelineRequest, PipelineResponse |
| 29 | +from azure.core.pipeline.policies import SansIOHTTPPolicy |
| 30 | + |
| 31 | +from .._sync_token import SyncToken |
| 32 | + |
| 33 | + |
| 34 | +class AsyncSyncTokenPolicy(SansIOHTTPPolicy): |
| 35 | + """A simple policy that enable the given callback |
| 36 | + with the response. |
| 37 | + :keyword callback raw_response_hook: Callback function. Will be invoked on response. |
| 38 | + """ |
| 39 | + |
| 40 | + def __init__(self, **kwargs): # pylint: disable=unused-argument |
| 41 | + # type: (**Any) -> None |
| 42 | + self._sync_token_header = "Sync-Token" |
| 43 | + self._sync_tokens = {} # type: Dict[str, Any] |
| 44 | + self._lock = Lock() |
| 45 | + |
| 46 | + async def on_request(self, request): # type: ignore # pylint: disable=arguments-differ, invalid-overridden-method |
| 47 | + # type: (PipelineRequest) -> None |
| 48 | + """This is executed before sending the request to the next policy. |
| 49 | + :param request: The PipelineRequest object. |
| 50 | + :type request: ~azure.core.pipeline.PipelineRequest |
| 51 | + """ |
| 52 | + async with self._lock: |
| 53 | + sync_token_header = ",".join(str(x) for x in self._sync_tokens.values()) |
| 54 | + if sync_token_header: |
| 55 | + request.http_request.headers.update( |
| 56 | + {self._sync_token_header: sync_token_header} |
| 57 | + ) |
| 58 | + |
| 59 | + async def on_response(self, request, response): # type: ignore # pylint: disable=arguments-differ, invalid-overridden-method |
| 60 | + # type: (PipelineRequest, PipelineResponse) -> None |
| 61 | + """This is executed after the request comes back from the policy. |
| 62 | + :param request: The PipelineRequest object. |
| 63 | + :type request: ~azure.core.pipeline.PipelineRequest |
| 64 | + :param response: The PipelineResponse object. |
| 65 | + :type response: ~azure.core.pipeline.PipelineResponse |
| 66 | + """ |
| 67 | + sync_token_header = response.http_response.headers.get(self._sync_token_header) |
| 68 | + if not sync_token_header: |
| 69 | + return |
| 70 | + sync_token_strings = sync_token_header.split(",") |
| 71 | + if not sync_token_strings: |
| 72 | + return |
| 73 | + for sync_token_string in sync_token_strings: |
| 74 | + sync_token = SyncToken.from_sync_token_string(sync_token_string) |
| 75 | + await self._update_sync_token(sync_token) |
| 76 | + |
| 77 | + async def add_token(self, full_raw_tokens): |
| 78 | + # type: (str) -> None |
| 79 | + raw_tokens = full_raw_tokens.split(",") |
| 80 | + for raw_token in raw_tokens: |
| 81 | + sync_token = SyncToken.from_sync_token_string(raw_token) |
| 82 | + await self._update_sync_token(sync_token) |
| 83 | + |
| 84 | + async def _update_sync_token(self, sync_token): |
| 85 | + # type: (SyncToken) -> None |
| 86 | + if not sync_token: |
| 87 | + return |
| 88 | + async with self._lock: |
| 89 | + existing_token = self._sync_tokens.get(sync_token.token_id, None) |
| 90 | + if not existing_token: |
| 91 | + self._sync_tokens[sync_token.token_id] = sync_token |
| 92 | + return |
| 93 | + if existing_token.sequence_number < sync_token.sequence_number: |
| 94 | + self._sync_tokens[sync_token.token_id] = sync_token |
0 commit comments