Skip to content

Latest commit

 

History

History
101 lines (74 loc) · 7.15 KB

initializing.md

File metadata and controls

101 lines (74 loc) · 7.15 KB

Initializing Your Python Client

The first step to using your generated client in code is to import and initialize your client. Our SDKs are modelled such that the client is the main point of access to the generated code.

Importing Your Client

You import your client from the namespace specified when generating (under flag --namespace). For the sake of this example, let's say the namespace is azure.pets. Your client's name is detailed in the swagger, and let's say ours is called PetsClient.

Putting this together, we import our client like so:

from azure.pets import PetsClient

Minimum Dependencies of Your Client

The only scenario the generated code can force dependencies is if you generate with a setup.py file using the --basic-setup-py flag. The following are core libraries your generated code depend on, and the minimum version we highly recommend:

Library Description Min Version
azure-core The most important library to have installed. It provides shared exceptions and modules for all the Python SDK client libraries. 1.24.0
msrest Library mainly used for serializing and deserializing objects 0.7.0
azure-mgmt-core Required if you're generating mgmt plane code (see --azure-arm flag in our flag index. Provides mgmt plane specific shared exceptions and modules. 1.2.1

Note: We highly recommend tying your library to a major version, for instance, adding azure-core<2.0.0 to tie the azure-core library to 1.x.x

Initializing and Authenticating Your Client

Next, on to initialization. Your constructor can take any number of parameters. By default we generate our clients with an Azure Active Directory (AAD) token credential. We always recommend using a credential type obtained from the azure-identity library for AAD authentication. For this example, we use the most common DefaultAzureCredential.

As an installation note, the azure-identity library is not a requirement in the basic setup.py file we generate (see --basic-setup-py in our flag index for more information), so you would need to explicitly include this library.

from azure.identity import DefaultAzureCredential
from azure.pets import PetsClient

client = PetsClient(credential=DefaultAzureCredential())

You can also have your generated client take in an AzureKeyCredential instead. To do so, generate with flag --credential-types=AzureKeyCredential, and for more information on this flag, see our flag index

from azure.core.credentials import AzureKeyCredential
from azure.pets import PetsClient

credential = "myCredential"
client = PetsClient(credential=AzureKeyCredential(credential))

Each of these credential types also correspond to their own authentication policies that handle the credential. AutoRest automatically generates with the following default authentication policies based on the credential types:

Credential Type Authentication Policy
TokenCredential BearerTokenCredentialPolicy / ARMChallengeAuthenticationPolicy
AzureKeyCredential AzureKeyCredentialPolicy

Currently, we only support generating credentials of type TokenCredential and / or AzureKeyCredential. If you'd like to use your own custom credential, you can pass the custom type into the client. However, you may have to use a custom authentication policy to handle the credential. That can also be passed in to the client. Say your custom credential is called MyCredential, and the policy that handles this credential is called MyAuthenticationPolicy. Initializing your client would look something like client = PetsClient(credential=MyCredential(), authentication_policy=MyAuthenticationPolicy()), though this of course varies based on inputs.

Multi API Client

Initializing your Multi API client is very similar to initializing a normal client. The only difference is there's an added optional parameter api_version. With this parameter, you can specify the API version you want your client to have. If not specified, the multi API client uses the default API version.

Using the Multi API client we generated in our multi API generation, our example client uses default API version v2. If we would like our client at runtime to have API version v1, we would initialize our client like:

from azure.identity import DefaultAzureCredential
from azure.pets import PetsClient

client = PetsClient(credential=DefaultAzureCredential(), api_version="v1")