-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Protocol Methods Quickstart with AutoRest
Java Azure SDK Design Guidelines is the overall design guideline of the data-plane SDK.
You can choose from
- Python script, Java developer environment required
- Azure pipeline
to create your first data-plane SDK.
A Python script in Azure SDK for Java can help generate a minimal data-plane client SDK from OpenAPI 2.0 specification.
Please see Working with AutoRest for the installation of autorest CLI. It also requires the installation of Node.js 10 or above, Java 8 or above, and Python 3.
If you are new to Java development environment, see Java Development Environment for details.
Required options from user:
-
input-file: URL to OpenAPI 2.0 specification JSON as input file. -
service: Service name undersdk/, sample:storage. -
module: Module name undersdk/<service>/, sample:azure-storage-blob. -
credential-types: Credential types, sample:tokencredentialfor AAD credential for OAuth 2.0 authentication. -
credential-scopes: OAuth 2.0 scopes, required ifcredential-typesincludestokencredential. -
title: Optional for client name. The name should always ends with "Client", sample:BlobClient, which results inBlobClientBuilderas builder class.
For service, it is usually your service name in REST API specifications.
For instance, if the JSON is under specification/storage, the service would normally be storage.
For module, please refer to Namespace in Java Guideline.
For example, if the namespace is com.azure.storage.blob, the module should be azure-storage-blob.
As an example,
python eng/mgmt/automation/generate_data.py --input-file=https://github.com/Azure/azure-rest-api-specs/blob/main/specification/purview/data-plane/Azure.Analytics.Purview.Account/preview/2019-11-01-preview/account.json --service=purview --module=azure-analytics-purview-account --credential-types=tokencredential --credential-scopes=https://purview.azure.net/.default
This will generate the SDK under sdk/purview/azure-analytics-purview-account. And your follow-up work will mostly happen in this SDK folder.
The Python script also builds the generated project. It is possible that the verification fails due to Java code quality requirements. See Build section on how to fix.
There is an internal pipeline java - client generation - data, which invokes the the Python script, generate the code, and create pull request of the generated SDK.
It takes similar input parameters as the script, e.g. INPUT_FILE variable for input-file option, SERVICE and MODULE variable for service and module option.
A draft pull request will be created at pull requests, named as [Automation] Generate Data-plane SDK for <module>.
An AutoRest configuration file is generated at swagger/README_SPEC.md during the first-time code generation. You can now edit it, and generate code through this configuration file.
As an example,
autorest --use=@autorest/java@4.0.43 swagger/README_SPEC.md
It is advised to use the latest version of autorest/java extension (see AutoRest.Java Release).
The possible settings can be found either from autorest --help --java, or from AutoRest.Java readme.
A few commonly used settings:
-
titleandservice-nameis used to specify the entry of your SDK. Usually it be as e.g.title=PurviewAccountClientandservice-name=PurviewAccount. -
service-versionsis a list of compatibleapi-versionfor the SDK. If otherapi-versionof your service is compatible with this OpenAPI 2.0 specification, you can add them to this list. -
pollingif the polling strategy of your long-running operation is not covered from pre-defined polling strategies.
See Building and Adding a Module.
Here is the list of files that you are required to modify/confirm for your new module.
-
/eng/versioning/version_client.txtfor versioning -
/sdk/<service>/pom.xmlfor project -
/sdk/<service>/ci.ymlfor CI
The Python script automatically created/modified these files for you. However it is still advised that you double check them.
See Building.
Install SDK to local maven repository:
mvn install -f sdk/<service>/<module>/pom.xml -Dgpg.skip -Drevapi.skip -DskipTests
See Maven Getting Start Guide, if you would like to create a client that uses your SDK.
See sdk/<service>/<module>/README.md for adding the SDK to dependency.
There is many information about the SDK that AutoRest will never know. These can only be provided by a contributor.
A minimal README.md is generated by AutoRest. Your first task will be improving it.
You might want to modify [product_documentation] to point to your service documentation.
A blank Java sample file is generated as ReadmeSamples.java, which is synchronized with your README (see JavaDoc with CodeSnippet).
A collection of generated Java sample files (based on x-ms-examples in OpenAPI specs) is generated under <module>.generated package. They might not be directly usable as runnable sample codes, however you should get the basic idea on how code works, and may even modify some to be runnable.
A blank Java test file is generated as ClientTests.java, with set-up code that prepares the environment for test record and playback. You can follow-up with your test codes.
ClientBuilder, Client, AsyncClient is the user-facing API surface that you might be interested to improve. See Partial Update.
Download Java, install.
Download Maven, unpack, set to PATH.
Download Node.js, install.
Install AutoRest with npm install -g autorest in command line.
Optional for Git and Python:
sudo apt -y install openjdk-17-jdk
sudo apt -y install maven
curl -sL https://deb.nodesource.com/setup_16.x | sudo bash -
sudo apt -y install nodejs
npm install -g autorest
Since autorest/java@4.0.42 and with partial-update option, you can in-place modify the generated code, and AutoRest.Java will keep it intact when it re-generates the SDK.
The user-facing API is of pattern:
FooClient client = new FooClientBuilder()
.endpoint(...)
.credential(new DefaultAzureCredentialBuilder().build())
.buildFooClient();
FooAsyncClient asyncClient = new FooClientBuilder()
.endpoint(...)
.credential(new DefaultAzureCredentialBuilder().build())
.buildFooAsyncClient();And the classes involved is FooClient, FooAsyncClient, and FooClientBuilder.
All of these classes can be customized in-place, to enhance the usability of the SDK, or to add new feature.
You can add new class variable and new class method to these classes, and with partial-update option in swagger/README_SPEC.md, new code generated via AutoRest will not override your change.
For modifying the signature or the implementation of a class method, please remove the @Generated annotation, so AutoRest.Java can recognize that you have modified the method.
If you would like to remove a class method, simply make the method package private, so it is not exposed to customer. Also you need to remove the @Generated annotation.
It is advised that you keep the modifications simple.
If certain feature requires complicated code, it is better to put the implementation code in some other classes within implementation package, and only add the public class method and invocation in these Client or ClientBuilder classes.
- Frequently Asked Questions
- Azure Identity Examples
- Configuration
- Performance Tuning
- Android Support
- Unit Testing
- Test Proxy Migration
- Azure Json Migration
- New Checkstyle and Spotbugs pattern migration
- Protocol Methods
- TypeSpec-Java Quickstart
- Getting Started Guidance
- Adding a Module
- Building
- Writing Performance Tests
- Working with AutoRest
- Deprecation
- BOM guidelines
- Release process
- Access helpers