codekingpro/portable-devtools
114k
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# --------------------------------------------------------------------------26import abc27 28from typing import Any, List, Union, Callable, AsyncIterator, Optional29 30try:31 from contextlib import AbstractAsyncContextManager # type: ignore32except ImportError: # Python <= 3.733 class AbstractAsyncContextManager(object): # type: ignore34 async def __aenter__(self):35 """Return `self` upon entering the runtime context."""36 return self37 38 @abc.abstractmethod39 async def __aexit__(self, exc_type, exc_value, traceback):40 """Raise any exception triggered within the runtime context."""41 return None42 43from . import ClientRequest, HTTPClientResponse44 45 46class AsyncClientResponse(HTTPClientResponse):47 48 def stream_download(self, chunk_size: Optional[int] = None, callback: Optional[Callable] = None) -> AsyncIterator[bytes]:49 """Generator for streaming request body data.50 51 Should be implemented by sub-classes if streaming download52 is supported.53 54 :param callback: Custom callback for monitoring progress.55 :param int chunk_size:56 """57 pass58 59 60class AsyncHTTPSender(AbstractAsyncContextManager, abc.ABC):61 """An http sender ABC.62 """63 64 @abc.abstractmethod65 async def send(self, request: ClientRequest, **config: Any) -> AsyncClientResponse:66 """Send the request using this HTTP sender.67 """68 pass69 70 def build_context(self) -> Any:71 """Allow the sender to build a context that will be passed72 across the pipeline with the request.73 74 Return type has no constraints. Implementation is not75 required and None by default.76 """77 return None78 79 def __enter__(self):80 raise TypeError("Use 'async with' instead")81 82 def __exit__(self, exc_type, exc_val, exc_tb):83 # __exit__ should exist in pair with __enter__ but never executed84 pass # pragma: no cover85 86 87__all__ = [88 'AsyncHTTPSender',89 'AsyncClientResponse'90]