Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions .chronus/changes/java-generate-clientcore-http-operations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@typespec/http-client-java"
---

Generate HTTP operations directly for the `generic` and `azurev2` Java clients, so newly generated SDKs no longer require the ClientCore annotation processor.
3 changes: 3 additions & 0 deletions packages/http-client-java/generator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

The **Microsoft Java client generator** tool generates client libraries for accessing RESTful web services.

The `generic` and `azurev2` flavors generate HTTP operations directly and no longer require the ClientCore annotation
processor. The `use-rest-proxy: true` option retains the HTTP annotations for ClientCore's runtime `RestProxy`.

## Table of Contents

- [Prerequisites](#prerequisites)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,52 +75,4 @@
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.15.0</version>
<executions>
<execution>
<id>run-annotation-processing</id>
<phase>generate-sources</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<source>1.8</source>
<target>1.8</target>
<release>8</release>
<proc>only</proc>
<generatedSourcesDirectory>${project.build.directory}/generated-sources/</generatedSourcesDirectory>
<annotationProcessorPaths>
<annotationProcessorPath>
<groupId>io.clientcore</groupId>
<artifactId>annotation-processor</artifactId>
<version>1.0.0-beta.4</version>
</annotationProcessorPath>
</annotationProcessorPaths>
<annotationProcessors>
<annotationProcessor>io.clientcore.annotation.processor.AnnotationProcessor</annotationProcessor>
</annotationProcessors>
<compilerArgs>
<arg>-Xlint:-options</arg>
</compilerArgs>
<excludes>
<exclude>module-info.java</exclude>
</excludes>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>io.clientcore</groupId>
<artifactId>annotation-processor</artifactId>
<version>1.0.0-beta.4</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package authentication.apikey.implementation;

import authentication.apikey.InvalidAuth;
import io.clientcore.core.annotations.ReturnType;
import io.clientcore.core.annotations.ServiceInterface;
import io.clientcore.core.annotations.ServiceMethod;
import io.clientcore.core.http.annotations.HostParam;
import io.clientcore.core.http.annotations.HttpRequestInformation;
import io.clientcore.core.http.annotations.UnexpectedResponseExceptionDetail;
import io.clientcore.core.http.models.HttpMethod;
import io.clientcore.core.http.models.HttpRequest;
import io.clientcore.core.http.models.HttpResponseException;
import io.clientcore.core.http.models.RequestContext;
import io.clientcore.core.http.models.Response;
import io.clientcore.core.http.pipeline.HttpPipeline;
import io.clientcore.core.instrumentation.Instrumentation;
import java.lang.reflect.InvocationTargetException;
import io.clientcore.core.instrumentation.logging.ClientLogger;
import io.clientcore.core.models.binarydata.BinaryData;
import io.clientcore.core.serialization.json.JsonSerializer;
import io.clientcore.core.serialization.xml.XmlSerializer;
import io.clientcore.core.utils.CoreUtils;
import io.clientcore.core.utils.GeneratedCodeUtils;
import java.util.Map;

/**
* Initializes a new instance of the ApiKeyClient type.
Expand Down Expand Up @@ -81,36 +83,67 @@ public ApiKeyClientImpl(HttpPipeline httpPipeline, Instrumentation instrumentati
}

/**
* The interface defining all the services for ApiKeyClient to be used by the proxy service to perform REST calls.
* The HTTP operations for ApiKeyClient.
*/
@ServiceInterface(name = "ApiKeyClient", host = "{endpoint}")
public interface ApiKeyClientService {
static ApiKeyClientService getNewInstance(HttpPipeline pipeline) {
try {
Class<?> clazz = Class.forName("authentication.apikey.implementation.ApiKeyClientServiceImpl");
return (ApiKeyClientService) clazz.getMethod("getNewInstance", HttpPipeline.class)
.invoke(null, pipeline);
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException
| InvocationTargetException e) {
throw new RuntimeException(e);
}
return new ApiKeyClientServiceImpl(pipeline);
}

Response<Void> valid(String endpoint, RequestContext requestContext);

Response<Void> invalid(String endpoint, RequestContext requestContext);
}

private static final class ApiKeyClientServiceImpl implements ApiKeyClientService {
private static final ClientLogger HTTP_LOG = new ClientLogger(ApiKeyClientService.class);

private final HttpPipeline httpPipeline;

private final JsonSerializer jsonSerializer = JsonSerializer.getInstance();

private final XmlSerializer xmlSerializer = XmlSerializer.getInstance();

private ApiKeyClientServiceImpl(HttpPipeline httpPipeline) {
this.httpPipeline = httpPipeline;
}

@HttpRequestInformation(
method = HttpMethod.GET,
path = "/authentication/api-key/valid",
expectedStatusCodes = { 204 })
@UnexpectedResponseExceptionDetail
Response<Void> valid(@HostParam("endpoint") String endpoint, RequestContext requestContext);

@HttpRequestInformation(
method = HttpMethod.GET,
path = "/authentication/api-key/invalid",
expectedStatusCodes = { 204 })
@UnexpectedResponseExceptionDetail(statusCode = { 403 }, exceptionBodyClass = InvalidAuth.class)
@UnexpectedResponseExceptionDetail
Response<Void> invalid(@HostParam("endpoint") String endpoint, RequestContext requestContext);
@Override
public Response<Void> valid(String endpoint, RequestContext requestContext) {
HttpRequest httpRequest
= new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/authentication/api-key/valid");
httpRequest.setContext(requestContext);
httpRequest.getContext().getRequestCallback().accept(httpRequest);
try (Response<BinaryData> networkResponse = this.httpPipeline.send(httpRequest)) {
int responseCode = networkResponse.getStatusCode();
boolean expectedResponse = responseCode == 204;
if (!expectedResponse) {
GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer,
this.xmlSerializer, null, null, HTTP_LOG);
}
return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), null);
}
}

