Skip to content

Commit

Permalink
[Corehttp] Add code snippets to docstrings (#33207)
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Van Eck <paulvaneck@microsoft.com>
  • Loading branch information
pvaneck authored Feb 23, 2024
1 parent 757f877 commit 143d2ff
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 31 deletions.
9 changes: 9 additions & 0 deletions sdk/core/corehttp/corehttp/runtime/_pipeline_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ class PipelineClient(PipelineClientBase, Generic[HTTPRequestType, HTTPResponseTy
:ivar pipeline: The Pipeline object associated with the client.
:vartype pipeline: ~corehttp.runtime.pipeline.Pipeline or None
.. admonition:: Example:
.. literalinclude:: ../samples/sample_pipeline_client.py
:start-after: [START build_pipeline_client]
:end-before: [END build_pipeline_client]
:language: python
:dedent: 4
:caption: Builds the pipeline client.
"""

def __init__(
Expand Down
9 changes: 9 additions & 0 deletions sdk/core/corehttp/corehttp/runtime/_pipeline_client_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ class AsyncPipelineClient(
:ivar pipeline: The Pipeline object associated with the client.
:vartype pipeline: ~corehttp.runtime.pipeline.Pipeline or None
.. admonition:: Example:
.. literalinclude:: ../samples/sample_async_pipeline_client.py
:start-after: [START build_async_pipeline_client]
:end-before: [END build_async_pipeline_client]
:language: python
:dedent: 4
:caption: Builds the async pipeline client.
"""

def __init__(
Expand Down
25 changes: 19 additions & 6 deletions sdk/core/corehttp/samples/sample_async_pipeline_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,43 @@
SansIOHTTPPolicy,
HeadersPolicy,
UserAgentPolicy,
ContentDecodePolicy,
AsyncRetryPolicy,
NetworkTraceLoggingPolicy,
)


async def run():
async def sample_pipeline_client():
# [START build_async_pipeline_client]
from corehttp.runtime import AsyncPipelineClient
from corehttp.rest import HttpRequest, AsyncHttpResponse
from corehttp.runtime.policies import (
AsyncHTTPPolicy,
SansIOHTTPPolicy,
HeadersPolicy,
UserAgentPolicy,
AsyncRetryPolicy,
)

policies: Iterable[Union[AsyncHTTPPolicy, SansIOHTTPPolicy]] = [
HeadersPolicy(),
UserAgentPolicy("myuseragent"),
ContentDecodePolicy(),
AsyncRetryPolicy(),
NetworkTraceLoggingPolicy(),
]
client: AsyncPipelineClient[HttpRequest, AsyncHttpResponse] = AsyncPipelineClient(
"https://bing.com", policies=policies
)
request = HttpRequest("GET", "https://bing.com")
async with client:
response = await client.send_request(request)
# [END build_async_pipeline_client]

pipeline_response = await client.pipeline.run(request)
print(response)
print(pipeline_response)


async def main():
await sample_pipeline_client()


if __name__ == "__main__":
asyncio.run(run())
asyncio.run(main())
56 changes: 31 additions & 25 deletions sdk/core/corehttp/samples/sample_pipeline_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,34 @@
"""

from typing import Iterable, Union
from corehttp.runtime import PipelineClient
from corehttp.rest import HttpRequest, HttpResponse
from corehttp.runtime.policies import (
HTTPPolicy,
SansIOHTTPPolicy,
HeadersPolicy,
UserAgentPolicy,
ContentDecodePolicy,
RetryPolicy,
NetworkTraceLoggingPolicy,
)

policies: Iterable[Union[HTTPPolicy, SansIOHTTPPolicy]] = [
HeadersPolicy(),
UserAgentPolicy("myuseragent"),
ContentDecodePolicy(),
RetryPolicy(),
NetworkTraceLoggingPolicy(),
]

client: PipelineClient[HttpRequest, HttpResponse] = PipelineClient("https://bing.com", policies=policies)
request = HttpRequest("GET", "https://bing.com")
response = client.send_request(request)
pipeline_response = client.pipeline.run(request)
print(response)


def sample_pipeline_client():
# [START build_pipeline_client]
from corehttp.runtime import PipelineClient
from corehttp.rest import HttpRequest, HttpResponse
from corehttp.runtime.policies import (
HTTPPolicy,
SansIOHTTPPolicy,
HeadersPolicy,
UserAgentPolicy,
RetryPolicy,
)

policies: Iterable[Union[HTTPPolicy, SansIOHTTPPolicy]] = [
HeadersPolicy(),
UserAgentPolicy("myuseragent"),
RetryPolicy(),
]

client: PipelineClient[HttpRequest, HttpResponse] = PipelineClient("https://bing.com", policies=policies)
request = HttpRequest("GET", "https://bing.com")
response = client.send_request(request)
# [END build_pipeline_client]

pipeline_response = client.pipeline.run(request)
print(response)


if __name__ == "__main__":
sample_pipeline_client()

0 comments on commit 143d2ff

Please sign in to comment.