Skip to content

Commit 8d16339

Browse files
lmazuelrakshith91
authored andcommitted
Readme doc update for azure-core (Azure#6611)
* Readme doc update * More readme update * Transport update * Typo * typo thanks bryan
1 parent 0e8ec60 commit 8d16339

File tree

1 file changed

+57
-54
lines changed

1 file changed

+57
-54
lines changed

sdk/core/azure-core/README.md

Lines changed: 57 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,30 @@ from azure.core.pipeline.policies import (
2727
ProxyPolicy
2828
)
2929

30-
class FooServiceClient():
30+
class FooServiceClient:
3131

3232
@staticmethod
33-
def create_config(credential, scopes, **kwargs):
33+
def _create_config(credential, scopes, **kwargs):
3434
# Here the SDK developer would define the default
3535
# config to interact with the service
3636
config = Configuration(**kwargs)
37-
config.headers_policy = HeadersPolicy({"CustomHeader": "Value"}, **kwargs)
38-
config.user_agent_policy = UserAgentPolicy("ServiceUserAgentValue", **kwargs)
39-
config.authentication_policy = BearerTokenCredentialPolicy(credential, scopes, **kwargs)
40-
config.retry_policy = RetryPolicy(**kwargs)
41-
config.redirect_policy = RedirectPolicy(**kwargs)
42-
config.logging_policy = NetworkTraceLoggingPolicy(**kwargs)
43-
config.proxy_policy = ProxyPolicy(**kwargs)
44-
config.transport = kwargs.get('transport', RequestsTransport)
45-
46-
def __init__(self, transport=None, configuration=None, **kwargs):
47-
config = configuration or FooServiceClient.create_config(**kwargs)
48-
transport = config.get_transport(**kwargs)
37+
config.headers_policy = kwargs.get('headers_policy', HeadersPolicy({"CustomHeader": "Value"}, **kwargs))
38+
config.user_agent_policy = kwargs.get('user_agent_policy', UserAgentPolicy("ServiceUserAgentValue", **kwargs))
39+
config.authentication_policy = kwargs.get('authentication_policy', BearerTokenCredentialPolicy(credential, scopes, **kwargs))
40+
config.retry_policy = kwargs.get('retry_policy', RetryPolicy(**kwargs))
41+
config.redirect_policy = kwargs.get('redirect_policy', RedirectPolicy(**kwargs))
42+
config.logging_policy = kwargs.get('logging_policy', NetworkTraceLoggingPolicy(**kwargs))
43+
config.proxy_policy = kwargs.get('proxy_policy', ProxyPolicy(**kwargs))
44+
45+
def __init__(self, **kwargs):
46+
transport = kwargs.get('transport', RequestsTransport(**kwargs))
47+
config = FooServiceClient._create_config(**kwargs)
4948
policies = [
5049
config.user_agent_policy,
5150
config.headers_policy,
5251
config.authentication_policy,
5352
ContentDecodePolicy(),
53+
config.proxy_policy,
5454
config.redirect_policy,
5555
config.retry_policy,
5656
config.logging_policy,
@@ -92,15 +92,14 @@ client = FooServiceClient(endpoint, creds)
9292
response = client.get_foo_properties(redirects_max=0)
9393

9494
# Scenario where user wishes to fully customize the policies.
95-
# We expose the SDK-developer defined configuration to allow it to be tweaked
96-
# or entire policies replaced or patched.
97-
foo_config = FooServiceClient.create_config()
98-
99-
foo_config.retry_policy = CustomRetryPolicy()
100-
foo_config.redirect_policy.max_redirects = 5
101-
foo_config.logging_policy.enable_http_logger = True
102-
103-
client = FooServiceClient(endpoint, creds, config=config)
95+
# All configuration options are passed through kwargs
96+
client = FooServiceClient(
97+
endpoint,
98+
creds,
99+
retry_policy=CustomRetryPolicy()
100+
redirect_max=5,
101+
logging_enable=True
102+
)
104103
response = client.get_foo_properties()
105104
```
106105

@@ -110,6 +109,8 @@ The Configuration object is the home of all the configurable policies in the pip
110109
A new Configuration object provides *no default policies*.
111110
It is up to the SDK developer to specify each of the policy defaults as required by the service.
112111

112+
*Configuration should not be exposed as part of the public API of the resulting SDK.*
113+
113114
This can be seen in the above code sample as implemented in a staticmethod on the client class.
114115
The Configuration object does not specify in what order the policies will be added to the pipeline.
115116
It is up to the SDK developer to use the policies in the Configuration to construct the pipeline correctly, as well
@@ -118,7 +119,7 @@ as inserting any unexposed/non-configurable policies.
118119
transport = RequestsTransport(**kwargs)
119120

120121
# SDK developer needs to build the policy order for the pipeline.
121-
config = config or FooServiceClient.create_config(**kwargs)
122+
config = FooServiceClient._create_config(**kwargs)
122123
policies = [
123124
config.headers_policy,
124125
config.user_agent_policy,
@@ -137,7 +138,6 @@ The policies that should currently be defined on the Configuration object are as
137138
- Configuration.redirect_policy # RedirectPolicy
138139
- Configuration.logging_policy # NetworkTraceLoggingPolicy
139140
- Configuration.user_agent_policy # UserAgentPolicy
140-
- Configuration.connection # The is a ConnectionConfiguration, used to provide common transport parameters.
141141
- Configuration.proxy_policy # While this is a ProxyPolicy object, current implementation is transport configuration.
142142
- Configuration.authentication_policy # BearerTokenCredentialPolicy
143143

@@ -149,14 +149,14 @@ Various combinations of sync/async HTTP libraries as well as alternative event l
149149

150150
The transport is the last node in the pipeline, and adheres to the same basic API as any policy within the pipeline.
151151
The only currently available transport for synchronous pipelines uses the `Requests` library:
152-
```
152+
```python
153153
from azure.core.pipeline.transport import RequestsTransport
154154
synchronous_transport = RequestsTransport()
155155
```
156156

157157
For asynchronous pipelines a couple of transport options are available. Each of these transports are interchangable depending on whether the user has installed various 3rd party dependencies (i.e. aiohttp or trio), and the user
158158
should easily be able to specify their chosen transport. SDK developers should use the `aiohttp` transport as the default for asynchronous pipelines where the user has not specified an alternative.
159-
```
159+
```python
160160
from azure.foo.aio import FooServiceClient
161161
from azure.core.pipeline.transport import (
162162
# Identical implementation as the synchronous RequestsTransport wrapped in an asynchronous using the
@@ -171,28 +171,30 @@ from azure.core.pipeline.transport import (
171171
AioHttpTransport,
172172
)
173173

174-
client = FooServiceClient(endpoint, creds, transport=AioHttpTransport)
174+
client = FooServiceClient(endpoint, creds, transport=AioHttpTransport())
175175
response = await client.get_foo_properties()
176176
```
177177

178-
Some common properties can be configured on all transports, and can be set on the ConnectionConfiguration, found in the Configuration object described above. These include the following properties:
178+
Some common properties can be configured on all transports. They must be passed
179+
as kwargs arguments while building the transport instance. These include the following properties:
179180
```python
180-
class ConnectionConfiguration(object):
181-
"""Configuration of HTTP transport.
182-
183-
:param int connection_timeout: The connect and read timeout value. Defaults to 100 seconds.
184-
:param bool connection_verify: SSL certificate verification. Enabled by default. Set to False to disable,
185-
alternatively can be set to the path to a CA_BUNDLE file or directory with certificates of trusted CAs.
186-
:param str connection_cert: Client-side certificates. You can specify a local cert to use as client side
187-
certificate, as a single file (containing the private key and the certificate) or as a tuple of both files' paths.
188-
:param int connection_data_block_size: The block size of data sent over the connection. Defaults to 4096 bytes.
189-
"""
190-
191-
def __init__(self, **kwargs):
192-
self.timeout = kwargs.pop('connection_timeout', 100)
193-
self.verify = kwargs.pop('connection_verify', True)
194-
self.cert = kwargs.pop('connection_cert', None)
195-
self.data_block_size = kwargs.pop('connection_data_block_size', 4096)
181+
transport = AioHttpTransport(
182+
# The connect and read timeout value. Defaults to 100 seconds.
183+
connection_timeout=100,
184+
185+
# SSL certificate verification. Enabled by default. Set to False to disable,
186+
# alternatively can be set to the path to a CA_BUNDLE file or directory with
187+
# certificates of trusted CAs.
188+
connection_verify=True,
189+
190+
# Client-side certificates. You can specify a local cert to use as client side
191+
# certificate, as a single file (containing the private key and the certificate)
192+
# or as a # tuple of both files' paths.
193+
connection_cert=None,
194+
195+
# The block size of data sent over the connection. Defaults to 4096 bytes.
196+
connection_data_block_size=4096
197+
)
196198
```
197199

198200
### HttpRequest and HttpResponse
@@ -264,8 +266,9 @@ class HttpResponse(object):
264266
self.request = request
265267
self.internal_response = internal_response # The object returned by the HTTP library
266268
self.status_code = None
267-
self.headers = {}
269+
self.headers = CaseInsensitiveDict()
268270
self.reason = None
271+
self.content_type = None
269272

270273
def body(self):
271274
"""Return the whole body as bytes in memory."""
@@ -314,7 +317,7 @@ class PipelineResponse(object):
314317

315318
### Policies
316319

317-
The Python pipeline implementation provides two flavors of policy. These are referred to as an HttpPolicy and a SansIOPolicy.
320+
The Python pipeline implementation provides two flavors of policy. These are referred to as an HttpPolicy and a SansIOHTTPPolicy.
318321

319322
#### SansIOHTTPPolicy
320323

@@ -326,13 +329,13 @@ This is a simple abstract class, that can act before the request is done, or aft
326329

327330
A SansIOHTTPPolicy should implement one or more of the following methods:
328331
```python
329-
def on_request(self, request, **kwargs):
332+
def on_request(self, request):
330333
"""Is executed before sending the request to next policy."""
331334

332-
def on_response(self, request, response, **kwargs):
335+
def on_response(self, request, response):
333336
"""Is executed after the request comes back from the policy."""
334337

335-
def on_exception(self, request, **kwargs):
338+
def on_exception(self, request):
336339
"""Is executed if an exception is raised while executing this policy.
337340
338341
Return True if the exception has been handled and should not
@@ -362,17 +365,17 @@ class CustomPolicy(HTTPPolicy):
362365
def __init__(self):
363366
self.next = None # Will be set when pipeline is instantiated and all the policies chained.
364367

365-
def send(self, request, **kwargs):
368+
def send(self, request):
366369
"""Mutate the request."""
367370

368-
return self.next.send(request, **kwargs)
371+
return self.next.send(request)
369372

370373
class CustomAsyncPolicy(AsyncHTTPPolicy):
371374

372-
async def send(self, request, **kwargs):
375+
async def send(self, request):
373376
"""Mutate the request."""
374377

375-
return await self.next.send(request, **kwargs)
378+
return await self.next.send(request)
376379
```
377380

378381
Currently provided HTTP policies include:

0 commit comments

Comments
 (0)