@Override
public Response<Void> invalid(String endpoint, RequestContext requestContext) {
HttpRequest httpRequest
= new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/authentication/api-key/invalid");
httpRequest.setContext(requestContext);
httpRequest.getContext().getRequestCallback().accept(httpRequest);
try (Response<BinaryData> networkResponse = this.httpPipeline.send(httpRequest)) {
int responseCode = networkResponse.getStatusCode();
boolean expectedResponse = responseCode == 204;
if (!expectedResponse) {
GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer,
this.xmlSerializer, null,
Map.ofEntries(
Map.entry(403, CoreUtils.createParameterizedType(authentication.apikey.InvalidAuth.class))),
HTTP_LOG);
}
return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), null);
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package authentication.http.custom.implementation;

import authentication.http.custom.InvalidAuth;
import io.clientcore.core.annotations.ReturnType;
import io.clientcore.core.annotations.ServiceInterface;
import io.clientcore.core.annotations.ServiceMethod;
import io.clientcore.core.http.annotations.HostParam;
import io.clientcore.core.http.annotations.HttpRequestInformation;
import io.clientcore.core.http.annotations.UnexpectedResponseExceptionDetail;
import io.clientcore.core.http.models.HttpMethod;
import io.clientcore.core.http.models.HttpRequest;
import io.clientcore.core.http.models.HttpResponseException;
import io.clientcore.core.http.models.RequestContext;
import io.clientcore.core.http.models.Response;
import io.clientcore.core.http.pipeline.HttpPipeline;
import io.clientcore.core.instrumentation.Instrumentation;
import java.lang.reflect.InvocationTargetException;
import io.clientcore.core.instrumentation.logging.ClientLogger;
import io.clientcore.core.models.binarydata.BinaryData;
import io.clientcore.core.serialization.json.JsonSerializer;
import io.clientcore.core.serialization.xml.XmlSerializer;
import io.clientcore.core.utils.CoreUtils;
import io.clientcore.core.utils.GeneratedCodeUtils;
import java.util.Map;

/**
* Initializes a new instance of the CustomClient type.
Expand Down Expand Up @@ -81,36 +83,67 @@ public CustomClientImpl(HttpPipeline httpPipeline, Instrumentation instrumentati
}

/**
* The interface defining all the services for CustomClient to be used by the proxy service to perform REST calls.
* The HTTP operations for CustomClient.
*/
@ServiceInterface(name = "CustomClient", host = "{endpoint}")
public interface CustomClientService {
static CustomClientService getNewInstance(HttpPipeline pipeline) {
try {
Class<?> clazz = Class.forName("authentication.http.custom.implementation.CustomClientServiceImpl");
return (CustomClientService) clazz.getMethod("getNewInstance", HttpPipeline.class)
.invoke(null, pipeline);
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException
| InvocationTargetException e) {
throw new RuntimeException(e);
}
return new CustomClientServiceImpl(pipeline);
}

Response<Void> valid(String endpoint, RequestContext requestContext);

Response<Void> invalid(String endpoint, RequestContext requestContext);
}

private static final class CustomClientServiceImpl implements CustomClientService {
private static final ClientLogger HTTP_LOG = new ClientLogger(CustomClientService.class);

private final HttpPipeline httpPipeline;

private final JsonSerializer jsonSerializer = JsonSerializer.getInstance();

private final XmlSerializer xmlSerializer = XmlSerializer.getInstance();

private CustomClientServiceImpl(HttpPipeline httpPipeline) {
this.httpPipeline = httpPipeline;
}

@HttpRequestInformation(
method = HttpMethod.GET,
path = "/authentication/http/custom/valid",
expectedStatusCodes = { 204 })
@UnexpectedResponseExceptionDetail
Response<Void> valid(@HostParam("endpoint") String endpoint, RequestContext requestContext);

@HttpRequestInformation(
method = HttpMethod.GET,
path = "/authentication/http/custom/invalid",
expectedStatusCodes = { 204 })
@UnexpectedResponseExceptionDetail(statusCode = { 403 }, exceptionBodyClass = InvalidAuth.class)
@UnexpectedResponseExceptionDetail
Response<Void> invalid(@HostParam("endpoint") String endpoint, RequestContext requestContext);
@Override
public Response<Void> valid(String endpoint, RequestContext requestContext) {
HttpRequest httpRequest
= new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/authentication/http/custom/valid");
httpRequest.setContext(requestContext);
httpRequest.getContext().getRequestCallback().accept(httpRequest);
try (Response<BinaryData> networkResponse = this.httpPipeline.send(httpRequest)) {
int responseCode = networkResponse.getStatusCode();
boolean expectedResponse = responseCode == 204;
if (!expectedResponse) {
GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer,
this.xmlSerializer, null, null, HTTP_LOG);
}
return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), null);
}
}

@Override
public Response<Void> invalid(String endpoint, RequestContext requestContext) {
HttpRequest httpRequest
= new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/authentication/http/custom/invalid");
httpRequest.setContext(requestContext);
httpRequest.getContext().getRequestCallback().accept(httpRequest);
try (Response<BinaryData> networkResponse = this.httpPipeline.send(httpRequest)) {
int responseCode = networkResponse.getStatusCode();
boolean expectedResponse = responseCode == 204;
if (!expectedResponse) {
GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer,
this.xmlSerializer, null,
Map.ofEntries(Map.entry(403,
CoreUtils.createParameterizedType(authentication.http.custom.InvalidAuth.class))),
HTTP_LOG);
}
return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), null);
}
}
}

/**
Expand Down
Loading