def _create_config(**kwargs):
     # type: (Any) -> Configuration
     config = Configuration(**kwargs)
     config.logging_policy = NetworkTraceLoggingPolicy(**kwargs)
     config.retry_policy = RetryPolicy(**kwargs)
     config.proxy_policy = ProxyPolicy(**kwargs)
     return config
 def _create_config(**kwargs: "Any") -> Configuration:
     config = Configuration(**kwargs)
     config.logging_policy = NetworkTraceLoggingPolicy(**kwargs)
     config.retry_policy = AsyncRetryPolicy(**kwargs)
     config.proxy_policy = ProxyPolicy(**kwargs)
     config.user_agent_policy = UserAgentPolicy(base_user_agent=USER_AGENT, **kwargs)
     return config
Esempio n. 3
0
 def _create_config(**kwargs):
     # type: (Mapping[str, Any]) -> Configuration
     config = Configuration(**kwargs)
     config.logging_policy = NetworkTraceLoggingPolicy(**kwargs)
     config.retry_policy = RetryPolicy(**kwargs)
     config.proxy_policy = ProxyPolicy(**kwargs)
     config.user_agent_policy = UserAgentPolicy(base_user_agent=USER_AGENT, **kwargs)
     return config
Esempio n. 4
0
 def create_config(**kwargs):
     # type: (Mapping[str, Any]) -> Configuration
     config = Configuration(**kwargs)
     config.logging_policy = NetworkTraceLoggingPolicy(**kwargs)
     config.retry_policy = RetryPolicy(retry_on_status_codes=[404, 429] +
                                       list(range(500, 600)),
                                       **kwargs)
     return config
 def _create_configuration(self, **kwargs):  # pylint: disable=no-self-use
     config = Configuration(**kwargs)
     config.user_agent_policy = kwargs.get('user_agent_policy') or \
         UserAgentPolicy(base_user_agent=USER_AGENT, **kwargs)
     config.headers_policy = kwargs.get('headers_policy') or HeadersPolicy(**kwargs)
     config.proxy_policy = kwargs.get('proxy_policy') or ProxyPolicy(**kwargs)
     config.logging_policy = kwargs.get('logging_policy') or NetworkTraceLoggingPolicy(**kwargs)
     return config
Esempio n. 6
0
 def create_config(**kwargs: Dict[str, Any]) -> Configuration:
     timeout = kwargs.pop("connection_timeout", 2)
     config = Configuration(connection_timeout=timeout, **kwargs)
     config.logging_policy = NetworkTraceLoggingPolicy(**kwargs)
     retries = kwargs.pop("retry_total", 5)
     config.retry_policy = AsyncRetryPolicy(
         retry_total=retries,
         retry_on_status_codes=[404, 429] + list(range(500, 600)),
         **kwargs)
     return config
    def _create_configuration(self, **kwargs):  # pylint: disable=no-self-use
        config = Configuration(**kwargs)
        config.user_agent_policy = kwargs.get("user_agent_policy") or UserAgentPolicy(**kwargs)
        config.headers_policy = kwargs.get("headers_policy") or HeadersPolicy(**kwargs)
        config.proxy_policy = kwargs.get("proxy_policy") or ProxyPolicy(**kwargs)
        config.logging_policy = kwargs.get("logging_policy") or NetworkTraceLoggingPolicy(**kwargs)
        config.retry_policy = kwargs.get("retry_policy") or RetryPolicy(**kwargs)
        config.redirect_policy = kwargs.get("redirect_policy") or RedirectPolicy(**kwargs)

        return config
def _get_config(**kwargs):
    """Configuration common to a/sync pipelines"""
    config = Configuration(**kwargs)
    config.custom_hook_policy = CustomHookPolicy(**kwargs)
    config.headers_policy = HeadersPolicy(**kwargs)
    config.http_logging_policy = HttpLoggingPolicy(**kwargs)
    config.logging_policy = NetworkTraceLoggingPolicy(**kwargs)
    config.proxy_policy = ProxyPolicy(**kwargs)
    config.user_agent_policy = UserAgentPolicy(base_user_agent=USER_AGENT, **kwargs)
    return config
