Skip to main content

Microsoft Azure Core Library for Python

Project description

Azure Core shared client library for Python

Azure core provides shared exceptions and modules for Python SDK client libraries. These libraries follow the Azure SDK Design Guidelines for Python .

If you are a client library developer, please reference client library developer reference for more information.

Source code | Package (Pypi) | API reference documentation

Getting started

Typically, you will not need to install azure core; it will be installed when you install one of the client libraries using it. In case you want to install it explicitly (to implement your own client library, for example), you can find it here.

Key concepts

Azure Core Library Exceptions

AzureError

AzureError is the base exception for all errors.

class AzureError(Exception):
    def __init__(self, message, *args, **kwargs):
        self.inner_exception = kwargs.get('error')
        self.exc_type, self.exc_value, self.exc_traceback = sys.exc_info()
        self.exc_type = self.exc_type.__name__ if self.exc_type else type(self.inner_exception)
        self.exc_msg = "{}, {}: {}".format(message, self.exc_type, self.exc_value)  # type: ignore
        self.message = str(message)
        super(AzureError, self).__init__(self.message, *args)

message is any message (str) to be associated with the exception.

args are any additional args to be included with exception.

kwargs are keyword arguments to include with the exception. Use the keyword error to pass in an internal exception.

The following exceptions inherit from AzureError:

ServiceRequestError

An error occurred while attempt to make a request to the service. No request was sent.

ServiceResponseError

The request was sent, but the client failed to understand the response. The connection may have timed out. These errors can be retried for idempotent or safe operations.

HttpResponseError

A request was made, and a non-success status code was received from the service.

class HttpResponseError(AzureError):
    def __init__(self, message=None, response=None, **kwargs):
        self.reason = None
        self.response = response
        if response:
            self.reason = response.reason
        message = "Operation returned an invalid status code '{}'".format(self.reason)
        try:
            try:
                if self.error.error.code or self.error.error.message:
                    message = "({}) {}".format(
                        self.error.error.code,
                        self.error.error.message)
            except AttributeError:
                if self.error.message: #pylint: disable=no-member
                    message = self.error.message #pylint: disable=no-member
        except AttributeError:
            pass
        super(HttpResponseError, self).__init__(message=message, **kwargs)

message is the HTTP response error message (optional)

response is the HTTP response (optional).

kwargs are keyword arguments to include with the exception.

The following exceptions inherit from HttpResponseError:

DecodeError

An error raised during response deserialization.

ResourceExistsError

An error response with status code 4xx. This will not be raised directly by the Azure core pipeline.

ResourceNotFoundError

An error response, typically triggered by a 412 response (for update) or 404 (for get/post).

ClientAuthenticationError

An error response with status code 4xx. This will not be raised directly by the Azure core pipeline.

ResourceModifiedError

An error response with status code 4xx, typically 412 Conflict. This will not be raised directly by the Azure core pipeline.

ResourceNotModifiedError

An error response with status code 304. This will not be raised directly by the Azure core pipeline.

TooManyRedirectsError

An error raised when the maximum number of redirect attempts is reached. The maximum amount of redirects can be configured in the RedirectPolicy.

class TooManyRedirectsError(HttpResponseError):
    def __init__(self, history, *args, **kwargs):
        self.history = history
        message = "Reached maximum redirect attempts."
        super(TooManyRedirectsError, self).__init__(message, *args, **kwargs)

history is used to document the requests/responses that resulted in redirected requests.

args are any additional args to be included with exception.

kwargs are keyword arguments to include with the exception.

Provisional StreamConsumedError

A provisional error thrown if you try to access the stream of the provisional responses azure.core.rest.HttpResponse or azure.core.rest.AsyncHttpResponse once the response stream has been consumed.

Provisional StreamClosedError

A provisional error thrown if you try to access the stream of the provisional responses azure.core.rest.HttpResponse or azure.core.rest.AsyncHttpResponse once the response stream has been closed.

Provisional ResponseNotReadError

A provisional error thrown if you try to access the content of the provisional responses azure.core.rest.HttpResponse or azure.core.rest.AsyncHttpResponse before reading in the response's bytes first.

Configurations

When calling the methods, some properties can be configured by passing in as kwargs arguments.

