-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add compile time sdk-codegen-tool #43457
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<!-- Copyright (c) Microsoft Corporation. All rights reserved. | ||
Licensed under the MIT License. --> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>io.clientcore</groupId> | ||
<artifactId>clientcore-tools-service</artifactId> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
<packaging>pom</packaging> | ||
<version>1.0.0</version> <!-- Need not change for every release--> | ||
<modules> | ||
<module>sdk-codegen-tool</module> | ||
</modules> | ||
</project> |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,135 @@ | ||||||
# Codegen Compile time generator Plugin | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
A Java annotation processor for generating HTTP service implementations based on annotated interfaces. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We might not always do this for generating HTTP service implementations. Might be best to just say this is the client-core annotation processor, for introducing compile-time code generation for libraries based on client core? |
||||||
|
||||||
## Usage | ||||||
|
||||||
1. Add the plugin dependency: | ||||||
```xml | ||||||
<dependencies> | ||||||
<dependency> | ||||||
<groupId>io.clientcore.tools</groupId> | ||||||
<artifactId>sdk-codegen-tool</artifactId> | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
<version>1.0.0.beta.1</version> <!-- {x-version-update;io.clientcore.tools:sdk-codegen-tool;external_dependency} --> | ||||||
<scope>provided</scope> | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why |
||||||
</dependency> | ||||||
</dependencies> | ||||||
``` | ||||||
1.1. Add the plugin configuration to your `pom.xml`: | ||||||
```xml | ||||||
<plugins> | ||||||
<plugin> | ||||||
<groupId>org.apache.maven.plugins</groupId> | ||||||
<artifactId>maven-compiler-plugin</artifactId> | ||||||
<version>3.11.0</version> <!-- {x-version-update;org.apache.maven.plugins:maven-compiler-plugin;external_dependency} --> | ||||||
<configuration> | ||||||
<generatedSourcesDirectory>${project.build.directory}/generated-sources/</generatedSourcesDirectory> | ||||||
<annotationProcessors> | ||||||
<annotationProcessor>io.generation.tools.codegen.AnnotationProcessor</annotationProcessor> | ||||||
</annotationProcessors> | ||||||
</configuration> | ||||||
</plugin> | ||||||
</plugins> | ||||||
``` | ||||||
2. Annotate your interfaces with `@ServiceInterface`, `@HttpRequestInformation` and | ||||||
`@UnexpectedResponseExceptionDetail` such annotations: | ||||||
```java | ||||||
@ServiceInterface(name = "ExampleClient", host = "{endpoint}/example") | ||||||
public interface ExampleService { | ||||||
@HttpRequestInformation(method = HttpMethod.GET, path = "/user/{userId}", expectedStatusCodes = { 200 }) | ||||||
@UnexpectedResponseExceptionDetail(exceptionTypeName = "CLIENT_AUTHENTICATION", statusCode = { 401 }) | ||||||
@UnexpectedResponseExceptionDetail(exceptionTypeName = "RESOURCE_NOT_FOUND", statusCode = { 404 }) | ||||||
@UnexpectedResponseExceptionDetail(exceptionTypeName = "RESOURCE_MODIFIED", statusCode = { 409 }) | ||||||
User getUser(@PathParam("userId") String userId); | ||||||
} | ||||||
``` | ||||||
|
||||||
3. Build your project and the plugin will generate an implementation of the annotated interface. | ||||||
The processor would generate an implementation: | ||||||
```java | ||||||
public class ExampleServiceImpl implements ExampleService { | ||||||
private static final ClientLogger LOGGER = new ClientLogger(OpenAIClientServiceImpl.class); | ||||||
|
||||||
private final HttpPipeline defaultPipeline; | ||||||
|
||||||
private final ObjectSerializer serializer; | ||||||
|
||||||
private final String endpoint; | ||||||
|
||||||
private final OpenAIServiceVersion serviceVersion; | ||||||
|
||||||
private String apiVersion; | ||||||
|
||||||
public OpenAIClientServiceImpl(HttpPipeline defaultPipeline, ObjectSerializer serializer, | ||||||
String endpoint, OpenAIServiceVersion serviceVersion) { | ||||||
this.defaultPipeline = defaultPipeline; | ||||||
this.serializer = serializer; | ||||||
this.endpoint = endpoint; | ||||||
this.apiVersion = serviceVersion.getVersion(); | ||||||
this.serviceVersion = serviceVersion; | ||||||
} | ||||||
|
||||||
public String getEndpoint() { | ||||||
return endpoint; | ||||||
} | ||||||
|
||||||
public HttpPipeline getPipeline() { | ||||||
return defaultPipeline; | ||||||
} | ||||||
|
||||||
public OpenAIServiceVersion getServiceVersion() { | ||||||
return serviceVersion; | ||||||
} | ||||||
|
||||||
private final HttpPipeline pipeline; | ||||||
|
||||||
public ExampleServiceImpl(HttpPipeline pipeline) { | ||||||
this.pipeline = pipeline; | ||||||
} | ||||||
|
||||||
public Response<BinaryData> getUser(String userId, Context context) { | ||||||
return getUser(endpoint, apiVersion, userId, context); | ||||||
} | ||||||
|
||||||
@Override | ||||||
private Response<BinaryData> getUser(String endpoint, String apiVersion, String userId, Context context) { | ||||||
HttpPipeline pipeline = this.getPipeline(); | ||||||
String host = endpoint + "/example/users/" + userId + "?api-version=" + apiVersion; | ||||||
|
||||||
// create the request | ||||||
HttpRequest httpRequest = new HttpRequest(HttpMethod.GET, host); | ||||||
|
||||||
// set the headers | ||||||
HttpHeaders headers = new HttpHeaders(); | ||||||
httpRequest.setHeaders(headers); | ||||||
|
||||||
// add RequestOptions to the request | ||||||
httpRequest.setRequestOptions(requestOptions); | ||||||
|
||||||
// set the body content if present | ||||||
|
||||||
// send the request through the pipeline | ||||||
Response<?> response = pipeline.send(httpRequest); | ||||||
|
||||||
final int responseCode = response.getStatusCode(); | ||||||
boolean expectedResponse = responseCode == 200; | ||||||
if (!expectedResponse) { | ||||||
throw new RuntimeException("Unexpected response code: " + responseCode); | ||||||
} | ||||||
ResponseBodyMode responseBodyMode = ResponseBodyMode.IGNORE; | ||||||
if (requestOptions != null) { | ||||||
responseBodyMode = requestOptions.getResponseBodyMode(); | ||||||
} | ||||||
if (responseBodyMode == ResponseBodyMode.DESERIALIZE) { | ||||||
BinaryData responseBody = response.getBody(); | ||||||
HttpResponseAccessHelper.setValue((HttpResponse<?>) response, responseBody); | ||||||
} else { | ||||||
BinaryData responseBody = response.getBody(); | ||||||
HttpResponseAccessHelper.setBodyDeserializer((HttpResponse<?>) response, (body) -> responseBody); | ||||||
} | ||||||
return (Response<BinaryData>) response; | ||||||
} | ||||||
} | ||||||
``` | ||||||
This implementation eliminates reflection and integrates directly with your HTTP client infrastructure. | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
|
||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>io.clientcore.tools</groupId> | ||
<artifactId>sdk-codegen-tool</artifactId> | ||
<version>1.0.0-beta.1</version> <!-- {x-version-update;io.clientcore.tools:sdk-codegen-tool;external_dependency} --> | ||
|
||
<name>Clientcore codegen tool</name> | ||
<description>A Java annotation processor tool for generating HTTP service implementations</description> | ||
|
||
<distributionManagement> | ||
<snapshotRepository> | ||
<id>ossrh</id> | ||
<url>https://oss.sonatype.org/content/repositories/snapshots</url> | ||
</snapshotRepository> | ||
<repository> | ||
<id>ossrh</id> | ||
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url> | ||
</repository> | ||
</distributionManagement> | ||
|
||
<url>https://github.com/azure/azure-sdk-for-java</url> | ||
<organization> | ||
<name>Microsoft Corporation</name> | ||
<url>http://microsoft.com</url> | ||
</organization> | ||
<licenses> | ||
<license> | ||
<name>The MIT License (MIT)</name> | ||
<url>http://opensource.org/licenses/MIT</url> | ||
<distribution>repo</distribution> | ||
</license> | ||
</licenses> | ||
|
||
<developers> | ||
<developer> | ||
<id>microsoft</id> | ||
<name>Microsoft Corporation</name> | ||
</developer> | ||
</developers> | ||
|
||
<issueManagement> | ||
<system>GitHub</system> | ||
<url>https://github.com/Azure/azure-sdk-for-java/issues</url> | ||
</issueManagement> | ||
|
||
<scm> | ||
<url>https://github.com/Azure/azure-sdk-for-java</url> | ||
<connection>scm:git:https://github.com/Azure/azure-sdk-for-java.git</connection> | ||
<developerConnection/> | ||
<tag>HEAD</tag> | ||
</scm> | ||
|
||
<properties> | ||
<maven.compiler.source>8</maven.compiler.source> | ||
<maven.compiler.target>8</maven.compiler.target> | ||
<packageOutputDirectory>${project.build.directory}</packageOutputDirectory> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.squareup</groupId> | ||
<artifactId>javapoet</artifactId> | ||
<version>1.13.0</version> <!-- {x-version-update;com.squareup:javapoet;external_dependency} --> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.clientcore</groupId> | ||
<artifactId>core</artifactId> | ||
<version>1.0.0-beta.1</version> <!-- {x-version-update;io.clientcore:core;dependency} --> | ||
<scope>compile</scope> | ||
</dependency> | ||
|
||
<!-- Unit Test --> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-api</artifactId> | ||
<version>5.11.2</version> <!-- {x-version-update;org.junit.jupiter:junit-jupiter-api;external_dependency} --> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-engine</artifactId> | ||
<version>5.11.2</version> <!-- {x-version-update;org.junit.jupiter:junit-jupiter-engine;external_dependency} --> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-params</artifactId> | ||
<version>5.11.2</version> <!-- {x-version-update;org.junit.jupiter:junit-jupiter-params;external_dependency} --> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-core</artifactId> | ||
<version>4.11.0</version> <!-- {x-version-update;org.mockito:mockito-core;external_dependency} --> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<resources> | ||
<resource> | ||
<directory>src/main/resources</directory> | ||
<filtering>true</filtering> | ||
</resource> | ||
</resources> | ||
</build> | ||
</project> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can just put this under the same
io.clientcore
groupId - we don't need another sub-group