Esempio n. 9
0
def _get_policies(config: "Configuration", **kwargs: "Any") -> "List[PolicyType]":
    return [
        UserAgentPolicy(base_user_agent=USER_AGENT, **kwargs),
        config.proxy_policy,
        config.retry_policy,
        ArcChallengeAuthPolicy(),
        NetworkTraceLoggingPolicy(**kwargs),
        DistributedTracingPolicy(**kwargs),
        HttpLoggingPolicy(**kwargs),
    ]
Esempio n. 10
0
def _get_policies(config, **kwargs):
    return [
        HeadersPolicy(**kwargs),
        UserAgentPolicy(base_user_agent=USER_AGENT, **kwargs),
        config.proxy_policy,
        config.retry_policy,
        NetworkTraceLoggingPolicy(**kwargs),
        DistributedTracingPolicy(**kwargs),
        HttpLoggingPolicy(**kwargs),
    ]
Esempio n. 11
0
 def create_config(credential, scopes, **kwargs):
     # Here the SDK developer would define the default
     # config to interact with the service
     config = Configuration(**kwargs)
     config.headers_policy = HeadersPolicy({"CustomHeader": "Value"}, **kwargs)
     config.authentication_policy = BearerTokenCredentialPolicy(credential, scopes, **kwargs)
     config.user_agent_policy = UserAgentPolicy("ServiceUserAgentValue", **kwargs)
     config.retry_policy = RetryPolicy(**kwargs)
     config.redirect_policy = RedirectPolicy(**kwargs)
     config.logging_policy = NetworkTraceLoggingPolicy(**kwargs)
     config.proxy_policy = ProxyPolicy(**kwargs)
Esempio n. 12
0
 def _set_universal(self, **kwargs):
     Configuration.__init__(self)
     self.connection = kwargs.get("connection_configuration",
                                  ConnectionConfiguration(**kwargs))
     self.user_agent_policy = kwargs.get("user_agent_policy",
                                         UserAgentPolicy(**kwargs))
     self.proxy_policy = kwargs.get("proxy_policy", ProxyPolicy(**kwargs))
     self.logging_policy = kwargs.get("logging_policy",
                                      NetworkTraceLoggingPolicy(**kwargs))
     self.headers_policy = kwargs.get("headers_policy",
                                      HeadersPolicy(**kwargs))
Esempio n. 13
0
 def create_config(**kwargs):
     # type: (Dict[str, str]) -> Configuration
     timeout = kwargs.pop("connection_timeout", 2)
     config = Configuration(connection_timeout=timeout, **kwargs)
     config.header_policy = HeadersPolicy(base_headers={"Metadata": "true"},
                                          **kwargs)
     config.logging_policy = NetworkTraceLoggingPolicy(**kwargs)
     retries = kwargs.pop("retry_total", 5)
     config.retry_policy = RetryPolicy(retry_total=retries,
                                       retry_on_status_codes=[404, 429] +
                                       list(range(500, 600)),
                                       **kwargs)
     return config
def example_network_trace_logging():
    filename = "log.txt"
    url = "https://bing.com"
    policies = [
        UserAgentPolicy("myuseragent"),
        RedirectPolicy()
    ]

    # [START network_trace_logging_policy]
    from azure.core.pipeline.policies import NetworkTraceLoggingPolicy
    import sys
    import logging

    # Create a logger for the 'azure' SDK
    logger = logging.getLogger("azure")
    logger.setLevel(logging.DEBUG)

    # Configure a console output
    handler = logging.StreamHandler(stream=sys.stdout)
    logger.addHandler(handler)

    # Configure a file output
    file_handler = logging.FileHandler(filename)
    logger.addHandler(file_handler)

    # Enable network trace logging. This will be logged at DEBUG level.
    # By default, logging is disabled.
    logging_policy = NetworkTraceLoggingPolicy()
    logging_policy.enable_http_logger = True

    # The logger can also be enabled per operation.
    policies.append(logging_policy)
    client = PipelineClient(base_url=url, policies=policies)
    request = client.get(url)
    pipeline_response = client._pipeline.run(request, logging_enable=True)

    # [END network_trace_logging_policy]
    response = pipeline_response.http_response
    assert response.status_code == 200
