def example_on_exception(): policy = SansIOHTTPPolicy() request = HttpRequest("GET", "https://bing.com") # [START on_exception] try: response = policy.next.send(request) except Exception: if not policy.on_exception(request): raise # or use exc_type, exc_value, exc_traceback = sys.exc_info()
async def test_sans_io_exception(): class BrokenSender(AsyncHttpTransport): async def send(self, request, **config): raise ValueError("Broken") async def open(self): self.session = requests.Session() async def close(self): self.session.close() async def __aexit__(self, exc_type, exc_value, traceback): """Raise any exception triggered within the runtime context.""" return self.close() pipeline = AsyncPipeline(BrokenSender(), [SansIOHTTPPolicy()]) req = HttpRequest('GET', '/') with pytest.raises(ValueError): await pipeline.run(req) class SwapExec(SansIOHTTPPolicy): def on_exception(self, requests, **kwargs): exc_type, exc_value, exc_traceback = sys.exc_info() raise NotImplementedError(exc_value) pipeline = AsyncPipeline(BrokenSender(), [SwapExec()]) with pytest.raises(NotImplementedError): await pipeline.run(req)
def cf_boolean_cl(cli_ctx, *_): from azure.cli.core.commands.client_factory import get_mgmt_service_client from ..vendored_sdks.boolean import AutoRestTestService from azure.core.pipeline.policies import SansIOHTTPPolicy return get_mgmt_service_client(cli_ctx, AutoRestTestService, subscription_bound=False, base_url_bound=False, authentication_policy=SansIOHTTPPolicy())
def _prepare_client_kwargs_track2(cli_ctx): """Prepare kwargs for Track 2 SDK client.""" client_kwargs = {} # Prepare connection_verify to change SSL verification behavior, used by ConnectionConfiguration client_kwargs.update(_debug.change_ssl_cert_verification_track2()) # Prepare User-Agent header, used by UserAgentPolicy client_kwargs['user_agent'] = get_az_user_agent() try: command_ext_name = cli_ctx.data['command_extension_name'] if command_ext_name: client_kwargs['user_agent'] += "CliExtension/{}".format( command_ext_name) except KeyError: pass # Prepare custom headers, used by HeadersPolicy headers = dict(cli_ctx.data['headers']) # - Prepare CommandName header command_name_suffix = ';completer-request' if cli_ctx.data[ 'completer_active'] else '' headers['CommandName'] = "{}{}".format(cli_ctx.data['command'], command_name_suffix) # - Prepare ParameterSetName header if cli_ctx.data.get('safe_params'): headers['ParameterSetName'] = ' '.join(cli_ctx.data['safe_params']) client_kwargs['headers'] = headers # Prepare x-ms-client-request-id header, used by RequestIdPolicy if 'x-ms-client-request-id' in cli_ctx.data['headers']: client_kwargs['request_id'] = cli_ctx.data['headers'][ 'x-ms-client-request-id'] # Replace NetworkTraceLoggingPolicy to redact 'Authorization' and 'x-ms-authorization-auxiliary' headers. # NetworkTraceLoggingPolicy: log raw network trace, with all headers. from azure.cli.core.sdk.policies import SafeNetworkTraceLoggingPolicy client_kwargs['logging_policy'] = SafeNetworkTraceLoggingPolicy() # Disable ARMHttpLoggingPolicy. # ARMHttpLoggingPolicy: Only log allowed information. from azure.core.pipeline.policies import SansIOHTTPPolicy client_kwargs['http_logging_policy'] = SansIOHTTPPolicy() return client_kwargs
def _configure( self, **kwargs ): from azure.core.pipeline.policies import UserAgentPolicy, HeadersPolicy, ProxyPolicy, \ RetryPolicy, CustomHookPolicy, RedirectPolicy, SansIOHTTPPolicy from azure.cli.core.sdk.policies import SafeNetworkTraceLoggingPolicy from ._http_policy import AAZBearerTokenCredentialPolicy self.user_agent_policy = kwargs.get('user_agent_policy') or UserAgentPolicy(**kwargs) self.headers_policy = kwargs.get('headers_policy') or HeadersPolicy(**kwargs) self.proxy_policy = kwargs.get('proxy_policy') or ProxyPolicy(**kwargs) self.logging_policy = kwargs.get('logging_policy') or SafeNetworkTraceLoggingPolicy(**kwargs) self.http_logging_policy = kwargs.get('http_logging_policy') or SansIOHTTPPolicy() self.retry_policy = kwargs.get('retry_policy') or RetryPolicy(**kwargs) self.custom_hook_policy = kwargs.get('custom_hook_policy') or CustomHookPolicy(**kwargs) self.redirect_policy = kwargs.get('redirect_policy') or RedirectPolicy(**kwargs) self.authentication_policy = kwargs.get('authentication_policy') if self.credential and not self.authentication_policy: self.authentication_policy = AAZBearerTokenCredentialPolicy( self.credential, *self.credential_scopes, **kwargs)