azure.core.pipeline.transport

class azure.core.pipeline.transport.HttpTransport[source]

An http sender ABC.

abstract close()[source]

Close the session if it is not externally owned.

abstract open()[source]

Assign new session if one does not already exist.

abstract send(request: HTTPRequestType, **kwargs: Any) → HTTPResponseType[source]

Send the request using this HTTP sender.

Parameters

request (HTTPRequest) – The pipeline request object

Returns

The pipeline response object.

Return type

HttpResponse

sleep(duration)[source]
class azure.core.pipeline.transport.HttpRequest(method: str, url: str, headers: Optional[Mapping[str, str]] = None, files: Optional[Any] = None, data: Optional[Any] = None)[source]

Represents a HTTP request.

URL can be given without query parameters, to be added later using “format_parameters”.

Parameters
  • method (str) – HTTP method (GET, HEAD, etc.)

  • url (str) – At least complete scheme/host/path

  • headers (dict[str,str]) – HTTP headers

  • files – Files list.

  • data (bytes or str.) – Body to be sent.

format_parameters(params: Dict[str, str])None[source]

Format parameters into a valid query string. It’s assumed all parameters have already been quoted as valid URL strings.

Parameters

params (dict) – A dictionary of parameters.

prepare_multipart_body(content_index: int = 0)int[source]

Will prepare the body of this request according to the multipart information.

This call assumes the on_request policies have been applied already in their correct context (sync/async)

Does nothing if “set_multipart_mixed” was never called.

Parameters

content_index (int) – The current index of parts within the batch message.

Returns

The updated index after all parts in this request have been added.

Return type

int

serialize()bytes[source]

Serialize this request using application/http spec.

Return type

bytes

set_bytes_body(data)[source]

Set generic bytes as the body of the request.

Will set content-length.

Parameters

data (bytes) – The request field data.

set_formdata_body(data=None)[source]

Set form-encoded data as the body of the request.

Parameters

data (dict) – The request field data.

set_json_body(data)[source]

Set a JSON-friendly object as the body of the request.

Parameters

data – A JSON serializable object

set_multipart_mixed(*requests: azure.core.pipeline.transport._base.HttpRequest, **kwargs: Any)None[source]

Set the part of a multipart/mixed.

Only supported args for now are HttpRequest objects.

boundary is optional, and one will be generated if you don’t provide one. Note that no verification are made on the boundary, this is considered advanced enough so you know how to respect RFC1341 7.2.1 and provide a correct boundary.

Any additional kwargs will be passed into the pipeline context for per-request policy configuration.

Keyword Arguments
  • policies (list[SansIOHTTPPolicy]) – SansIOPolicy to apply at preparation time

  • boundary (str) – Optional boundary

Parameters

requests – HttpRequests object

set_streamed_data_body(data)[source]

Set a streamable data body.

Parameters

data (stream or generator or asyncgenerator) – The request field data.

set_text_body(data)[source]

Set a text as body of the request.

Parameters

data (str) – A text to send as body.

set_xml_body(data)[source]

Set an XML element tree as the body of the request.

Parameters

data (XML node) – The request field data.

property body

Alias to data.

Return type

bytes

property query

The query parameters of the request as a dict.

Return type

dict[str, str]

class azure.core.pipeline.transport.HttpResponse(request: azure.core.pipeline.transport._base.HttpRequest, internal_response: Any, block_size: Optional[int] = None)[source]
body()bytes

Return the whole body as bytes in memory.

parts() → Iterator[azure.core.pipeline.transport._base.HttpResponse][source]

Assuming the content-type is multipart/mixed, will return the parts as an iterator.

Return type

iterator[HttpResponse]

Raises

ValueError – If the content is not multipart/mixed

stream_download(pipeline: PipelineType) → Iterator[bytes][source]

Generator for streaming request body data.

Should be implemented by sub-classes if streaming download is supported.

Return type

iterator[bytes]

text(encoding: Optional[str] = None)str

Return the whole body as a string.

Parameters

encoding (str) – The encoding to apply. If None, use “utf-8” with BOM parsing (utf-8-sig). Implementation can be smarter if they want (using headers or chardet).

class azure.core.pipeline.transport.RequestsTransport(**kwargs: Any)[source]

Implements a basic requests HTTP sender.

Since requests team recommends to use one session per requests, you should not consider this class as thread-safe, since it will use one Session per instance.

In this simple implementation: - You provide the configured session if you want to, or a basic session is created. - All kwargs received by “send” are sent to session.request directly

Keyword Arguments
  • session (requests.Session) – Request session to use instead of the default one.

  • session_owner (bool) – Decide if the session provided by user is owned by this transport. Default to True.

  • use_env_settings (bool) – Uses proxy settings from environment. Defaults to True.

Example:

