-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Generate a Python SDK from a Swagger file
This document provides instructions and guidelines on how to generate a Python SDK from a Swagger file.
Table of contents:
If you're discovering Python for the first time, we have great intros using Notebooks on Azure. Click the "Show me more samples" button to see them.
Once you're comfortable with Python:
-
You need to install Autorest: aka.ms/autorest/install. Minimal Autorest version is "1.0.1-20170416-2300-nightly".
-
To call Autorest, you need the following options:
-
Required parameter:
-ft 2
-
About the generator:
- If your endpoint is ARM, add
-g Azure.Python
- If not, add
-g Python
. If your client might ask authentication, add-AddCredentials true
- If your endpoint is ARM, add
-
If your input is a Composite file, add
-Modeler CompositeSwagger
-
And that's it! You should now have Python code ready to test. Note that this generation is for testing only and should not be sent to a customer or published to PyPI.
ARM management Swagger (not composite):
autorest --version=latest -g Azure.Python -ft 2 -i myswagger.json
ARM management Swagger composite:
autorest --version=latest -g Azure.Python -ft 2 -Modeler CompositeSwagger -i myswagger.json
Not-ARM Swagger:
autorest --version=latest -g Python -ft 2 -AddCredentials true -i myswagger.json
First, your Swagger needs to at least be a PR in the official Microsoft Swagger RestAPI repo. This allows you to determine the name of your future package.
- If your root folder is prefixed by
arm-
:- Your package name will be prefixed with
azure-mgmt-
- Your namespace will start with
azure.mgmt
- Your package name will be prefixed with
- If not:
- Your package name will be prefixed with
azure-
- Your namespace will start with
azure
- Your package name will be prefixed with
For example, the arm-compute
folder will create the package azure-mgmt-compute
and will use the namespace azure.mgmt.compute
.
Let's assume for now that your Swagger is in arm-compute
To call Autorest, you need the following options:
-
Required parameters:
-ft 2 -Header MICROSOFT_MIT_NO_VERSION -Namespace azure.mgmt.compute -PackageName azure-mgmt-compute -PackageVersion 0.1.0
-
About the generator:
- If your endpoint is ARM, add
-g Azure.Python
- If not, add
-g Python
. If your client might ask authentication, add-AddCredentials true
- If your endpoint is ARM, add
-
About the modeler, if your file is Composite file, add
-Modeler CompositeSwagger
ARM Swagger:
autorest --version=latest -g Azure.Python -ft 2 -Header MICROSOFT_MIT_NO_VERSION -Namespace azure.mgmt.storage -PackageName azure-mgmt-storage -PackageVersion 0.1.0 -i arm-storage/2016-12-01/swagger/storage.json
ARM composite Swagger:
autorest --version=latest -g Azure.Python -ft 2 -Header MICROSOFT_MIT_NO_VERSION -Namespace azure.mgmt.compute -PackageName azure-mgmt-compute -PackageVersion 0.1.0 -Modeler CompositeSwagger -i arm-compute/compositeComputeClient.json
We recommend you to test your package. We made a tutorial to help you with this process: https://github.com/Azure/azure-sdk-for-python/wiki/Contributing-to-the-tests
Note this is not mandatory, but testing is important, and Autorest has regularly issues that can only be seen in one specific client in one specific context (so code generation does not protect you from bugs)
You can create a PR on this repo with your package. Create at the root a folder with the name of your package and put the files generated by Autorest inside. If you did it correctly, you should see in your folder a setup.py
file generated by Autorest.
Example: if your package name is azure-mgmt-logic
, create a azure-mgmt-logic
folder at the root and put the Autorest files inside in a way that azure-mgmt-logic/setup.py
exists.
In your PR, you need to update the configuration file called swagger_to_sdk_config.json
as well.
A typical new entry looks like this:
"logic": {
"swagger": "arm-logic/2016-06-01/swagger/logic.json",
"autorest_options": {
"Namespace": "azure.mgmt.logic",
"PackageName": "azure-mgmt-logic",
"PackageVersion": "2.0.0"
},
"output_dir": "azure-mgmt-logic/azure/mgmt/logic"
},
This will be used by the CI to update your package automatically in the future.
You're also encouraged to create tests for your package in the PR. We made a "contribute to the tests" tutorial.
When you have a PR merged on https://github.com/Azure/azure-rest-api-specs, if your configuration file is up to date, then a PR will be automatically created here. The link will appear in your Swagger PR. You can review the Python PR and comment on it if necessary (even just a LGTM).
Open a PR here with the update in the configuration file for your project.
- If your Swagger PR is not merged yet, that's it: your package will be updated when the Swagger PR is merged.
- If your Swagger PR is already merged, there is no automatic way to regenerate your package. You can regenerate manually yourself and create a PR for the change, or you can simulate the CI generation (there is a process for that) or simply ask me @lmazuel to do it.
To insure all packages are released correctly and meets the quality we expect (for instance, there is some tricky configuration details on how to build the package to improve the performance on the client machine), only us in the Python team can release on PyPI for now.