def _create_pipeline(**kwargs):
    """Creates and returns a PipelineClient configured for the provided base_url and kwargs"""
    transport = kwargs.get("transport", RequestsTransport(**kwargs))
    policies = [
        kwargs.get("user_agent_policy",
                   UserAgentPolicy(_constants.USER_AGENT, **kwargs)),
        kwargs.get("headers_policy", HeadersPolicy(**kwargs)),
        kwargs.get("authentication_policy"),
        kwargs.get("retry_policy", RetryPolicy(**kwargs)),
        kwargs.get("redirect_policy", RedirectPolicy(**kwargs)),
        kwargs.get("logging_policy", NetworkTraceLoggingPolicy(**kwargs)),
        kwargs.get("proxy_policy", ProxyPolicy(**kwargs)),
    ]
    return Pipeline(policies=policies, transport=transport)
 def _configure_policies(self, **kwargs):
     return [
         RequestIdPolicy(**kwargs),
         StorageHeadersPolicy(**kwargs),
         UserAgentPolicy(sdk_moniker=SDK_MONIKER, **kwargs),
         ProxyPolicy(**kwargs),
         self._credential_policy,
         ContentDecodePolicy(response_encoding="utf-8"),
         RedirectPolicy(**kwargs),
         StorageHosts(**kwargs),
         TablesRetryPolicy(**kwargs),
         CustomHookPolicy(**kwargs),
         NetworkTraceLoggingPolicy(**kwargs),
         DistributedTracingPolicy(**kwargs),
         HttpLoggingPolicy(**kwargs),
     ]
Esempio n. 17
0
    def create_config(**kwargs):
        # type: (Mapping[str, Any]) -> Configuration
        timeout = kwargs.pop("connection_timeout", 2)
        config = Configuration(connection_timeout=timeout, **kwargs)

        # retry is the only IO policy, so its class is a kwarg to increase async code sharing
        retry_policy = kwargs.pop("retry_policy", RetryPolicy)  # type: ignore
        args = kwargs.copy(
        )  # combine kwargs and default retry settings in a Python 2-compatible way
        args.update(_ManagedIdentityBase._retry_settings)  # type: ignore
        config.retry_policy = retry_policy(**args)  # type: ignore

        # Metadata header is required by IMDS and in Cloud Shell; App Service ignores it
        config.headers_policy = HeadersPolicy(
            base_headers={"Metadata": "true"}, **kwargs)
        config.logging_policy = NetworkTraceLoggingPolicy(**kwargs)

        return config
Esempio n. 18
0
 def _policies(credential, **kwargs):
     # type: (Union[AzureKeyCredential, EventGridSharedAccessSignatureCredential], Any) -> List[Any]
     auth_policy = _get_authentication_policy(credential)
     sdk_moniker = 'eventgrid/{}'.format(VERSION)
     policies = [
         RequestIdPolicy(**kwargs),
         HeadersPolicy(**kwargs),
         UserAgentPolicy(sdk_moniker=sdk_moniker, **kwargs),
         ProxyPolicy(**kwargs),
         ContentDecodePolicy(**kwargs),
         RedirectPolicy(**kwargs),
         RetryPolicy(**kwargs), auth_policy,
         CustomHookPolicy(**kwargs),
         NetworkTraceLoggingPolicy(**kwargs),
         DistributedTracingPolicy(**kwargs),
         CloudEventDistributedTracingPolicy(),
         HttpLoggingPolicy(**kwargs)
     ]
     return policies
 def _policies(credential: Union[AzureKeyCredential, AzureSasCredential],
               **kwargs: Any) -> List[Any]:
     auth_policy = _get_authentication_policy(credential)
     sdk_moniker = 'eventgridpublisherclient/{}'.format(VERSION)
     policies = [
         RequestIdPolicy(**kwargs),
         HeadersPolicy(**kwargs),
         UserAgentPolicy(sdk_moniker=sdk_moniker, **kwargs),
         ProxyPolicy(**kwargs),
         ContentDecodePolicy(**kwargs),
         AsyncRedirectPolicy(**kwargs),
         AsyncRetryPolicy(**kwargs), auth_policy,
         CustomHookPolicy(**kwargs),
         NetworkTraceLoggingPolicy(**kwargs),
         DistributedTracingPolicy(**kwargs),
         CloudEventDistributedTracingPolicy(),
         HttpLoggingPolicy(**kwargs)
     ]
     return policies
Esempio n. 20
0
 def _create_config(**kwargs: "Any") -> Configuration:
     config = Configuration(**kwargs)
     config.logging_policy = NetworkTraceLoggingPolicy(**kwargs)
     config.retry_policy = AsyncRetryPolicy(**kwargs)
     config.proxy_policy = ProxyPolicy(**kwargs)
     return config