Synchronous transport with Requests.
from azure.core.pipeline.transport import RequestsTransport

with Pipeline(transport=RequestsTransport(), policies=policies) as pipeline:
    response = pipeline.run(request)
close()[source]

Close the session if it is not externally owned.

open()[source]

Assign new session if one does not already exist.

send(request: azure.core.pipeline.transport._base.HttpRequest, **kwargs: Any) → azure.core.pipeline.transport._base.HttpResponse[source]

Send request object according to configuration.

Parameters

request (HttpRequest) – The request object to be sent.

Returns

An HTTPResponse object.

Return type

HttpResponse

Keyword Arguments
  • session (requests.Session) – will override the driver session and use yours. Should NOT be done unless really required. Anything else is sent straight to requests.

  • proxies (dict) – will define the proxy to use. Proxy is a dict (protocol, url)

sleep(duration)
class azure.core.pipeline.transport.RequestsTransportResponse(request, requests_response, block_size=None)[source]

Streaming of data from the response.

body()

Return the whole body as bytes in memory.

parts() → Iterator[azure.core.pipeline.transport._base.HttpResponse]

Assuming the content-type is multipart/mixed, will return the parts as an iterator.

Return type

iterator[HttpResponse]

Raises

ValueError – If the content is not multipart/mixed

stream_download(pipeline: PipelineType) → Iterator[bytes][source]

Generator for streaming request body data.

text(encoding: Optional[str] = None)str

Return the whole body as a string.

If encoding is not provided, mostly rely on requests auto-detection, except for BOM, that requests ignores. If we see a UTF8 BOM, we assumes UTF8 unlike requests.

Parameters

encoding (str) – The encoding to apply.

class azure.core.pipeline.transport.AsyncHttpTransport[source]

An http sender ABC.

abstract async close()[source]

Close the session if it is not externally owned.

abstract async open()[source]

Assign new session if one does not already exist.

abstract async send(request, **kwargs)[source]

Send the request using this HTTP sender.

async sleep(duration)[source]
class azure.core.pipeline.transport.AsyncHttpResponse(request: azure.core.pipeline.transport._base.HttpRequest, internal_response: Any, block_size: Optional[int] = None)[source]

An AsyncHttpResponse ABC.

Allows for the asynchronous streaming of data from the response.

body()bytes

Return the whole body as bytes in memory.

parts()collections.abc.AsyncIterator[source]

Assuming the content-type is multipart/mixed, will return the parts as an async iterator.

Return type

AsyncIterator

Raises

ValueError – If the content is not multipart/mixed

stream_download(pipeline) → AsyncIterator[bytes][source]

Generator for streaming response body data.

Should be implemented by sub-classes if streaming download is supported. Will return an asynchronous generator.

Parameters

pipeline (azure.core.pipeline) – The pipeline object

text(encoding: Optional[str] = None)str

Return the whole body as a string.

Parameters

encoding (str) – The encoding to apply. If None, use “utf-8” with BOM parsing (utf-8-sig). Implementation can be smarter if they want (using headers or chardet).

class azure.core.pipeline.transport.AsyncioRequestsTransport(**kwargs: Any)[source]

Identical implementation as the synchronous RequestsTransport wrapped in a class with asynchronous methods. Uses the built-in asyncio event loop.

Example:

Asynchronous transport with asyncio.
from azure.core.pipeline.transport import AsyncioRequestsTransport

async with AsyncPipeline(AsyncioRequestsTransport(), policies=policies) as pipeline:
    response = await pipeline.run(request)
close()

Close the session if it is not externally owned.

open()

Assign new session if one does not already exist.

async send(request: azure.core.pipeline.transport._base.HttpRequest, **kwargs: Any) → azure.core.pipeline.transport._base_async.AsyncHttpResponse[source]

Send the request using this HTTP sender.

Parameters

request (HttpRequest) – The HttpRequest

Returns

The AsyncHttpResponse

Return type

AsyncHttpResponse

Keyword Arguments
  • session (requests.Session) – will override the driver session and use yours. Should NOT be done unless really required. Anything else is sent straight to requests.

  • proxies (dict) – will define the proxy to use. Proxy is a dict (protocol, url)

async sleep(duration)[source]
class azure.core.pipeline.transport.AsyncioRequestsTransportResponse(request, requests_response, block_size=None)[source]

Asynchronous streaming of data from the response.

body()

Return the whole body as bytes in memory.

parts()collections.abc.AsyncIterator

Assuming the content-type is multipart/mixed, will return the parts as an async iterator.

Return type

AsyncIterator

Raises

ValueError – If the content is not multipart/mixed

stream_download(pipeline) → AsyncIterator[bytes][source]

Generator for streaming request body data.

text(encoding: Optional[str] = None)str

Return the whole body as a string.

