Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
async_poller.py89 linesDownload Raw Back to polling
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 copy8# of this software and associated documentation files (the ""Software""), to9# deal in the Software without restriction, including without limitation the10# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or11# sell copies of the Software, and to permit persons to whom the Software is12# furnished to do so, subject to the following conditions:13#14# The above copyright notice and this permission notice shall be included in15# all copies or substantial portions of the Software.16#17# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE20# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING22# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS23# IN THE SOFTWARE.24#25# --------------------------------------------------------------------------26from .poller import NoPolling as _NoPolling27 28from ..serialization import Model29from ..async_client import ServiceClientAsync30from ..pipeline import ClientRawResponse31 32class AsyncPollingMethod(object):33    """ABC class for polling method.34    """35    def initialize(self, client, initial_response, deserialization_callback):36        raise NotImplementedError("This method needs to be implemented")37 38    async def run(self):39        raise NotImplementedError("This method needs to be implemented")40 41    def status(self):42        raise NotImplementedError("This method needs to be implemented")43 44    def finished(self):45        raise NotImplementedError("This method needs to be implemented")46 47    def resource(self):48        raise NotImplementedError("This method needs to be implemented")49 50 51class AsyncNoPolling(_NoPolling):52    """An empty async poller that returns the deserialized initial response.53    """54    async def run(self):55        """Empty run, no polling.56 57        Just override initial run to add "async"58        """59        pass60 61 62async def async_poller(client, initial_response, deserialization_callback, polling_method):63    """Async Poller for long running operations.64 65    :param client: A msrest service client. Can be a SDK client and it will be casted to a ServiceClient.66    :type client: msrest.service_client.ServiceClient67    :param initial_response: The initial call response68    :type initial_response: msrest.universal_http.ClientResponse or msrest.pipeline.ClientRawResponse69    :param deserialization_callback: A callback that takes a Response and return a deserialized object. If a subclass of Model is given, this passes "deserialize" as callback.70    :type deserialization_callback: callable or msrest.serialization.Model71    :param polling_method: The polling strategy to adopt72    :type polling_method: msrest.polling.PollingMethod73    """74 75    try:76        client = client if isinstance(client, ServiceClientAsync) else client._client77    except AttributeError:78        raise ValueError("Poller client parameter must be a low-level msrest Service Client or a SDK client.")79    response = initial_response.response if isinstance(initial_response, ClientRawResponse) else initial_response80 81    if isinstance(deserialization_callback, type) and issubclass(deserialization_callback, Model):82        deserialization_callback = deserialization_callback.deserialize83 84    # Might raise a CloudError85    polling_method.initialize(client, response, deserialization_callback)86 87    await polling_method.run()88    return polling_method.resource()89 
codekingpro/portable-devtools · Team Ai