Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions docs/client/initializing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# <img align="center" src="../images/logo.png"> 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, (TODO link to swagger docs), and let's say
ours is called `PetsClient`.

Putting this together, we import our client like so:

```python
from azure.pets import PetsClient
```

## Initializing Your Client

Next, on to initialization. Your constructor can take any number of parameters. If your client has no parameters (no client parameters detailed
in the swagger (TODO: link to swagger docs) and you choose to generate without credentials), initializing your client would just look like

```python
from azure.pets import PetsClient

client = PetsClient()
```

However, by default we generate clients with credentials, so continue on to [Authenticating Your Client](#authenticating-your-client "Authenticating Your Client")
to find out how to input a credential.

## Authenticating Your Client
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we mention what to do when your service has a custom credential? E.g. MetricsAdvisor uses a MetricsAdvisorKeyCredential.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sound sgood, since we don't support generating custom credentials, I can talk about passing in yoru own and your own authentication polity. to handle it


By default we generate our clients with an [Azure Active Directory (AAD) token credential][aad_authentication]. We always recommend
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it default or do you need the add credentials flag?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(this isn't the current behavior, but this is what I think the behavior is going to be): default without specifying you want credentials. We're thinking of deprecating add-credentials, and instead encouring new flag --credential-types. Default of that being TokenCredential, can also specify AzureKeyCredential or None

using a [credential type][identity_credentials] obtained from the [`azure-identity`][identity_pypi] library for AAD authentication. For this example,
we use the most common [`DefaultAzureCredential`][default_azure_credential].

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

```python
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`][azure_key_credential] instead. To do so, generate with flag `--credential-types=AzureKeyCredential`,
and for more information on this flag, see our [flag index][flag_index]

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

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

## 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][multiapi_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:

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

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


<!-- LINKS -->
[multiapi_generation]: ../generate/multiapi.md
[aad_authentication]: https://docs.microsoft.com/en-us/azure/cognitive-services/authentication?tabs=powershell#authenticate-with-azure-active-directory
[identity_credentials]: https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/identity/azure-identity#credentials
[identity_pypi]: https://pypi.org/project/azure-identity/
[default_azure_credential]: https://docs.microsoft.com/en-us/python/api/azure-identity/azure.identity.defaultazurecredential?view=azure-python
[azure_key_credential]: https://docs.microsoft.com/en-us/python/api/azure-core/azure.core.credentials.azurekeycredential?view=azure-python
[flag_index]: https://github.com/Azure/autorest/tree/master/docs/generate/flags.md
13 changes: 13 additions & 0 deletions docs/client/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# <img align="center" src="./images/logo.png"> Using the Python Client

After [generating][generate] your client, this section tells you how to actually use your generated client.

* Initializing your Python Client
* Calling operations with your Python Client
* Error Handling
* Tracing
* Dependencies your Generated Code Has

<!-- LINKS -->
[generate]: https://github.com/Azure/autorest/tree/master/docs/generate/readme.md
[initializing]: ./initializing.md
1 change: 1 addition & 0 deletions docs/generate/directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ Here is the before and after of the generated code.


We have put all of these Python-specific directives in a [config file][directive_readme] for you to refer to.

<!-- LINKS -->

[lro_poller_docs]: https://docs.microsoft.com/en-us/python/api/azure-core/azure.core.polling.lropoller?view=azure-python
Expand Down
10 changes: 7 additions & 3 deletions docs/generate/readme.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# <img align="center" src="./images/logo.png"> Generating Python Clients with AutoRest

Most of the information you'll need to generate a Python client can be found in the general docs [here](https://github.com/Azure/autorest/tree/master/docs/generate/readme.md). In these docs, we go over a couple Python-specific scenarios.
Most of the information you'll need to generate a Python client can be found in the general docs [here][general]. In these docs, we go over a couple Python-specific scenarios.

## Generating Multi API code
* [Generating Multi API code][multiapi]
* [Generating with Directives][directives]

See the docs [here](./multiapi.md)
<!-- LINKS -->
[general]: https://github.com/Azure/autorest/tree/master/docs/generate/readme.md
[multiapi]: ./multiapi.md
[directives]: ./directives.md