Parameters Description
headers The HTTP Request headers.
request_id The request id to be added into header.
user_agent If specified, this will be added in front of the user agent string.
logging_enable Use to enable per operation. Defaults to False.
logger If specified, it will be used to log information.
response_encoding The encoding to use if known for this service (will disable auto-detection).
proxies Maps protocol or protocol and hostname to the URL of the proxy.
raw_request_hook Callback function. Will be invoked on request.
raw_response_hook Callback function. Will be invoked on response.
network_span_namer A callable to customize the span name.
tracing_attributes Attributes to set on all created spans.
permit_redirects Whether the client allows redirects. Defaults to True.
redirect_max The maximum allowed redirects. Defaults to 30.
retry_total Total number of retries to allow. Takes precedence over other counts. Default value is 10.
retry_connect 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.
retry_read 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.
retry_status How many times to retry on bad status codes. Default value is 3.
retry_backoff_factor 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.
retry_backoff_max The maximum back off time. Default value is 120 seconds (2 minutes).
retry_mode Fixed or exponential delay between attemps, default is Exponential.
timeout Timeout setting for the operation in seconds, default is 604800s (7 days).
connection_timeout A single float in seconds for the connection timeout. Defaults to 300 seconds.
read_timeout A single float in seconds for the read timeout. Defaults to 300 seconds.
connection_verify SSL certificate verification. Enabled by default. Set to False to disable, alternatively can be set to the path to a CA_BUNDLE file or directory with certificates of trusted CAs.
connection_cert Client-side certificates. You can specify a local cert to use as client side certificate, as a single file (containing the private key and the certificate) or as a tuple of both files' paths.
proxies Dictionary mapping protocol or protocol and hostname to the URL of the proxy.
cookies Dict or CookieJar object to send with the Request.
connection_data_block_size The block size of data sent over the connection. Defaults to 4096 bytes.

Async transport

The async transport is designed to be opt-in. AioHttp is one of the supported implementations of async transport. It is not installed by default. You need to install it separately.

Shared modules

MatchConditions

MatchConditions is an enum to describe match conditions.

class MatchConditions(Enum):
    Unconditionally = 1
    IfNotModified = 2
    IfModified = 3
    IfPresent = 4
    IfMissing = 5

CaseInsensitiveEnumMeta

A metaclass to support case-insensitive enums.

from enum import Enum
from six import with_metaclass

from azure.core import CaseInsensitiveEnumMeta

class MyCustomEnum(with_metaclass(CaseInsensitiveEnumMeta, str, Enum)):
    FOO = 'foo'
    BAR = 'bar'

Null Sentinel Value

A falsy sentinel object which is supposed to be used to specify attributes with no data. This gets serialized to null on the wire.

from azure.core.serialization import NULL

assert bool(NULL) is False

foo = Foo(
    attr=NULL
)

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information, see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Release History

1.16.0 (2021-07-01)

Features Added

  • Add new provisional methods send_request onto the azure.core.PipelineClient and azure.core.AsyncPipelineClient. This method takes in requests and sends them through our pipelines.
  • Add new provisional module azure.core.rest. azure.core.rest is our new public simple HTTP library in azure.core that users will use to create requests, and consume responses.
  • Add new provisional errors StreamConsumedError, StreamClosedError, and ResponseNotReadError to azure.core.exceptions. These errors are thrown if you mishandle streamed responses from the provisional azure.core.rest module

Fixed

  • Improved error message in the from_dict method of CloudEvent when a wrong schema is sent.

1.15.0 (2021-06-04)

New Features

  • Added BearerTokenCredentialPolicy.on_challenge and .authorize_request to allow subclasses to optionally handle authentication challenges

Bug Fixes

  • Retry policies don't sleep after operations time out
  • The from_dict methhod in the CloudEvent can now convert a datetime string to datetime object when microsecond exceeds the python limitation

1.14.0 (2021-05-13)

New Features

  • Added azure.core.credentials.AzureNamedKeyCredential credential #17548.
  • Added decompress parameter for stream_download method. If it is set to False, will not do decompression upon the stream. #17920

1.13.0 (2021-04-02)

Azure core requires Python 2.7 or Python 3.6+ since this release.

New Features

  • Added azure.core.utils.parse_connection_string function to parse connection strings across SDKs, with common validation and support for case insensitive keys.
  • Supported adding custom policies #16519
  • Added ~azure.core.tracing.Link that should be used while passing Links to AbstractSpan.
  • AbstractSpan constructor can now take in additional keyword only args.