If encoding is not provided, mostly rely on requests auto-detection, except for BOM, that requests ignores. If we see a UTF8 BOM, we assumes UTF8 unlike requests.

Parameters

encoding (str) – The encoding to apply.

class azure.core.pipeline.transport.TrioRequestsTransport(**kwargs: Any)[source]

Identical implementation as the synchronous RequestsTransport wrapped in a class with asynchronous methods. Uses the third party trio event loop.

Example:

Asynchronous transport with trio.
    from azure.core.pipeline.transport import TrioRequestsTransport

    async with AsyncPipeline(TrioRequestsTransport(), policies=policies) as pipeline:
        return await pipeline.run(request)
close()

Close the session if it is not externally owned.

open()

Assign new session if one does not already exist.

async send(request: azure.core.pipeline.transport._base.HttpRequest, **kwargs: Any) → azure.core.pipeline.transport._base_async.AsyncHttpResponse[source]

Send the request using this HTTP sender.

Parameters

request (HttpRequest) – The HttpRequest

Returns

The AsyncHttpResponse

Return type

AsyncHttpResponse

Keyword Arguments
  • session (requests.Session) – will override the driver session and use yours. Should NOT be done unless really required. Anything else is sent straight to requests.

  • proxies (dict) – will define the proxy to use. Proxy is a dict (protocol, url)

async sleep(duration)[source]
class azure.core.pipeline.transport.TrioRequestsTransportResponse(request, requests_response, block_size=None)[source]

Asynchronous streaming of data from the response.

body()

Return the whole body as bytes in memory.

parts()collections.abc.AsyncIterator

Assuming the content-type is multipart/mixed, will return the parts as an async iterator.

Return type

AsyncIterator

Raises

ValueError – If the content is not multipart/mixed

stream_download(pipeline) → AsyncIterator[bytes][source]

Generator for streaming response data.

text(encoding: Optional[str] = None)str

Return the whole body as a string.

If encoding is not provided, mostly rely on requests auto-detection, except for BOM, that requests ignores. If we see a UTF8 BOM, we assumes UTF8 unlike requests.

Parameters

encoding (str) – The encoding to apply.

class azure.core.pipeline.transport.AioHttpTransport(*, session=None, loop=None, session_owner=True, **kwargs)[source]

AioHttp HTTP sender implementation.

Fully asynchronous implementation using the aiohttp library.

Parameters
  • session – The client session.

  • loop – The event loop.

  • session_owner (bool) – Session owner. Defaults True.

Keyword Arguments

use_env_settings (bool) – Uses proxy settings from environment. Defaults to True.

Example:

Asynchronous transport with aiohttp.
from azure.core.pipeline.transport import AioHttpTransport

async with AsyncPipeline(AioHttpTransport(), policies=policies) as pipeline:
    response = await pipeline.run(request)
async close()[source]

Closes the connection.

async open()[source]

Opens the connection.

async send(request: azure.core.pipeline.transport._base.HttpRequest, **config: Any) → Optional[azure.core.pipeline.transport._base_async.AsyncHttpResponse][source]

Send the request using this HTTP sender.

Will pre-load the body into memory to be available with a sync method. Pass stream=True to avoid this behavior.

Parameters
  • request (HttpRequest) – The HttpRequest object

  • config – Any keyword arguments

Returns

The AsyncHttpResponse

Return type

AsyncHttpResponse

Keyword Arguments
  • stream (bool) – Defaults to False.

  • proxies (dict) – dict of proxy to used based on protocol. Proxy is a dict (protocol, url)

  • proxy (str) – will define the proxy to use all the time

async sleep(duration)
class azure.core.pipeline.transport.AioHttpTransportResponse(request: azure.core.pipeline.transport._base.HttpRequest, aiohttp_response: aiohttp.client_reqrep.ClientResponse, block_size=None)[source]

Methods for accessing response body data.

Parameters
  • request (HttpRequest) – The HttpRequest object

  • aiohttp_response (aiohttp.ClientResponse object) – Returned from ClientSession.request().

  • block_size (int) – block size of data sent over connection.

body()bytes[source]

Return the whole body as bytes in memory.

async load_body()None[source]

Load in memory the body, so it could be accessible from sync methods.

parts()collections.abc.AsyncIterator

Assuming the content-type is multipart/mixed, will return the parts as an async iterator.

Return type

AsyncIterator

Raises

ValueError – If the content is not multipart/mixed

stream_download(pipeline) → AsyncIterator[bytes][source]

Generator for streaming response body data.

Parameters

pipeline (azure.core.pipeline) – The pipeline object

text(encoding: Optional[str] = None)str[source]

Return the whole body as a string.

If encoding is not provided, rely on aiohttp auto-detection.

Parameters

encoding (str) – The encoding to apply.