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 copy
8# of this software and associated documentation files (the ""Software""), to
9# deal in the Software without restriction, including without limitation the
10# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
11# sell copies of the Software, and to permit persons to whom the Software is
12# furnished to do so, subject to the following conditions:
13#
14# The above copyright notice and this permission notice shall be included in
15# all copies or substantial portions of the Software.
16#
17# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23# IN THE SOFTWARE.
24#
25# --------------------------------------------------------------------------
26from __future__ import annotations
27from typing import Union, Optional, Any, Generic, TypeVar, TYPE_CHECKING
28
29HTTPResponseType = TypeVar("HTTPResponseType")
30HTTPRequestType = TypeVar("HTTPRequestType")
31
32if TYPE_CHECKING:
33 from .pipeline.policies import HTTPPolicy, AsyncHTTPPolicy, SansIOHTTPPolicy
34
35 AnyPolicy = Union[
36 HTTPPolicy[HTTPRequestType, HTTPResponseType],
37 AsyncHTTPPolicy[HTTPRequestType, HTTPResponseType],
38 SansIOHTTPPolicy[HTTPRequestType, HTTPResponseType],
39 ]
40
41
42class Configuration(Generic[HTTPRequestType, HTTPResponseType]): # pylint: disable=too-many-instance-attributes
43 """Provides the home for all of the configurable policies in the pipeline.
44
45 A new Configuration object provides no default policies and does not specify in what
46 order the policies will be added to the pipeline. The SDK developer must specify each
47 of the policy defaults as required by the service and use the policies in the
48 Configuration to construct the pipeline correctly, as well as inserting any
49 unexposed/non-configurable policies.
50
51 :ivar headers_policy: Provides parameters for custom or additional headers to be sent with the request.
52 :ivar proxy_policy: Provides configuration parameters for proxy.
53 :ivar redirect_policy: Provides configuration parameters for redirects.
54 :ivar retry_policy: Provides configuration parameters for retries in the pipeline.
55 :ivar custom_hook_policy: Provides configuration parameters for a custom hook.
56 :ivar logging_policy: Provides configuration parameters for logging.
57 :ivar http_logging_policy: Provides configuration parameters for HTTP specific logging.
58 :ivar user_agent_policy: Provides configuration parameters to append custom values to the
59 User-Agent header.
60 :ivar authentication_policy: Provides configuration parameters for adding a bearer token Authorization
61 header to requests.
62 :ivar request_id_policy: Provides configuration parameters for adding a request id to requests.
63 :keyword polling_interval: Polling interval while doing LRO operations, if Retry-After is not set.
64
65 .. admonition:: Example:
66
67 .. literalinclude:: ../samples/test_example_config.py
68 :start-after: [START configuration]
69 :end-before: [END configuration]
70 :language: python
71 :caption: Creates the service configuration and adds policies.
72 """
73
74 def __init__(self, **kwargs: Any) -> None:
75 # Headers (sent with every request)
76 self.headers_policy: Optional[AnyPolicy[HTTPRequestType, HTTPResponseType]] = None
77
78 # Proxy settings (Currently used to configure transport, could be pipeline policy instead)
79 self.proxy_policy: Optional[AnyPolicy[HTTPRequestType, HTTPResponseType]] = None
80
81 # Redirect configuration
82 self.redirect_policy: Optional[AnyPolicy[HTTPRequestType, HTTPResponseType]] = None
83
84 # Retry configuration
85 self.retry_policy: Optional[AnyPolicy[HTTPRequestType, HTTPResponseType]] = None
86
87 # Custom hook configuration
88 self.custom_hook_policy: Optional[AnyPolicy[HTTPRequestType, HTTPResponseType]] = None
89
90 # Logger configuration
91 self.logging_policy: Optional[AnyPolicy[HTTPRequestType, HTTPResponseType]] = None
92
93 # Http logger configuration
94 self.http_logging_policy: Optional[AnyPolicy[HTTPRequestType, HTTPResponseType]] = None
95
96 # User Agent configuration
97 self.user_agent_policy: Optional[AnyPolicy[HTTPRequestType, HTTPResponseType]] = None
98
99 # Authentication configuration
100 self.authentication_policy: Optional[AnyPolicy[HTTPRequestType, HTTPResponseType]] = None
101
102 # Request ID policy
103 self.request_id_policy: Optional[AnyPolicy[HTTPRequestType, HTTPResponseType]] = None
104
105 # Polling interval if no retry-after in polling calls results
106 self.polling_interval: float = kwargs.get("polling_interval", 30)
107
108
109class ConnectionConfiguration:
110 """HTTP transport connection configuration settings.
111
112 Common properties that can be configured on all transports. Found in the
113 Configuration object.
114
115 :keyword float connection_timeout: A single float in seconds for the connection timeout. Defaults to 300 seconds.
116 :keyword float read_timeout: A single float in seconds for the read timeout. Defaults to 300 seconds.
117 :keyword connection_verify: SSL certificate verification. Enabled by default. Set to False to disable,
118 alternatively can be set to the path to a CA_BUNDLE file or directory with certificates of trusted CAs.
119 :paramtype connection_verify: bool or str
120 :keyword str connection_cert: Client-side certificates. You can specify a local cert to use as client side
121 certificate, as a single file (containing the private key and the certificate) or as a tuple of both files' paths.
122 :keyword int connection_data_block_size: The block size of data sent over the connection. Defaults to 4096 bytes.
123
124 .. admonition:: Example:
125
126 .. literalinclude:: ../samples/test_example_config.py
127 :start-after: [START connection_configuration]
128 :end-before: [END connection_configuration]
129 :language: python
130 :dedent: 4
131 :caption: Configuring transport connection settings.
132 """
133
134 def __init__(
135 self, # pylint: disable=unused-argument
136 *,
137 connection_timeout: float = 300,
138 read_timeout: float = 300,
139 connection_verify: Union[bool, str] = True,
140 connection_cert: Optional[str] = None,
141 connection_data_block_size: int = 4096,
142 **kwargs: Any,
143 ) -> None:
144 self.timeout = connection_timeout
145 self.read_timeout = read_timeout
146 self.verify = connection_verify
147 self.cert = connection_cert
148 self.data_block_size = connection_data_block_size