Bug fixes

  • Make NetworkTraceLoggingPolicy show the auth token in plain text. #14191
  • Fixed RetryPolicy overriding default connection timeout with an extreme value #17481

1.12.0 (2021-03-08)

This version will be the last version to officially support Python 3.5, future versions will require Python 2.7 or Python 3.6+.

Features

  • Added azure.core.messaging.CloudEvent model that follows the cloud event spec.
  • Added azure.core.serialization.NULL sentinel value
  • Improve reprs for HttpRequest and HttpResponses #16972

Bug Fixes

  • Disable retry in stream downloading. (thanks to @jochen-ott-by @hoffmann for the contribution) #16723

1.11.0 (2021-02-08)

Features

  • Added CaseInsensitiveEnumMeta class for case-insensitive enums. #16316
  • Add raise_for_status method onto HttpResponse. Calling response.raise_for_status() on a response with an error code will raise an HttpResponseError. Calling it on a good response will do nothing #16399

Bug Fixes

  • Update conn.conn_kw rather than overriding it when setting block size. (thanks for @jiasli for the contribution) #16587

1.10.0 (2021-01-11)

Features

  • Added AzureSasCredential and its respective policy. #15946

1.9.0 (2020-11-09)

Features

  • Add a continuation_token attribute to the base AzureError exception, and set this value for errors raised during paged or long-running operations.

Bug Fixes

  • Set retry_interval to 1 second instead of 1000 seconds (thanks vbarbaresi for contributing) #14357

1.8.2 (2020-10-05)

Bug Fixes

  • Fixed bug to allow polling in the case of parameterized endpoints with relative polling urls #14097

1.8.1 (2020-09-08)

Bug fixes

  • SAS credential replicated "/" fix #13159

1.8.0 (2020-08-10)

Features

  • Support params as list for exploding parameters #12410

1.7.0 (2020-07-06)

Bug fixes

  • AzureKeyCredentialPolicy will now accept (and ignore) passed in kwargs #11963
  • Better error messages if passed endpoint is incorrect #12106
  • Do not JSON encore a string if content type is "text" #12137

Features

  • Added http_logging_policy property on the Configuration object, allowing users to individually set the http logging policy of the config #12218

1.6.0 (2020-06-03)

Bug fixes

  • Fixed deadlocks in AsyncBearerTokenCredentialPolicy #11543
  • Fix AttributeException in StreamDownloadGenerator #11462

Features

  • Added support for changesets as part of multipart message support #10485
  • Add AsyncLROPoller in azure.core.polling #10801
  • Add get_continuation_token/from_continuation_token/polling_method methods in pollers (sync and async) #10801
  • HttpResponse and PipelineContext objects are now pickable #10801

1.5.0 (2020-05-04)

Features

  • Support "x-ms-retry-after-ms" in response header #10743
  • link and link_from_headers now accepts attributes #10765

Bug fixes

  • Not retry if the status code is less than 400 #10778
  • "x-ms-request-id" is not considered safe header for logging #10967

1.4.0 (2020-04-06)

Features

  • Support a default error type in map_error #9773
  • Added AzureKeyCredential and its respective policy. #10509
  • Added azure.core.polling.base_polling module with a "Microsoft One API" polling implementation #10090 Also contains the async version in azure.core.polling.async_base_polling
  • Support kwarg enforce_https to disable HTTPS check on authentication #9821
  • Support additional kwargs in HttpRequest.set_multipart_mixed that will be passed into pipeline context.

1.3.0 (2020-03-09)

Bug fixes

  • Appended RequestIdPolicy to the default pipeline #9841
  • Rewind the body position in async_retry #10117

Features

  • Add raw_request_hook support in custom_hook_policy #9958
  • Add timeout support in retry_policy #10011
  • Add OdataV4 error format auto-parsing in all exceptions ('error' attribute) #9738

1.2.2 (2020-02-10)

Bug fixes

  • Fixed a bug that sends None as request_id #9545
  • Enable mypy for customers #9572
  • Handle TypeError in deep copy #9620
  • Fix text/plain content-type in decoder #9589

1.2.1 (2020-01-14)

Bug fixes

  • Fixed a regression in 1.2.0 that was incompatible with azure-keyvault-* 4.0.0 #9462

