def test_example_retry_policy(): url = "https://bing.com" config = Configuration() config.user_agent_policy = UserAgentPolicy("myusergant") config.redirect_policy = RedirectPolicy() # [START retry_policy] from azure.core.pipeline.policies import RetryPolicy config.retry_policy = RetryPolicy() # Total number of retries to allow. Takes precedence over other counts. # Default value is 10. config.retry_policy.total_retries = 5 # How many connection-related errors to retry on. # These are errors raised before the request is sent to the remote server, # which we assume has not triggered the server to process the request. Default value is 3 config.retry_policy.connect_retries = 2 # How many times to retry on read errors. # These errors are raised after the request was sent to the server, so the # request may have side-effects. Default value is 3. config.retry_policy.read_retries = 4 # How many times to retry on bad status codes. Default value is 3. config.retry_policy.status_retries = 3 # A backoff factor to apply between attempts after the second try # (most errors are resolved immediately by a second try without a delay). # Retry policy will sleep for: # {backoff factor} * (2 ** ({number of total retries} - 1)) # seconds. If the backoff_factor is 0.1, then the retry will sleep # for [0.0s, 0.2s, 0.4s, ...] between retries. # The default value is 0.8. config.retry_policy.backoff_factor = 0.5 # The maximum back off time. Default value is 120 seconds (2 minutes). config.retry_policy.backoff_max = 120 # Alternatively you can disable redirects entirely config.retry_policy = RetryPolicy.no_retries() # All of these settings can also be configured per operation. client = PipelineClient(base_url=url, config=config) request = client.get(url) pipeline_response = client._pipeline.run(request, retry_total=10, retry_connect=1, retry_read=1, retry_status=5, retry_backoff_factor=0.5, retry_backoff_max=120, retry_on_methods=['GET']) # [END retry_policy] response = pipeline_response.http_response assert response.status_code == 200
def test_example_redirect_policy(): url = "https://bing.com" # [START redirect_policy] from azure.core.pipeline.policies import RedirectPolicy redirect_policy = RedirectPolicy() # Client allows redirects. Defaults to True. redirect_policy.allow = True # The maximum allowed redirects. The default value is 30 redirect_policy.max_redirects = 10 # Alternatively you can disable redirects entirely redirect_policy = RedirectPolicy.no_redirects() # It can also be overridden per operation. client = PipelineClient(base_url=url, policies=[redirect_policy]) request = client.get(url) pipeline_response = client._pipeline.run(request, permit_redirects=True, redirect_max=5) # [END redirect_policy] response = pipeline_response.http_response assert response.status_code == 200
def test_example_user_agent_policy(): url = "https://bing.com" redirect_policy = RedirectPolicy() # [START user_agent_policy] from azure.core.pipeline.policies import UserAgentPolicy user_agent_policy = UserAgentPolicy() # The user-agent policy allows you to append a custom value to the header. user_agent_policy.add_user_agent("CustomValue") # You can also pass in a custom value per operation to append to the end of the user-agent. # This can be used together with the policy configuration to append multiple values. policies = [ redirect_policy, user_agent_policy, ] client = PipelineClient(base_url=url, policies=policies) request = client.get(url) pipeline_response = client._pipeline.run(request, user_agent="AnotherValue") # [END user_agent_policy] response = pipeline_response.http_response assert response.status_code == 200
def test_request_hook_policy_in_request(): def test_callback(response): raise ValueError() url = "https://bing.com" custom_hook_policy = CustomHookPolicy() policies = [UserAgentPolicy("myuseragent"), custom_hook_policy] client = PipelineClient(base_url=url, policies=policies) request = client.get(url) with pytest.raises(ValueError): client._pipeline.run(request, raw_request_hook=test_callback)
def test_example_no_retries(): url = "https://bing.com" retry_policy = RetryPolicy.no_retries() client = PipelineClient(base_url=url, policies=[retry_policy]) request = client.get(url) pipeline_response = client._pipeline.run(request) response = pipeline_response.http_response # bing returns 301 if not retried assert response.status_code == 301
def test_compress_plain_no_header(): # expect plain text account_name = "coretests" account_url = "https://{}.blob.core.windows.net".format(account_name) url = "https://{}.blob.core.windows.net/tests/test.txt".format(account_name) client = PipelineClient(account_url) request = client.get(url) pipeline_response = client._pipeline.run(request, stream=True) response = pipeline_response.http_response data = response.stream_download(client._pipeline, decompress=False) content = b"".join(list(data)) decoded = content.decode('utf-8') assert decoded == "test"
def test_example_no_redirects(): url = "https://bing.com" config = Configuration() config.redirect_policy = RedirectPolicy.no_redirects() client = PipelineClient(base_url=url, config=config) request = client.get(url) pipeline_response = client._pipeline.run(request) response = pipeline_response.http_response # bing returns 301 if not redirected assert response.status_code == 301
def test_request_hook_policy_in_request(): def test_callback(response): raise ValueError() transport = mock.MagicMock(spec=HttpTransport) url = "http://localhost" custom_hook_policy = CustomHookPolicy() policies = [ UserAgentPolicy("myuseragent"), custom_hook_policy ] client = PipelineClient(base_url=url, policies=policies, transport=transport) request = client.get(url) with pytest.raises(ValueError): client._pipeline.run(request, raw_request_hook=test_callback)
def test_decompress_plain_header(): # expect error import requests account_name = "coretests" account_url = "https://{}.blob.core.windows.net".format(account_name) url = "https://{}.blob.core.windows.net/tests/test_with_header.txt".format(account_name) client = PipelineClient(account_url) request = client.get(url) pipeline_response = client._pipeline.run(request, stream=True) response = pipeline_response.http_response data = response.stream_download(client._pipeline, decompress=True) try: content = b"".join(list(data)) assert False except (requests.exceptions.ContentDecodingError, DecodeError): pass
def test_decompress_compressed_no_header(): # expect compressed text account_name = "coretests" account_url = "https://{}.blob.core.windows.net".format(account_name) url = "https://{}.blob.core.windows.net/tests/test.tar.gz".format(account_name) client = PipelineClient(account_url) request = client.get(url) pipeline_response = client._pipeline.run(request, stream=True) response = pipeline_response.http_response data = response.stream_download(client._pipeline, decompress=True) content = b"".join(list(data)) try: decoded = content.decode('utf-8') assert False except UnicodeDecodeError: pass
class PipelineClientGetTest(PerfStressTest): pipeline_client: PipelineClient = None async_pipeline_client: AsyncPipelineClient = None _first_run = True def __init__(self, arguments): super().__init__(arguments) self.pipeline_client = PipelineClient(base_url=self.args.url, **self._client_kwargs) self.async_pipeline_client = AsyncPipelineClient(base_url=self.args.url, **self._client_kwargs) def run_sync(self): if self._first_run: for _ in range(self.args.first_run_extra_requests): self._send_request_sync() self._first_run = False self._send_request_sync() async def run_async(self): if self._first_run: for _ in range(self.args.first_run_extra_requests): await self._send_request_async() self._first_run = False await self._send_request_async() def _send_request_sync(self): request = self.pipeline_client.get(self.args.url) response: PipelineResponse = self.pipeline_client._pipeline.run(request) # Consume response body http_response: HttpResponse = response.http_response data = http_response.body() async def _send_request_async(self): request = self.async_pipeline_client.get(self.args.url) response: PipelineResponse = await self.async_pipeline_client._pipeline.run(request) # Consume response body http_response: HttpResponse = response.http_response data = http_response.body() async def close(self): await self.async_pipeline_client.close() await super().close() @staticmethod def add_arguments(parser): parser.add_argument("--first-run-extra-requests", type=int, default=0, help='Extra requests to send on first run. ' + 'Simulates SDKs which require extra requests (like authentication) on first API call.') parser.add_argument("-u", "--url", required=True)
def test_example_pipeline_client(): url = "https://bing.com" # [START build_pipeline_client] from azure.core import PipelineClient from azure.core.pipeline.policies import RedirectPolicy, UserAgentPolicy # example configuration with some policies policies = [UserAgentPolicy("myuseragent"), RedirectPolicy()] client = PipelineClient(base_url=url, policies=policies) request = client.get("https://bing.com") pipeline_response = client._pipeline.run(request) # [END build_pipeline_client] response = pipeline_response.http_response assert isinstance(response.status_code, int)
def test_cloud_shell_live(cloud_shell): credential = ManagedIdentityCredential() token = credential.get_token("https://vault.azure.net") # Validate the token by sending a request to the Key Vault. The request is manual because azure-keyvault-secrets # can't authenticate in Cloud Shell; the MSI endpoint there doesn't support AADv2 scopes. policies = [ ContentDecodePolicy(), RedirectPolicy(), RetryPolicy(), HttpLoggingPolicy() ] client = PipelineClient(cloud_shell["vault_url"], policies=policies) list_secrets = client.get( "secrets", headers={"Authorization": "Bearer " + token.token}, params={"api-version": "7.0"}) with client: client._pipeline.run(list_secrets)
def test_example_pipeline_client(): url = "https://bing.com" # [START build_pipeline_client] from azure.core import PipelineClient from azure.core.configuration import Configuration from azure.core.pipeline.policies import RedirectPolicy, UserAgentPolicy # example configuration with some policies config = Configuration() config.user_agent_policy = UserAgentPolicy("myuseragent") config.redirect_policy = RedirectPolicy() client = PipelineClient(base_url=url, config=config) request = client.get("https://bing.com") pipeline_response = client._pipeline.run(request) # [END build_pipeline_client] response = pipeline_response.http_response assert response.status_code == 200
def test_example_headers_policy(): url = "https://bing.com" policies = [UserAgentPolicy("myuseragent"), RedirectPolicy()] # [START headers_policy] from azure.core.pipeline.policies import HeadersPolicy headers_policy = HeadersPolicy() headers_policy.add_header('CustomValue', 'Foo') # Or headers can be added per operation. These headers will supplement existing headers # or those defined in the config headers policy. They will also overwrite existing # identical headers. policies.append(headers_policy) client = PipelineClient(base_url=url, policies=policies) request = client.get(url) pipeline_response = client._pipeline.run(request, headers={'CustomValue': 'Bar'}) # [END headers_policy] response = pipeline_response.http_response assert response.status_code == 200
def test_example_request_id_policy(): url = "https://bing.com" policies = [UserAgentPolicy("myuseragent"), RedirectPolicy()] # [START request_id_policy] from azure.core.pipeline.policies import HeadersPolicy request_id_policy = RequestIdPolicy() request_id_policy.set_request_id('azconfig-test') # Or headers can be added per operation. These headers will supplement existing headers # or those defined in the config headers policy. They will also overwrite existing # identical headers. policies.append(request_id_policy) client = PipelineClient(base_url=url, policies=policies) request = client.get(url) pipeline_response = client._pipeline.run(request, request_id="azconfig-test") # [END request_id_policy] response = pipeline_response.http_response assert isinstance(response.status_code, int)
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