Esempio n. 21
0
def test_no_log(mock_http_logger):
    universal_request = HttpRequest('GET', 'http://127.0.0.1/')
    request = PipelineRequest(universal_request, PipelineContext(None))
    http_logger = NetworkTraceLoggingPolicy()
    response = PipelineResponse(request, HttpResponse(universal_request, None),
                                request.context)

    # By default, no log handler for HTTP
    http_logger.on_request(request)
    mock_http_logger.debug.assert_not_called()
    http_logger.on_response(request, response)
    mock_http_logger.debug.assert_not_called()
    mock_http_logger.reset_mock()

    # I can enable it per request
    request.context.options['logging_enable'] = True
    http_logger.on_request(request)
    assert mock_http_logger.debug.call_count >= 1
    mock_http_logger.reset_mock()
    request.context.options['logging_enable'] = True
    http_logger.on_response(request, response)
    assert mock_http_logger.debug.call_count >= 1
    mock_http_logger.reset_mock()

    # I can enable it per request (bool value should be honored)
    request.context.options['logging_enable'] = False
    http_logger.on_request(request)
    mock_http_logger.debug.assert_not_called()
    request.context.options['logging_enable'] = False
    http_logger.on_response(request, response)
    mock_http_logger.debug.assert_not_called()
    mock_http_logger.reset_mock()

    # I can enable it globally
    request.context.options = {}
    http_logger.enable_http_logger = True
    http_logger.on_request(request)
    assert mock_http_logger.debug.call_count >= 1
    http_logger.on_response(request, response)
    assert mock_http_logger.debug.call_count >= 1
    mock_http_logger.reset_mock()

    # I can enable it globally and override it locally
    http_logger.enable_http_logger = True
    request.context.options['logging_enable'] = False
    http_logger.on_request(request)
    mock_http_logger.debug.assert_not_called()
    response.context['logging_enable'] = False
    http_logger.on_response(request, response)
    mock_http_logger.debug.assert_not_called()
    mock_http_logger.reset_mock()

    # Let's make this request a failure, retried twice
    request.context.options['logging_enable'] = True
    http_logger.on_request(request)
    http_logger.on_response(request, response)

    first_count = mock_http_logger.debug.call_count
    assert first_count >= 1

    http_logger.on_request(request)
    http_logger.on_response(request, response)

    second_count = mock_http_logger.debug.call_count
    assert second_count == first_count * 2
Esempio n. 22
0
 def _create_config(**kwargs: Mapping[str, Any]) -> Configuration:
     config = Configuration(**kwargs)
     config.logging_policy = NetworkTraceLoggingPolicy(**kwargs)
     config.retry_policy = AsyncRetryPolicy(**kwargs)
     return config
Esempio n. 23
0
 def create_config(**kwargs):
     # type: (Mapping[str, Any]) -> Configuration
     config = Configuration(**kwargs)
     config.logging_policy = NetworkTraceLoggingPolicy(**kwargs)
     config.retry_policy = RetryPolicy(**kwargs)
     return config
Esempio n. 24
0
# Create a logger for the 'azure' SDK
logger = logging.getLogger("azure")
logger.setLevel(logging.DEBUG)

# Configure a console output
handler = logging.StreamHandler(stream=sys.stdout)
logger.addHandler(handler)

# Enable network trace logging. This will be logged at DEBUG level.
# By default, logging is disabled.
logging_policy_enabled = os.environ.get("ARR_LOGGING_ENABLED", None)

logging_policy = None
if logging_policy_enabled:
    logging_policy = NetworkTraceLoggingPolicy()
    logging_policy.enable_http_logger = True

arr_endpoint = os.environ.get("ARR_SERVICE_ENDPOINT", None)
if not arr_endpoint:
    raise ValueError("Set ARR_SERVICE_ENDPOINT env before run this sample.")

account_id = os.environ.get("ARR_ACCOUNT_ID", None)
if not account_id:
    raise ValueError("Set ARR_ACCOUNT_ID env before run this sample.")

account_domain = os.environ.get("ARR_ACCOUNT_DOMAIN", None)
if not account_domain:
    raise ValueError("Set ARR_ACCOUNT_DOMAIN env before run this sample.")

account_key = os.environ.get("ARR_ACCOUNT_KEY", None)