1.2.0 (2020-01-14)

Features

  • Add user_agent & sdk_moniker kwargs in UserAgentPolicy init #9355
  • Support OPTIONS HTTP verb #9322
  • Add tracing_attributes to tracing decorator #9297
  • Support auto_request_id in RequestIdPolicy #9163
  • Support fixed retry #6419
  • Support "retry-after-ms" in response header #9240

Bug fixes

  • Removed __enter__ and __exit__ from async context managers #9313

1.1.1 (2019-12-03)

Bug fixes

  • Bearer token authorization requires HTTPS
  • Rewind the body position in retry #8307

1.1.0 (2019-11-25)

Features

  • New RequestIdPolicy #8437
  • Enable logging policy in default pipeline #8053
  • Normalize transport timeout. #8000 Now we have:
    • 'connection_timeout' - a single float in seconds for the connection timeout. Default 5min
    • 'read_timeout' - a single float in seconds for the read timeout. Default 5min

Bug fixes

  • RequestHistory: deepcopy fails if request contains a stream #7732
  • Retry: retry raises error if response does not have http_response #8629
  • Client kwargs are now passed to DistributedTracingPolicy correctly #8051
  • NetworkLoggingPolicy now logs correctly all requests in case of retry #8262

1.0.0 (2019-10-29)

Features

  • Tracing: DistributedTracingPolicy now accepts kwargs network_span_namer to change network span name #7773
  • Tracing: Implementation of AbstractSpan can now use the mixin HttpSpanMixin to get HTTP span update automatically #7773
  • Tracing: AbstractSpan contract "change_context" introduced #7773
  • Introduce new policy HttpLoggingPolicy #7988

Bug fixes

  • Fix AsyncioRequestsTransport if input stream is an async generator #7743
  • Fix form-data with aiohttp transport #7749

Breaking changes

  • Tracing: AbstractSpan.set_current_span is longer supported. Use change_context instead. #7773
  • azure.core.pipeline.policies.ContentDecodePolicy.deserialize_from_text changed

1.0.0b4 (2019-10-07)

Features

  • Tracing: network span context is available with the TRACING_CONTEXT in pipeline response #7252

  • Tracing: Span contract now has kind, traceparent and is a context manager #7252

  • SansIOHTTPPolicy methods can now be coroutines #7497

  • Add multipart/mixed support #7083:

    • HttpRequest now has a "set_multipart_mixed" method to set the parts of this request
    • HttpRequest now has a "prepare_multipart_body" method to build final body.
    • HttpResponse now has a "parts" method to return an iterator of parts
    • AsyncHttpResponse now has a "parts" methods to return an async iterator of parts
    • Note that multipart/mixed is a Python 3.x only feature

Bug fixes

  • Tracing: policy cannot fail the pipeline, even in the worst condition #7252
  • Tracing: policy pass correctly status message if exception #7252
  • Tracing: incorrect span if exception raised from decorated function #7133
  • Fixed urllib3 ConnectTimeoutError being raised by Requests during a socket timeout. Now this exception is caught and wrapped as a ServiceRequestError #7542

Breaking changes

  • Tracing: azure.core.tracing.context removed

  • Tracing: azure.core.tracing.context.tracing_context.with_current_context renamed to azure.core.tracing.common.with_current_context #7252

  • Tracing: link renamed link_from_headers and link takes now a string

  • Tracing: opencensus implementation has been moved to the package azure-core-tracing-opencensus

  • Some modules and classes that were importables from several differente places have been removed:

    • azure.core.HttpResponseError is now only azure.core.exceptions.HttpResponseError
    • azure.core.Configuration is now only azure.core.configuration.Configuration
    • azure.core.HttpRequest is now only azure.core.pipeline.transport.HttpRequest
    • azure.core.version module has been removed. Use azure.core.__version__ to get version number.
    • azure.core.pipeline_client has been removed. Import from azure.core instead.
    • azure.core.pipeline_client_async has been removed. Import from azure.core instead.
    • azure.core.pipeline.base has been removed. Import from azure.core.pipeline instead.
    • azure.core.pipeline.base_async has been removed. Import from azure.core.pipeline instead.
    • azure.core.pipeline.policies.base has been removed. Import from azure.core.pipeline.policies instead.
    • azure.core.pipeline.policies.base_async has been removed. Import from azure.core.pipeline.policies instead.
    • azure.core.pipeline.policies.authentication has been removed. Import from azure.core.pipeline.policies instead.
    • azure.core.pipeline.policies.authentication_async has been removed. Import from azure.core.pipeline.policies instead.
    • azure.core.pipeline.policies.custom_hook has been removed. Import from azure.core.pipeline.policies instead.
    • azure.core.pipeline.policies.redirect has been removed. Import from azure.core.pipeline.policies instead.
    • azure.core.pipeline.policies.redirect_async has been removed. Import from azure.core.pipeline.policies instead.
    • azure.core.pipeline.policies.retry has been removed. Import from azure.core.pipeline.policies instead.
    • azure.core.pipeline.policies.retry_async has been removed. Import from azure.core.pipeline.policies instead.
    • azure.core.pipeline.policies.distributed_tracing has been removed. Import from azure.core.pipeline.policies instead.
    • azure.core.pipeline.policies.universal has been removed. Import from azure.core.pipeline.policies instead.
    • azure.core.tracing.abstract_span has been removed. Import from azure.core.tracing instead.
    • azure.core.pipeline.transport.base has been removed. Import from azure.core.pipeline.transport instead.
    • azure.core.pipeline.transport.base_async has been removed. Import from azure.core.pipeline.transport instead.
    • azure.core.pipeline.transport.requests_basic has been removed. Import from azure.core.pipeline.transport instead.
    • azure.core.pipeline.transport.requests_asyncio has been removed. Import from azure.core.pipeline.transport instead.
    • azure.core.pipeline.transport.requests_trio has been removed. Import from azure.core.pipeline.transport instead.
    • azure.core.pipeline.transport.aiohttp has been removed. Import from azure.core.pipeline.transport instead.
    • azure.core.polling.poller has been removed. Import from azure.core.polling instead.
    • azure.core.polling.async_poller has been removed. Import from azure.core.polling instead.

1.0.0b3 (2019-09-09)

Bug fixes

  • Fix aiohttp auto-headers #6992
  • Add tracing to policies module init #6951

1.0.0b2 (2019-08-05)

Breaking changes

  • Transport classes don't take config parameter anymore (use kwargs instead) #6372
  • azure.core.paging has been completely refactored #6420
  • HttpResponse.content_type attribute is now a string (was a list) #6490
  • For StreamDownloadGenerator subclasses, response is now an HttpResponse, and not a transport response like aiohttp.ClientResponse or requests.Response. The transport response is available in internal_response attribute #6490

Bug fixes

  • aiohttp is not required to import async pipelines classes #6496
  • AsyncioRequestsTransport.sleep is now a coroutine as expected #6490
  • RequestsTransport is not tight to ProxyPolicy implementation details anymore #6372
  • AiohttpTransport does not raise on unexpected kwargs #6355

Features

  • New paging base classes that support continuation_token and by_page() #6420
  • Proxy support for AiohttpTransport #6372

1.0.0b1 (2019-06-26)

  • Preview 1 release

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

azure-core-1.16.0.zip (317.9 kB view details)

Uploaded Source

Built Distribution

azure_core-1.16.0-py2.py3-none-any.whl (163.3 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file azure-core-1.16.0.zip.

File metadata

  • Download URL: azure-core-1.16.0.zip
  • Upload date:
  • Size: 317.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

File hashes

Hashes for azure-core-1.16.0.zip
Algorithm Hash digest
SHA256 b1c7d2e01846074f258c8b2e592239aef836a2b1c27d8d0e8491a2c7e2906ef4
MD5 e5ab77dd6f07dfbba52a863c4f5789ca
BLAKE2b-256 9b6fc4d598f119f69da1ebd9e0bfdada0b6a2e01e4b5a987a55ce6921dc53629

See more details on using hashes here.

File details

Details for the file azure_core-1.16.0-py2.py3-none-any.whl.

File metadata

  • Download URL: azure_core-1.16.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 163.3 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

File hashes

Hashes for azure_core-1.16.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 83ef981ca4ad167c1df7b1ec5c4bda74999fcd21cacc048e83cf8ce51c61d5c2
MD5 f6233436e02469bf7d5302d42497c7f1
BLAKE2b-256 320a837c3071ed24adba0e6733991b967336069bd49f1abf018ceae38a8ea759

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page