diff --git a/.chronus/changes/java-generate-clientcore-http-operations.md b/.chronus/changes/java-generate-clientcore-http-operations.md new file mode 100644 index 00000000000..9a39058bf21 --- /dev/null +++ b/.chronus/changes/java-generate-clientcore-http-operations.md @@ -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. diff --git a/packages/http-client-java/generator/README.md b/packages/http-client-java/generator/README.md index a48f0837409..ca280d4f244 100644 --- a/packages/http-client-java/generator/README.md +++ b/packages/http-client-java/generator/README.md @@ -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) diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/pom.xml b/packages/http-client-java/generator/http-client-generator-clientcore-test/pom.xml index f1941780c33..6572a6bf4b5 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/pom.xml +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/pom.xml @@ -75,52 +75,4 @@ - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.15.0 - - - run-annotation-processing - generate-sources - - compile - - - 1.8 - 1.8 - 8 - only - ${project.build.directory}/generated-sources/ - - - io.clientcore - annotation-processor - 1.0.0-beta.4 - - - - io.clientcore.annotation.processor.AnnotationProcessor - - - -Xlint:-options - - - module-info.java - - - - - - - io.clientcore - annotation-processor - 1.0.0-beta.4 - - - - - diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/authentication/apikey/implementation/ApiKeyClientImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/authentication/apikey/implementation/ApiKeyClientImpl.java index 372a57b8f03..3d87f3c9607 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/authentication/apikey/implementation/ApiKeyClientImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/authentication/apikey/implementation/ApiKeyClientImpl.java @@ -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. @@ -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 valid(String endpoint, RequestContext requestContext); + + Response 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 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 invalid(@HostParam("endpoint") String endpoint, RequestContext requestContext); + @Override + public Response 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 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 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 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/authentication/http/custom/implementation/CustomClientImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/authentication/http/custom/implementation/CustomClientImpl.java index 9eaf9738200..9c08c4f71e5 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/authentication/http/custom/implementation/CustomClientImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/authentication/http/custom/implementation/CustomClientImpl.java @@ -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. @@ -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 valid(String endpoint, RequestContext requestContext); + + Response 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 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 invalid(@HostParam("endpoint") String endpoint, RequestContext requestContext); + @Override + public Response 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 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 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 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/authentication/noauth/union/implementation/UnionClientImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/authentication/noauth/union/implementation/UnionClientImpl.java index 548ad00e46c..658209787f7 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/authentication/noauth/union/implementation/UnionClientImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/authentication/noauth/union/implementation/UnionClientImpl.java @@ -1,18 +1,19 @@ package authentication.noauth.union.implementation; 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.GeneratedCodeUtils; /** * Initializes a new instance of the UnionClient type. @@ -80,35 +81,64 @@ public UnionClientImpl(HttpPipeline httpPipeline, Instrumentation instrumentatio } /** - * The interface defining all the services for UnionClient to be used by the proxy service to perform REST calls. + * The HTTP operations for UnionClient. */ - @ServiceInterface(name = "UnionClient", host = "{endpoint}") public interface UnionClientService { static UnionClientService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("authentication.noauth.union.implementation.UnionClientServiceImpl"); - return (UnionClientService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new UnionClientServiceImpl(pipeline); + } + + Response validNoAuth(String endpoint, RequestContext requestContext); + + Response validToken(String endpoint, RequestContext requestContext); + } + + private static final class UnionClientServiceImpl implements UnionClientService { + private static final ClientLogger HTTP_LOG = new ClientLogger(UnionClientService.class); + + private final HttpPipeline httpPipeline; + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private UnionClientServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/authentication/noauth/union/valid", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response validNoAuth(@HostParam("endpoint") String endpoint, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/authentication/noauth/union/validtoken", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response validToken(@HostParam("endpoint") String endpoint, RequestContext requestContext); + @Override + public Response validNoAuth(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/authentication/noauth/union/valid"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 validToken(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/authentication/noauth/union/validtoken"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/authentication/oauth2/implementation/OAuth2ClientImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/authentication/oauth2/implementation/OAuth2ClientImpl.java index e8c55050490..3c4251ce678 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/authentication/oauth2/implementation/OAuth2ClientImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/authentication/oauth2/implementation/OAuth2ClientImpl.java @@ -1,19 +1,21 @@ package authentication.oauth2.implementation; -import authentication.oauth2.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 OAuth2Client type. @@ -81,36 +83,67 @@ public OAuth2ClientImpl(HttpPipeline httpPipeline, Instrumentation instrumentati } /** - * The interface defining all the services for OAuth2Client to be used by the proxy service to perform REST calls. + * The HTTP operations for OAuth2Client. */ - @ServiceInterface(name = "OAuth2Client", host = "{endpoint}") public interface OAuth2ClientService { static OAuth2ClientService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("authentication.oauth2.implementation.OAuth2ClientServiceImpl"); - return (OAuth2ClientService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new OAuth2ClientServiceImpl(pipeline); + } + + Response valid(String endpoint, RequestContext requestContext); + + Response invalid(String endpoint, RequestContext requestContext); + } + + private static final class OAuth2ClientServiceImpl implements OAuth2ClientService { + private static final ClientLogger HTTP_LOG = new ClientLogger(OAuth2ClientService.class); + + private final HttpPipeline httpPipeline; + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private OAuth2ClientServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/authentication/oauth2/valid", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response valid(@HostParam("endpoint") String endpoint, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/authentication/oauth2/invalid", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail(statusCode = { 403 }, exceptionBodyClass = InvalidAuth.class) - @UnexpectedResponseExceptionDetail - Response invalid(@HostParam("endpoint") String endpoint, RequestContext requestContext); + @Override + public Response valid(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/authentication/oauth2/valid"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 invalid(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/authentication/oauth2/invalid"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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.oauth2.InvalidAuth.class))), + HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), null); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/authentication/union/implementation/UnionClientImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/authentication/union/implementation/UnionClientImpl.java index e1bea9c2211..076a2356df9 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/authentication/union/implementation/UnionClientImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/authentication/union/implementation/UnionClientImpl.java @@ -1,18 +1,19 @@ package authentication.union.implementation; 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.GeneratedCodeUtils; /** * Initializes a new instance of the UnionClient type. @@ -80,35 +81,64 @@ public UnionClientImpl(HttpPipeline httpPipeline, Instrumentation instrumentatio } /** - * The interface defining all the services for UnionClient to be used by the proxy service to perform REST calls. + * The HTTP operations for UnionClient. */ - @ServiceInterface(name = "UnionClient", host = "{endpoint}") public interface UnionClientService { static UnionClientService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("authentication.union.implementation.UnionClientServiceImpl"); - return (UnionClientService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new UnionClientServiceImpl(pipeline); + } + + Response validKey(String endpoint, RequestContext requestContext); + + Response validToken(String endpoint, RequestContext requestContext); + } + + private static final class UnionClientServiceImpl implements UnionClientService { + private static final ClientLogger HTTP_LOG = new ClientLogger(UnionClientService.class); + + private final HttpPipeline httpPipeline; + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private UnionClientServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/authentication/union/validkey", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response validKey(@HostParam("endpoint") String endpoint, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/authentication/union/validtoken", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response validToken(@HostParam("endpoint") String endpoint, RequestContext requestContext); + @Override + public Response validKey(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/authentication/union/validkey"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 validToken(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/authentication/union/validtoken"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/documentation/implementation/ListsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/documentation/implementation/ListsImpl.java index f23f2bb60ad..f1836fbf046 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/documentation/implementation/ListsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/documentation/implementation/ListsImpl.java @@ -3,20 +3,22 @@ import documentation.lists.BulletPointsModel; import documentation.lists.implementation.BulletPointsModelRequest; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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; /** * An instance of this class provides access to all the operations defined in Lists. @@ -49,45 +51,92 @@ public final class ListsImpl { } /** - * The interface defining all the services for DocumentationClientLists to be used by the proxy service to perform - * REST calls. + * The HTTP operations for DocumentationClientLists. */ - @ServiceInterface(name = "DocumentationClientLists", host = "{endpoint}") public interface ListsService { static ListsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("documentation.implementation.ListsServiceImpl"); - return (ListsService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new ListsServiceImpl(pipeline); + } + + Response bulletPointsOp(String endpoint, RequestContext requestContext); + + Response bulletPointsModel(String endpoint, String contentType, + BulletPointsModelRequest bulletPointsModelRequest, RequestContext requestContext); + + Response numbered(String endpoint, RequestContext requestContext); + } + + private static final class ListsServiceImpl implements ListsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ListsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ListsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response bulletPointsOp(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/documentation/lists/bullet-points/op"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 bulletPointsModel(String endpoint, String contentType, + BulletPointsModelRequest bulletPointsModelRequest, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/documentation/lists/bullet-points/model"); + if (bulletPointsModelRequest != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(bulletPointsModelRequest, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response networkResponse = this.httpPipeline.send(httpRequest)) { + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), null); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/documentation/lists/bullet-points/op", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response bulletPointsOp(@HostParam("endpoint") String endpoint, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/documentation/lists/bullet-points/model", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response bulletPointsModel(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") BulletPointsModelRequest bulletPointsModelRequest, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/documentation/lists/numbered", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response numbered(@HostParam("endpoint") String endpoint, RequestContext requestContext); + @Override + public Response numbered(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/documentation/lists/numbered"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/documentation/implementation/TextFormattingsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/documentation/implementation/TextFormattingsImpl.java index 3b3076f04f9..a52f9bad1ab 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/documentation/implementation/TextFormattingsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/documentation/implementation/TextFormattingsImpl.java @@ -1,18 +1,19 @@ package documentation.implementation; 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.GeneratedCodeUtils; /** * An instance of this class provides access to all the operations defined in TextFormattings. @@ -45,43 +46,83 @@ public final class TextFormattingsImpl { } /** - * The interface defining all the services for DocumentationClientTextFormattings to be used by the proxy service to - * perform REST calls. + * The HTTP operations for DocumentationClientTextFormattings. */ - @ServiceInterface(name = "DocumentationClientTextFormattings", host = "{endpoint}") public interface TextFormattingsService { static TextFormattingsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("documentation.implementation.TextFormattingsServiceImpl"); - return (TextFormattingsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new TextFormattingsServiceImpl(pipeline); + } + + Response boldText(String endpoint, RequestContext requestContext); + + Response italicText(String endpoint, RequestContext requestContext); + + Response combinedFormatting(String endpoint, RequestContext requestContext); + } + + private static final class TextFormattingsServiceImpl implements TextFormattingsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(TextFormattingsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private TextFormattingsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response boldText(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/documentation/text-formatting/bold"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 italicText(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/documentation/text-formatting/italic"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/documentation/text-formatting/bold", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response boldText(@HostParam("endpoint") String endpoint, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/documentation/text-formatting/italic", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response italicText(@HostParam("endpoint") String endpoint, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/documentation/text-formatting/combined", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response combinedFormatting(@HostParam("endpoint") String endpoint, RequestContext requestContext); + @Override + public Response combinedFormatting(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/documentation/text-formatting/combined"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/array/implementation/PropertiesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/array/implementation/PropertiesImpl.java index c7c1dfdecd5..dfaba413024 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/array/implementation/PropertiesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/array/implementation/PropertiesImpl.java @@ -13,20 +13,23 @@ import encode.array.SpaceDelimitedEnumArrayProperty; import encode.array.SpaceDelimitedExtensibleEnumArrayProperty; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; /** * An instance of this class provides access to all the operations defined in Properties. @@ -59,139 +62,445 @@ public final class PropertiesImpl { } /** - * The interface defining all the services for ArrayClientProperties to be used by the proxy service to perform REST - * calls. + * The HTTP operations for ArrayClientProperties. */ - @ServiceInterface(name = "ArrayClientProperties", host = "{endpoint}") public interface PropertiesService { static PropertiesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("encode.array.implementation.PropertiesServiceImpl"); - return (PropertiesService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new PropertiesServiceImpl(pipeline); } - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/array/property/comma-delimited", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response commaDelimited(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") CommaDelimitedArrayProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/array/property/space-delimited", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response spaceDelimited(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") SpaceDelimitedArrayProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/array/property/pipe-delimited", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response pipeDelimited(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") PipeDelimitedArrayProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/array/property/newline-delimited", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response newlineDelimited(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") NewlineDelimitedArrayProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/array/property/enum/comma-delimited", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response enumCommaDelimited(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") CommaDelimitedEnumArrayProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/array/property/enum/space-delimited", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response enumSpaceDelimited(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") SpaceDelimitedEnumArrayProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/array/property/enum/pipe-delimited", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response enumPipeDelimited(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") PipeDelimitedEnumArrayProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/array/property/enum/newline-delimited", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response enumNewlineDelimited(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") NewlineDelimitedEnumArrayProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/array/property/extensible-enum/comma-delimited", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response extensibleEnumCommaDelimited( - @HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @HeaderParam("Accept") String accept, - @BodyParam("application/json") CommaDelimitedExtensibleEnumArrayProperty body, + Response commaDelimited(String endpoint, String contentType, String accept, + CommaDelimitedArrayProperty body, RequestContext requestContext); + + Response spaceDelimited(String endpoint, String contentType, String accept, + SpaceDelimitedArrayProperty body, RequestContext requestContext); + + Response pipeDelimited(String endpoint, String contentType, String accept, + PipeDelimitedArrayProperty body, RequestContext requestContext); + + Response newlineDelimited(String endpoint, String contentType, String accept, + NewlineDelimitedArrayProperty body, RequestContext requestContext); + + Response enumCommaDelimited(String endpoint, String contentType, String accept, + CommaDelimitedEnumArrayProperty body, RequestContext requestContext); + + Response enumSpaceDelimited(String endpoint, String contentType, String accept, + SpaceDelimitedEnumArrayProperty body, RequestContext requestContext); + + Response enumPipeDelimited(String endpoint, String contentType, String accept, + PipeDelimitedEnumArrayProperty body, RequestContext requestContext); + + Response enumNewlineDelimited(String endpoint, String contentType, + String accept, NewlineDelimitedEnumArrayProperty body, RequestContext requestContext); + + Response extensibleEnumCommaDelimited(String endpoint, + String contentType, String accept, CommaDelimitedExtensibleEnumArrayProperty body, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/array/property/extensible-enum/space-delimited", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response extensibleEnumSpaceDelimited( - @HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @HeaderParam("Accept") String accept, - @BodyParam("application/json") SpaceDelimitedExtensibleEnumArrayProperty body, + Response extensibleEnumSpaceDelimited(String endpoint, + String contentType, String accept, SpaceDelimitedExtensibleEnumArrayProperty body, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/array/property/extensible-enum/pipe-delimited", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response extensibleEnumPipeDelimited( - @HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @HeaderParam("Accept") String accept, - @BodyParam("application/json") PipeDelimitedExtensibleEnumArrayProperty body, + Response extensibleEnumPipeDelimited(String endpoint, + String contentType, String accept, PipeDelimitedExtensibleEnumArrayProperty body, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/array/property/extensible-enum/newline-delimited", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response extensibleEnumNewlineDelimited( - @HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @HeaderParam("Accept") String accept, - @BodyParam("application/json") NewlineDelimitedExtensibleEnumArrayProperty body, + Response extensibleEnumNewlineDelimited(String endpoint, + String contentType, String accept, NewlineDelimitedExtensibleEnumArrayProperty body, RequestContext requestContext); } + private static final class PropertiesServiceImpl implements PropertiesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(PropertiesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private PropertiesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response commaDelimited(String endpoint, String contentType, String accept, + CommaDelimitedArrayProperty body, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/encode/array/property/comma-delimited"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, CommaDelimitedArrayProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response spaceDelimited(String endpoint, String contentType, String accept, + SpaceDelimitedArrayProperty body, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/encode/array/property/space-delimited"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, SpaceDelimitedArrayProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response pipeDelimited(String endpoint, String contentType, String accept, + PipeDelimitedArrayProperty body, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/encode/array/property/pipe-delimited"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, PipeDelimitedArrayProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response newlineDelimited(String endpoint, String contentType, + String accept, NewlineDelimitedArrayProperty body, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/encode/array/property/newline-delimited"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, NewlineDelimitedArrayProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response enumCommaDelimited(String endpoint, String contentType, + String accept, CommaDelimitedEnumArrayProperty body, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/encode/array/property/enum/comma-delimited"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, CommaDelimitedEnumArrayProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response enumSpaceDelimited(String endpoint, String contentType, + String accept, SpaceDelimitedEnumArrayProperty body, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/encode/array/property/enum/space-delimited"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, SpaceDelimitedEnumArrayProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response enumPipeDelimited(String endpoint, String contentType, + String accept, PipeDelimitedEnumArrayProperty body, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/encode/array/property/enum/pipe-delimited"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, PipeDelimitedEnumArrayProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response enumNewlineDelimited(String endpoint, String contentType, + String accept, NewlineDelimitedEnumArrayProperty body, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/encode/array/property/enum/newline-delimited"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, NewlineDelimitedEnumArrayProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response extensibleEnumCommaDelimited(String endpoint, + String contentType, String accept, CommaDelimitedExtensibleEnumArrayProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/encode/array/property/extensible-enum/comma-delimited"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, CommaDelimitedExtensibleEnumArrayProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response extensibleEnumSpaceDelimited(String endpoint, + String contentType, String accept, SpaceDelimitedExtensibleEnumArrayProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/encode/array/property/extensible-enum/space-delimited"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, SpaceDelimitedExtensibleEnumArrayProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response extensibleEnumPipeDelimited(String endpoint, + String contentType, String accept, PipeDelimitedExtensibleEnumArrayProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/encode/array/property/extensible-enum/pipe-delimited"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, PipeDelimitedExtensibleEnumArrayProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response extensibleEnumNewlineDelimited(String endpoint, + String contentType, String accept, NewlineDelimitedExtensibleEnumArrayProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/encode/array/property/extensible-enum/newline-delimited"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, + NewlineDelimitedExtensibleEnumArrayProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } + } + /** * The commaDelimited operation. * diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/booleannamespace/implementation/PropertiesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/booleannamespace/implementation/PropertiesImpl.java index b9488a092de..dc3d318735d 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/booleannamespace/implementation/PropertiesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/booleannamespace/implementation/PropertiesImpl.java @@ -2,20 +2,23 @@ import encode.booleannamespace.property.BoolAsStringProperty; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; /** * An instance of this class provides access to all the operations defined in Properties. @@ -48,57 +51,170 @@ public final class PropertiesImpl { } /** - * The interface defining all the services for BooleanClientProperties to be used by the proxy service to perform - * REST calls. + * The HTTP operations for BooleanClientProperties. */ - @ServiceInterface(name = "BooleanClientProperties", host = "{endpoint}") public interface PropertiesService { static PropertiesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("encode.booleannamespace.implementation.PropertiesServiceImpl"); - return (PropertiesService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new PropertiesServiceImpl(pipeline); + } + + Response trueLower(String endpoint, String contentType, String accept, + BoolAsStringProperty value, RequestContext requestContext); + + Response falseLower(String endpoint, String contentType, String accept, + BoolAsStringProperty value, RequestContext requestContext); + + Response trueUpper(String endpoint, String contentType, String accept, + BoolAsStringProperty value, RequestContext requestContext); + + Response falseMixed(String endpoint, String contentType, String accept, + BoolAsStringProperty value, RequestContext requestContext); + } + + private static final class PropertiesServiceImpl implements PropertiesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(PropertiesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private PropertiesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response trueLower(String endpoint, String contentType, String accept, + BoolAsStringProperty value, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/encode/boolean/property/true-lower"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (value != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(value, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, BoolAsStringProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response falseLower(String endpoint, String contentType, String accept, + BoolAsStringProperty value, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/encode/boolean/property/false-lower"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (value != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(value, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, BoolAsStringProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response trueUpper(String endpoint, String contentType, String accept, + BoolAsStringProperty value, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/encode/boolean/property/true-upper"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (value != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(value, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, BoolAsStringProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response falseMixed(String endpoint, String contentType, String accept, + BoolAsStringProperty value, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/encode/boolean/property/false-mixed"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (value != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(value, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, BoolAsStringProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); } - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/boolean/property/true-lower", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response trueLower(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") BoolAsStringProperty value, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/boolean/property/false-lower", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response falseLower(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") BoolAsStringProperty value, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/boolean/property/true-upper", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response trueUpper(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") BoolAsStringProperty value, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/boolean/property/false-mixed", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response falseMixed(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") BoolAsStringProperty value, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/bytes/implementation/HeadersImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/bytes/implementation/HeadersImpl.java index e909dfc993a..32fd7b0c5b9 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/bytes/implementation/HeadersImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/bytes/implementation/HeadersImpl.java @@ -1,21 +1,21 @@ package encode.bytes.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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 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.Base64Uri; -import java.lang.reflect.InvocationTargetException; +import io.clientcore.core.utils.GeneratedCodeUtils; import java.util.Base64; import java.util.List; import java.util.stream.Collectors; @@ -51,53 +51,114 @@ public final class HeadersImpl { } /** - * The interface defining all the services for BytesClientHeaders to be used by the proxy service to perform REST - * calls. + * The HTTP operations for BytesClientHeaders. */ - @ServiceInterface(name = "BytesClientHeaders", host = "{endpoint}") public interface HeadersService { static HeadersService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("encode.bytes.implementation.HeadersServiceImpl"); - return (HeadersService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new HeadersServiceImpl(pipeline); + } + + Response defaultMethod(String endpoint, String value, RequestContext requestContext); + + Response base64(String endpoint, String value, RequestContext requestContext); + + Response base64url(String endpoint, Base64Uri value, RequestContext requestContext); + + Response base64urlArray(String endpoint, String value, RequestContext requestContext); + } + + private static final class HeadersServiceImpl implements HeadersService { + private static final ClientLogger HTTP_LOG = new ClientLogger(HeadersService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private HeadersServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response defaultMethod(String endpoint, String value, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/encode/bytes/header/default"); + if (value != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("value"), value); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 base64(String endpoint, String value, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/encode/bytes/header/base64"); + if (value != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("value"), value); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 base64url(String endpoint, Base64Uri value, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/encode/bytes/header/base64url"); + if (value != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("value"), String.valueOf(value)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/bytes/header/default", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response defaultMethod(@HostParam("endpoint") String endpoint, @HeaderParam("value") String value, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/bytes/header/base64", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response base64(@HostParam("endpoint") String endpoint, @HeaderParam("value") String value, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/bytes/header/base64url", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response base64url(@HostParam("endpoint") String endpoint, @HeaderParam("value") Base64Uri value, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/bytes/header/base64url-array", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response base64urlArray(@HostParam("endpoint") String endpoint, @HeaderParam("value") String value, - RequestContext requestContext); + @Override + public Response base64urlArray(String endpoint, String value, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/encode/bytes/header/base64url-array"); + if (value != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("value"), value); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/bytes/implementation/PropertiesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/bytes/implementation/PropertiesImpl.java index 572df90a61f..c9bd07f593f 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/bytes/implementation/PropertiesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/bytes/implementation/PropertiesImpl.java @@ -5,20 +5,23 @@ import encode.bytes.Base64urlBytesProperty; import encode.bytes.DefaultBytesProperty; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; /** * An instance of this class provides access to all the operations defined in Properties. @@ -51,57 +54,170 @@ public final class PropertiesImpl { } /** - * The interface defining all the services for BytesClientProperties to be used by the proxy service to perform REST - * calls. + * The HTTP operations for BytesClientProperties. */ - @ServiceInterface(name = "BytesClientProperties", host = "{endpoint}") public interface PropertiesService { static PropertiesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("encode.bytes.implementation.PropertiesServiceImpl"); - return (PropertiesService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new PropertiesServiceImpl(pipeline); + } + + Response defaultMethod(String endpoint, String contentType, String accept, + DefaultBytesProperty body, RequestContext requestContext); + + Response base64(String endpoint, String contentType, String accept, + Base64BytesProperty body, RequestContext requestContext); + + Response base64url(String endpoint, String contentType, String accept, + Base64urlBytesProperty body, RequestContext requestContext); + + Response base64urlArray(String endpoint, String contentType, String accept, + Base64urlArrayBytesProperty body, RequestContext requestContext); + } + + private static final class PropertiesServiceImpl implements PropertiesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(PropertiesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private PropertiesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response defaultMethod(String endpoint, String contentType, String accept, + DefaultBytesProperty body, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/encode/bytes/property/default"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, DefaultBytesProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response base64(String endpoint, String contentType, String accept, + Base64BytesProperty body, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/encode/bytes/property/base64"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, Base64BytesProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response base64url(String endpoint, String contentType, String accept, + Base64urlBytesProperty body, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/encode/bytes/property/base64url"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, Base64urlBytesProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response base64urlArray(String endpoint, String contentType, String accept, + Base64urlArrayBytesProperty body, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/encode/bytes/property/base64url-array"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, Base64urlArrayBytesProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); } - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/bytes/property/default", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response defaultMethod(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") DefaultBytesProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/bytes/property/base64", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response base64(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") Base64BytesProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/bytes/property/base64url", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response base64url(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") Base64urlBytesProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/bytes/property/base64url-array", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response base64urlArray(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") Base64urlArrayBytesProperty body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/bytes/implementation/QueriesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/bytes/implementation/QueriesImpl.java index e6580272ff0..b7b47717ed0 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/bytes/implementation/QueriesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/bytes/implementation/QueriesImpl.java @@ -1,21 +1,21 @@ package encode.bytes.implementation; 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.QueryParam; -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 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.Base64Uri; -import java.lang.reflect.InvocationTargetException; +import io.clientcore.core.utils.GeneratedCodeUtils; +import io.clientcore.core.utils.UriBuilder; import java.util.Base64; import java.util.List; import java.util.stream.Collectors; @@ -51,53 +51,106 @@ public final class QueriesImpl { } /** - * The interface defining all the services for BytesClientQueries to be used by the proxy service to perform REST - * calls. + * The HTTP operations for BytesClientQueries. */ - @ServiceInterface(name = "BytesClientQueries", host = "{endpoint}") public interface QueriesService { static QueriesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("encode.bytes.implementation.QueriesServiceImpl"); - return (QueriesService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new QueriesServiceImpl(pipeline); + } + + Response defaultMethod(String endpoint, String value, RequestContext requestContext); + + Response base64(String endpoint, String value, RequestContext requestContext); + + Response base64url(String endpoint, Base64Uri value, RequestContext requestContext); + + Response base64urlArray(String endpoint, String value, RequestContext requestContext); + } + + private static final class QueriesServiceImpl implements QueriesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(QueriesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private QueriesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response defaultMethod(String endpoint, String value, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/encode/bytes/query/default"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "value", true, value, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 base64(String endpoint, String value, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/encode/bytes/query/base64"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "value", true, value, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 base64url(String endpoint, Base64Uri value, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/encode/bytes/query/base64url"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "value", true, value, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/bytes/query/default", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response defaultMethod(@HostParam("endpoint") String endpoint, @QueryParam("value") String value, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/bytes/query/base64", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response base64(@HostParam("endpoint") String endpoint, @QueryParam("value") String value, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/bytes/query/base64url", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response base64url(@HostParam("endpoint") String endpoint, @QueryParam("value") Base64Uri value, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/bytes/query/base64url-array", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response base64urlArray(@HostParam("endpoint") String endpoint, @QueryParam("value") String value, - RequestContext requestContext); + @Override + public Response base64urlArray(String endpoint, String value, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/encode/bytes/query/base64url-array"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "value", true, value, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/bytes/implementation/RequestBodiesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/bytes/implementation/RequestBodiesImpl.java index 7d30a2085c9..482e55d531d 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/bytes/implementation/RequestBodiesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/bytes/implementation/RequestBodiesImpl.java @@ -1,22 +1,23 @@ package encode.bytes.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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 io.clientcore.core.instrumentation.logging.ClientLogger; import io.clientcore.core.models.binarydata.BinaryData; +import io.clientcore.core.serialization.SerializationFormat; +import io.clientcore.core.serialization.json.JsonSerializer; +import io.clientcore.core.serialization.xml.XmlSerializer; import io.clientcore.core.utils.Base64Uri; -import java.lang.reflect.InvocationTargetException; +import io.clientcore.core.utils.CoreUtils; +import io.clientcore.core.utils.GeneratedCodeUtils; /** * An instance of this class provides access to all the operations defined in RequestBodies. @@ -49,66 +50,163 @@ public final class RequestBodiesImpl { } /** - * The interface defining all the services for BytesClientRequestBodies to be used by the proxy service to perform - * REST calls. + * The HTTP operations for BytesClientRequestBodies. */ - @ServiceInterface(name = "BytesClientRequestBodies", host = "{endpoint}") public interface RequestBodiesService { static RequestBodiesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("encode.bytes.implementation.RequestBodiesServiceImpl"); - return (RequestBodiesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new RequestBodiesServiceImpl(pipeline); + } + + Response defaultMethod(String endpoint, String contentType, BinaryData value, long contentLength, + RequestContext requestContext); + + Response octetStream(String endpoint, String contentType, BinaryData value, long contentLength, + RequestContext requestContext); + + Response customContentType(String endpoint, String contentType, BinaryData value, long contentLength, + RequestContext requestContext); + + Response base64(String endpoint, String contentType, byte[] value, RequestContext requestContext); + + Response base64url(String endpoint, String contentType, Base64Uri value, RequestContext requestContext); + } + + private static final class RequestBodiesServiceImpl implements RequestBodiesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(RequestBodiesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private RequestBodiesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response defaultMethod(String endpoint, String contentType, BinaryData value, long contentLength, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/encode/bytes/body/request/default"); + httpRequest.getHeaders().add(HttpHeaderName.fromString("Content-Length"), String.valueOf(contentLength)); + if (value != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + if (value.getLength() != null) { + httpRequest.setBody(value); + } } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 octetStream(String endpoint, String contentType, BinaryData value, long contentLength, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/encode/bytes/body/request/octet-stream"); + httpRequest.getHeaders().add(HttpHeaderName.fromString("Content-Length"), String.valueOf(contentLength)); + if (value != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + if (value.getLength() != null) { + httpRequest.setBody(value); + } + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/bytes/body/request/default", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response defaultMethod(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("application/octet-stream") BinaryData value, - @HeaderParam("Content-Length") long contentLength, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/bytes/body/request/octet-stream", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response octetStream(@HostParam("endpoint") String endpoint, - @HeaderParam("content-type") String contentType, @BodyParam("application/octet-stream") BinaryData value, - @HeaderParam("Content-Length") long contentLength, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/bytes/body/request/custom-content-type", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response customContentType(@HostParam("endpoint") String endpoint, - @HeaderParam("content-type") String contentType, @BodyParam("image/png") BinaryData value, - @HeaderParam("Content-Length") long contentLength, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/bytes/body/request/base64", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response base64(@HostParam("endpoint") String endpoint, @HeaderParam("content-type") String contentType, - @BodyParam("application/json") byte[] value, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/bytes/body/request/base64url", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response base64url(@HostParam("endpoint") String endpoint, - @HeaderParam("content-type") String contentType, @BodyParam("application/json") Base64Uri value, - RequestContext requestContext); + @Override + public Response customContentType(String endpoint, String contentType, BinaryData value, + long contentLength, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/encode/bytes/body/request/custom-content-type"); + httpRequest.getHeaders().add(HttpHeaderName.fromString("Content-Length"), String.valueOf(contentLength)); + if (value != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + if (value.getLength() != null) { + httpRequest.setBody(value); + } + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 base64(String endpoint, String contentType, byte[] value, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/encode/bytes/body/request/base64"); + if (value != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(value, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 base64url(String endpoint, String contentType, Base64Uri value, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/encode/bytes/body/request/base64url"); + if (value != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(value, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/bytes/implementation/ResponseBodiesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/bytes/implementation/ResponseBodiesImpl.java index ea49f43b2c3..339c4913d19 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/bytes/implementation/ResponseBodiesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/bytes/implementation/ResponseBodiesImpl.java @@ -1,21 +1,21 @@ package encode.bytes.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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 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.Base64Uri; -import java.lang.reflect.InvocationTargetException; +import io.clientcore.core.utils.GeneratedCodeUtils; /** * An instance of this class provides access to all the operations defined in ResponseBodies. @@ -48,63 +48,137 @@ public final class ResponseBodiesImpl { } /** - * The interface defining all the services for BytesClientResponseBodies to be used by the proxy service to perform - * REST calls. + * The HTTP operations for BytesClientResponseBodies. */ - @ServiceInterface(name = "BytesClientResponseBodies", host = "{endpoint}") public interface ResponseBodiesService { static ResponseBodiesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("encode.bytes.implementation.ResponseBodiesServiceImpl"); - return (ResponseBodiesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new ResponseBodiesServiceImpl(pipeline); + } + + Response defaultMethod(String endpoint, String accept, RequestContext requestContext); + + Response octetStream(String endpoint, String accept, RequestContext requestContext); + + Response customContentType(String endpoint, String accept, RequestContext requestContext); + + Response base64(String endpoint, String accept, RequestContext requestContext); + + Response base64url(String endpoint, String accept, RequestContext requestContext); + } + + private static final class ResponseBodiesServiceImpl implements ResponseBodiesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ResponseBodiesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ResponseBodiesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response defaultMethod(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/encode/bytes/body/response/default"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return networkResponse; + } + + @Override + public Response octetStream(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/encode/bytes/body/response/octet-stream"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return networkResponse; + } + + @Override + public Response customContentType(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/encode/bytes/body/response/custom-content-type"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return networkResponse; + } + @Override + public Response base64(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/encode/bytes/body/response/base64"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response networkResponse = this.httpPipeline.send(httpRequest)) { + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + BinaryData responseBody = networkResponse.getValue(); + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + responseBody == null ? null : responseBody.toBytes()); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/bytes/body/response/default", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response defaultMethod(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/bytes/body/response/octet-stream", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response octetStream(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/bytes/body/response/custom-content-type", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response customContentType(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/bytes/body/response/base64", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response base64(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/bytes/body/response/base64url", - expectedStatusCodes = { 200 }, - returnValueWireType = Base64Uri.class) - @UnexpectedResponseExceptionDetail - Response base64url(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); + @Override + public Response base64url(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/encode/bytes/body/response/base64url"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response networkResponse = this.httpPipeline.send(httpRequest)) { + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + BinaryData responseBody = networkResponse.getValue(); + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + responseBody == null ? null : new Base64Uri(responseBody.toBytes()).decodedBytes()); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/datetime/implementation/HeadersImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/datetime/implementation/HeadersImpl.java index 165b6207817..902100a6ff4 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/datetime/implementation/HeadersImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/datetime/implementation/HeadersImpl.java @@ -1,22 +1,23 @@ package encode.datetime.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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 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.DateTimeRfc1123; -import java.lang.reflect.InvocationTargetException; +import io.clientcore.core.utils.GeneratedCodeUtils; import java.time.OffsetDateTime; +import java.time.format.DateTimeFormatter; import java.util.List; import java.util.stream.Collectors; @@ -51,61 +52,135 @@ public final class HeadersImpl { } /** - * The interface defining all the services for DatetimeClientHeaders to be used by the proxy service to perform REST - * calls. + * The HTTP operations for DatetimeClientHeaders. */ - @ServiceInterface(name = "DatetimeClientHeaders", host = "{endpoint}") public interface HeadersService { static HeadersService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("encode.datetime.implementation.HeadersServiceImpl"); - return (HeadersService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new HeadersServiceImpl(pipeline); + } + + Response defaultMethod(String endpoint, DateTimeRfc1123 value, RequestContext requestContext); + + Response rfc3339(String endpoint, OffsetDateTime value, RequestContext requestContext); + + Response rfc7231(String endpoint, DateTimeRfc1123 value, RequestContext requestContext); + + Response unixTimestamp(String endpoint, long value, RequestContext requestContext); + + Response unixTimestampArray(String endpoint, String value, RequestContext requestContext); + } + + private static final class HeadersServiceImpl implements HeadersService { + private static final ClientLogger HTTP_LOG = new ClientLogger(HeadersService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private HeadersServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response defaultMethod(String endpoint, DateTimeRfc1123 value, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/encode/datetime/header/default"); + if (value != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("value"), String.valueOf(value)); } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 rfc3339(String endpoint, OffsetDateTime value, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/encode/datetime/header/rfc3339"); + if (value != null) { + httpRequest.getHeaders() + .add(HttpHeaderName.fromString("value"), value.format(DateTimeFormatter.ISO_INSTANT)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 rfc7231(String endpoint, DateTimeRfc1123 value, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/encode/datetime/header/rfc7231"); + if (value != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("value"), String.valueOf(value)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 unixTimestamp(String endpoint, long value, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/encode/datetime/header/unix-timestamp"); + httpRequest.getHeaders().add(HttpHeaderName.fromString("value"), String.valueOf(value)); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/datetime/header/default", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response defaultMethod(@HostParam("endpoint") String endpoint, - @HeaderParam("value") DateTimeRfc1123 value, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/datetime/header/rfc3339", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response rfc3339(@HostParam("endpoint") String endpoint, @HeaderParam("value") OffsetDateTime value, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/datetime/header/rfc7231", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response rfc7231(@HostParam("endpoint") String endpoint, @HeaderParam("value") DateTimeRfc1123 value, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/datetime/header/unix-timestamp", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response unixTimestamp(@HostParam("endpoint") String endpoint, @HeaderParam("value") long value, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/datetime/header/unix-timestamp-array", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response unixTimestampArray(@HostParam("endpoint") String endpoint, @HeaderParam("value") String value, - RequestContext requestContext); + @Override + public Response unixTimestampArray(String endpoint, String value, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/encode/datetime/header/unix-timestamp-array"); + if (value != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("value"), value); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/datetime/implementation/PropertiesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/datetime/implementation/PropertiesImpl.java index 857c7a174a2..3e73addce62 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/datetime/implementation/PropertiesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/datetime/implementation/PropertiesImpl.java @@ -6,20 +6,23 @@ import encode.datetime.UnixTimestampArrayDatetimeProperty; import encode.datetime.UnixTimestampDatetimeProperty; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; /** * An instance of this class provides access to all the operations defined in Properties. @@ -52,66 +55,203 @@ public final class PropertiesImpl { } /** - * The interface defining all the services for DatetimeClientProperties to be used by the proxy service to perform - * REST calls. + * The HTTP operations for DatetimeClientProperties. */ - @ServiceInterface(name = "DatetimeClientProperties", host = "{endpoint}") public interface PropertiesService { static PropertiesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("encode.datetime.implementation.PropertiesServiceImpl"); - return (PropertiesService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new PropertiesServiceImpl(pipeline); + } + + Response defaultMethod(String endpoint, String contentType, String accept, + DefaultDatetimeProperty body, RequestContext requestContext); + + Response rfc3339(String endpoint, String contentType, String accept, + Rfc3339DatetimeProperty body, RequestContext requestContext); + + Response rfc7231(String endpoint, String contentType, String accept, + Rfc7231DatetimeProperty body, RequestContext requestContext); + + Response unixTimestamp(String endpoint, String contentType, String accept, + UnixTimestampDatetimeProperty body, RequestContext requestContext); + + Response unixTimestampArray(String endpoint, String contentType, + String accept, UnixTimestampArrayDatetimeProperty body, RequestContext requestContext); + } + + private static final class PropertiesServiceImpl implements PropertiesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(PropertiesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private PropertiesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response defaultMethod(String endpoint, String contentType, String accept, + DefaultDatetimeProperty body, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/encode/datetime/property/default"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, DefaultDatetimeProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response rfc3339(String endpoint, String contentType, String accept, + Rfc3339DatetimeProperty body, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/encode/datetime/property/rfc3339"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, Rfc3339DatetimeProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response rfc7231(String endpoint, String contentType, String accept, + Rfc7231DatetimeProperty body, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/encode/datetime/property/rfc7231"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, Rfc7231DatetimeProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); } - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/datetime/property/default", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response defaultMethod(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") DefaultDatetimeProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/datetime/property/rfc3339", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response rfc3339(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") Rfc3339DatetimeProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/datetime/property/rfc7231", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response rfc7231(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") Rfc7231DatetimeProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/datetime/property/unix-timestamp", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response unixTimestamp(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") UnixTimestampDatetimeProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/datetime/property/unix-timestamp-array", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response unixTimestampArray(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") UnixTimestampArrayDatetimeProperty body, RequestContext requestContext); + @Override + public Response unixTimestamp(String endpoint, String contentType, String accept, + UnixTimestampDatetimeProperty body, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/encode/datetime/property/unix-timestamp"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, UnixTimestampDatetimeProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response unixTimestampArray(String endpoint, String contentType, + String accept, UnixTimestampArrayDatetimeProperty body, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/encode/datetime/property/unix-timestamp-array"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, UnixTimestampArrayDatetimeProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/datetime/implementation/QueriesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/datetime/implementation/QueriesImpl.java index e9dfe30d7a2..c7213ec86f1 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/datetime/implementation/QueriesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/datetime/implementation/QueriesImpl.java @@ -1,21 +1,21 @@ package encode.datetime.implementation; 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.QueryParam; -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 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.DateTimeRfc1123; -import java.lang.reflect.InvocationTargetException; +import io.clientcore.core.utils.GeneratedCodeUtils; +import io.clientcore.core.utils.UriBuilder; import java.time.OffsetDateTime; import java.util.List; import java.util.stream.Collectors; @@ -51,61 +51,126 @@ public final class QueriesImpl { } /** - * The interface defining all the services for DatetimeClientQueries to be used by the proxy service to perform REST - * calls. + * The HTTP operations for DatetimeClientQueries. */ - @ServiceInterface(name = "DatetimeClientQueries", host = "{endpoint}") public interface QueriesService { static QueriesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("encode.datetime.implementation.QueriesServiceImpl"); - return (QueriesService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new QueriesServiceImpl(pipeline); + } + + Response defaultMethod(String endpoint, OffsetDateTime value, RequestContext requestContext); + + Response rfc3339(String endpoint, OffsetDateTime value, RequestContext requestContext); + + Response rfc7231(String endpoint, DateTimeRfc1123 value, RequestContext requestContext); + + Response unixTimestamp(String endpoint, long value, RequestContext requestContext); + + Response unixTimestampArray(String endpoint, String value, RequestContext requestContext); + } + + private static final class QueriesServiceImpl implements QueriesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(QueriesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private QueriesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response defaultMethod(String endpoint, OffsetDateTime value, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/encode/datetime/query/default"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "value", true, value, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 rfc3339(String endpoint, OffsetDateTime value, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/encode/datetime/query/rfc3339"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "value", true, value, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 rfc7231(String endpoint, DateTimeRfc1123 value, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/encode/datetime/query/rfc7231"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "value", true, value, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 unixTimestamp(String endpoint, long value, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/encode/datetime/query/unix-timestamp"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "value", true, value, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/datetime/query/default", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response defaultMethod(@HostParam("endpoint") String endpoint, @QueryParam("value") OffsetDateTime value, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/datetime/query/rfc3339", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response rfc3339(@HostParam("endpoint") String endpoint, @QueryParam("value") OffsetDateTime value, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/datetime/query/rfc7231", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response rfc7231(@HostParam("endpoint") String endpoint, @QueryParam("value") DateTimeRfc1123 value, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/datetime/query/unix-timestamp", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response unixTimestamp(@HostParam("endpoint") String endpoint, @QueryParam("value") long value, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/datetime/query/unix-timestamp-array", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response unixTimestampArray(@HostParam("endpoint") String endpoint, @QueryParam("value") String value, - RequestContext requestContext); + @Override + public Response unixTimestampArray(String endpoint, String value, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/encode/datetime/query/unix-timestamp-array"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "value", true, value, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/datetime/implementation/ResponseHeadersImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/datetime/implementation/ResponseHeadersImpl.java index b6b2a70e768..5f29a2b2e93 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/datetime/implementation/ResponseHeadersImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/datetime/implementation/ResponseHeadersImpl.java @@ -1,18 +1,19 @@ package encode.datetime.implementation; 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.GeneratedCodeUtils; /** * An instance of this class provides access to all the operations defined in ResponseHeaders. @@ -45,50 +46,102 @@ public final class ResponseHeadersImpl { } /** - * The interface defining all the services for DatetimeClientResponseHeaders to be used by the proxy service to - * perform REST calls. + * The HTTP operations for DatetimeClientResponseHeaders. */ - @ServiceInterface(name = "DatetimeClientResponseHeaders", host = "{endpoint}") public interface ResponseHeadersService { static ResponseHeadersService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("encode.datetime.implementation.ResponseHeadersServiceImpl"); - return (ResponseHeadersService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new ResponseHeadersServiceImpl(pipeline); + } + + Response defaultMethod(String endpoint, RequestContext requestContext); + + Response rfc3339(String endpoint, RequestContext requestContext); + + Response rfc7231(String endpoint, RequestContext requestContext); + + Response unixTimestamp(String endpoint, RequestContext requestContext); + } + + private static final class ResponseHeadersServiceImpl implements ResponseHeadersService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ResponseHeadersService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ResponseHeadersServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response defaultMethod(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/encode/datetime/responseheader/default"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 rfc3339(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/encode/datetime/responseheader/rfc3339"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 rfc7231(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/encode/datetime/responseheader/rfc7231"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/datetime/responseheader/default", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response defaultMethod(@HostParam("endpoint") String endpoint, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/datetime/responseheader/rfc3339", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response rfc3339(@HostParam("endpoint") String endpoint, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/datetime/responseheader/rfc7231", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response rfc7231(@HostParam("endpoint") String endpoint, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/datetime/responseheader/unix-timestamp", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response unixTimestamp(@HostParam("endpoint") String endpoint, RequestContext requestContext); + @Override + public Response unixTimestamp(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/encode/datetime/responseheader/unix-timestamp"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/duration/implementation/HeadersImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/duration/implementation/HeadersImpl.java index aebf57bc18d..05b043d9a5d 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/duration/implementation/HeadersImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/duration/implementation/HeadersImpl.java @@ -1,20 +1,20 @@ package encode.duration.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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 io.clientcore.core.instrumentation.logging.ClientLogger; import io.clientcore.core.models.binarydata.BinaryData; -import java.lang.reflect.InvocationTargetException; +import io.clientcore.core.serialization.json.JsonSerializer; +import io.clientcore.core.serialization.xml.XmlSerializer; +import io.clientcore.core.utils.GeneratedCodeUtils; import java.time.Duration; import java.util.List; import java.util.stream.Collectors; @@ -50,133 +50,316 @@ public final class HeadersImpl { } /** - * The interface defining all the services for DurationClientHeaders to be used by the proxy service to perform REST - * calls. + * The HTTP operations for DurationClientHeaders. */ - @ServiceInterface(name = "DurationClientHeaders", host = "{endpoint}") public interface HeadersService { static HeadersService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("encode.duration.implementation.HeadersServiceImpl"); - return (HeadersService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new HeadersServiceImpl(pipeline); + } + + Response defaultMethod(String endpoint, Duration duration, RequestContext requestContext); + + Response iso8601(String endpoint, Duration duration, RequestContext requestContext); + + Response iso8601Array(String endpoint, String duration, RequestContext requestContext); + + Response int32Seconds(String endpoint, long duration, RequestContext requestContext); + + Response int32SecondsLargerUnit(String endpoint, long duration, RequestContext requestContext); + + Response floatSeconds(String endpoint, double duration, RequestContext requestContext); + + Response floatSecondsLargerUnit(String endpoint, double duration, RequestContext requestContext); + + Response float64Seconds(String endpoint, double duration, RequestContext requestContext); + + Response int32Milliseconds(String endpoint, long duration, RequestContext requestContext); + + Response int32MillisecondsLargerUnit(String endpoint, long duration, RequestContext requestContext); + + Response floatMilliseconds(String endpoint, double duration, RequestContext requestContext); + + Response floatMillisecondsLargerUnit(String endpoint, double duration, RequestContext requestContext); + + Response float64Milliseconds(String endpoint, double duration, RequestContext requestContext); + + Response int32MillisecondsArray(String endpoint, String duration, RequestContext requestContext); + } + + private static final class HeadersServiceImpl implements HeadersService { + private static final ClientLogger HTTP_LOG = new ClientLogger(HeadersService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private HeadersServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response defaultMethod(String endpoint, Duration duration, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/encode/duration/header/default"); + if (duration != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("duration"), String.valueOf(duration)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 iso8601(String endpoint, Duration duration, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/encode/duration/header/iso8601"); + if (duration != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("duration"), String.valueOf(duration)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 iso8601Array(String endpoint, String duration, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/encode/duration/header/iso8601-array"); + if (duration != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("duration"), duration); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 int32Seconds(String endpoint, long duration, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/encode/duration/header/int32-seconds"); + httpRequest.getHeaders().add(HttpHeaderName.fromString("duration"), String.valueOf(duration)); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 int32SecondsLargerUnit(String endpoint, long duration, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/encode/duration/header/int32-seconds-larger-unit"); + httpRequest.getHeaders().add(HttpHeaderName.fromString("duration"), String.valueOf(duration)); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 floatSeconds(String endpoint, double duration, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/encode/duration/header/float-seconds"); + httpRequest.getHeaders().add(HttpHeaderName.fromString("duration"), String.valueOf(duration)); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 floatSecondsLargerUnit(String endpoint, double duration, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/encode/duration/header/float-seconds-larger-unit"); + httpRequest.getHeaders().add(HttpHeaderName.fromString("duration"), String.valueOf(duration)); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 float64Seconds(String endpoint, double duration, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/encode/duration/header/float64-seconds"); + httpRequest.getHeaders().add(HttpHeaderName.fromString("duration"), String.valueOf(duration)); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 int32Milliseconds(String endpoint, long duration, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/encode/duration/header/int32-milliseconds"); + httpRequest.getHeaders().add(HttpHeaderName.fromString("duration"), String.valueOf(duration)); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 int32MillisecondsLargerUnit(String endpoint, long duration, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/encode/duration/header/int32-milliseconds-larger-unit"); + httpRequest.getHeaders().add(HttpHeaderName.fromString("duration"), String.valueOf(duration)); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 floatMilliseconds(String endpoint, double duration, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/encode/duration/header/float-milliseconds"); + httpRequest.getHeaders().add(HttpHeaderName.fromString("duration"), String.valueOf(duration)); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 floatMillisecondsLargerUnit(String endpoint, double duration, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/encode/duration/header/float-milliseconds-larger-unit"); + httpRequest.getHeaders().add(HttpHeaderName.fromString("duration"), String.valueOf(duration)); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 float64Milliseconds(String endpoint, double duration, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/encode/duration/header/float64-milliseconds"); + httpRequest.getHeaders().add(HttpHeaderName.fromString("duration"), String.valueOf(duration)); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/duration/header/default", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response defaultMethod(@HostParam("endpoint") String endpoint, @HeaderParam("duration") Duration duration, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/duration/header/iso8601", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response iso8601(@HostParam("endpoint") String endpoint, @HeaderParam("duration") Duration duration, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/duration/header/iso8601-array", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response iso8601Array(@HostParam("endpoint") String endpoint, @HeaderParam("duration") String duration, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/duration/header/int32-seconds", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response int32Seconds(@HostParam("endpoint") String endpoint, @HeaderParam("duration") long duration, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/duration/header/int32-seconds-larger-unit", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response int32SecondsLargerUnit(@HostParam("endpoint") String endpoint, - @HeaderParam("duration") long duration, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/duration/header/float-seconds", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response floatSeconds(@HostParam("endpoint") String endpoint, @HeaderParam("duration") double duration, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/duration/header/float-seconds-larger-unit", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response floatSecondsLargerUnit(@HostParam("endpoint") String endpoint, - @HeaderParam("duration") double duration, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/duration/header/float64-seconds", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response float64Seconds(@HostParam("endpoint") String endpoint, @HeaderParam("duration") double duration, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/duration/header/int32-milliseconds", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response int32Milliseconds(@HostParam("endpoint") String endpoint, @HeaderParam("duration") long duration, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/duration/header/int32-milliseconds-larger-unit", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response int32MillisecondsLargerUnit(@HostParam("endpoint") String endpoint, - @HeaderParam("duration") long duration, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/duration/header/float-milliseconds", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response floatMilliseconds(@HostParam("endpoint") String endpoint, - @HeaderParam("duration") double duration, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/duration/header/float-milliseconds-larger-unit", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response floatMillisecondsLargerUnit(@HostParam("endpoint") String endpoint, - @HeaderParam("duration") double duration, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/duration/header/float64-milliseconds", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response float64Milliseconds(@HostParam("endpoint") String endpoint, - @HeaderParam("duration") double duration, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/duration/header/int32-milliseconds-array", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response int32MillisecondsArray(@HostParam("endpoint") String endpoint, - @HeaderParam("duration") String duration, RequestContext requestContext); + @Override + public Response int32MillisecondsArray(String endpoint, String duration, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/encode/duration/header/int32-milliseconds-array"); + if (duration != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("duration"), duration); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/duration/implementation/LossiesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/duration/implementation/LossiesImpl.java index 30d09ff9117..d5e55f08782 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/duration/implementation/LossiesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/duration/implementation/LossiesImpl.java @@ -1,19 +1,20 @@ package encode.duration.implementation; 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.QueryParam; -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.GeneratedCodeUtils; +import io.clientcore.core.utils.UriBuilder; import java.time.Duration; /** @@ -47,37 +48,66 @@ public final class LossiesImpl { } /** - * The interface defining all the services for DurationClientLossies to be used by the proxy service to perform REST - * calls. + * The HTTP operations for DurationClientLossies. */ - @ServiceInterface(name = "DurationClientLossies", host = "{endpoint}") public interface LossiesService { static LossiesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("encode.duration.implementation.LossiesServiceImpl"); - return (LossiesService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new LossiesServiceImpl(pipeline); + } + + Response intSeconds(String endpoint, long input, RequestContext requestContext); + + Response intMilliseconds(String endpoint, long input, RequestContext requestContext); + } + + private static final class LossiesServiceImpl implements LossiesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(LossiesService.class); + + private final HttpPipeline httpPipeline; + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private LossiesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/duration/lossy/int32-seconds", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response intSeconds(@HostParam("endpoint") String endpoint, @QueryParam("input") long input, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/duration/lossy/int32-milliseconds", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response intMilliseconds(@HostParam("endpoint") String endpoint, @QueryParam("input") long input, - RequestContext requestContext); + @Override + public Response intSeconds(String endpoint, long input, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/encode/duration/lossy/int32-seconds"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "input", true, input, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 intMilliseconds(String endpoint, long input, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/encode/duration/lossy/int32-milliseconds"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "input", true, input, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/duration/implementation/PropertiesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/duration/implementation/PropertiesImpl.java index ec5fa141019..1cedb87a7a2 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/duration/implementation/PropertiesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/duration/implementation/PropertiesImpl.java @@ -15,20 +15,23 @@ import encode.duration.property.Int32SecondsDurationProperty; import encode.duration.property.Int32SecondsLargerUnitDurationProperty; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; /** * An instance of this class provides access to all the operations defined in Properties. @@ -61,153 +64,511 @@ public final class PropertiesImpl { } /** - * The interface defining all the services for DurationClientProperties to be used by the proxy service to perform - * REST calls. + * The HTTP operations for DurationClientProperties. */ - @ServiceInterface(name = "DurationClientProperties", host = "{endpoint}") public interface PropertiesService { static PropertiesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("encode.duration.implementation.PropertiesServiceImpl"); - return (PropertiesService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new PropertiesServiceImpl(pipeline); } - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/duration/property/default", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response defaultMethod(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") DefaultDurationProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/duration/property/iso8601", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response iso8601(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") ISO8601DurationProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/duration/property/int32-seconds", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response int32Seconds(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") Int32SecondsDurationProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/duration/property/float-seconds", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response floatSeconds(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") FloatSecondsDurationProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/duration/property/float64-seconds", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response float64Seconds(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") Float64SecondsDurationProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/duration/property/int32-milliseconds", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response int32Milliseconds(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") Int32MillisecondsDurationProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/duration/property/float-milliseconds", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response floatMilliseconds(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") FloatMillisecondsDurationProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/duration/property/float64-milliseconds", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response float64Milliseconds(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") Float64MillisecondsDurationProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/duration/property/float-seconds-array", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response floatSecondsArray(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") FloatSecondsDurationArrayProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/duration/property/float-milliseconds-array", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response floatMillisecondsArray(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") FloatMillisecondsDurationArrayProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/duration/property/int32-seconds-larger-unit", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response int32SecondsLargerUnit(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") Int32SecondsLargerUnitDurationProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/duration/property/float-seconds-larger-unit", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response floatSecondsLargerUnit(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") FloatSecondsLargerUnitDurationProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/duration/property/int32-milliseconds-larger-unit", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response int32MillisecondsLargerUnit( - @HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @HeaderParam("Accept") String accept, - @BodyParam("application/json") Int32MillisecondsLargerUnitDurationProperty body, + Response defaultMethod(String endpoint, String contentType, String accept, + DefaultDurationProperty body, RequestContext requestContext); + + Response iso8601(String endpoint, String contentType, String accept, + ISO8601DurationProperty body, RequestContext requestContext); + + Response int32Seconds(String endpoint, String contentType, String accept, + Int32SecondsDurationProperty body, RequestContext requestContext); + + Response floatSeconds(String endpoint, String contentType, String accept, + FloatSecondsDurationProperty body, RequestContext requestContext); + + Response float64Seconds(String endpoint, String contentType, String accept, + Float64SecondsDurationProperty body, RequestContext requestContext); + + Response int32Milliseconds(String endpoint, String contentType, + String accept, Int32MillisecondsDurationProperty body, RequestContext requestContext); + + Response floatMilliseconds(String endpoint, String contentType, + String accept, FloatMillisecondsDurationProperty body, RequestContext requestContext); + + Response float64Milliseconds(String endpoint, String contentType, + String accept, Float64MillisecondsDurationProperty body, RequestContext requestContext); + + Response floatSecondsArray(String endpoint, String contentType, + String accept, FloatSecondsDurationArrayProperty body, RequestContext requestContext); + + Response floatMillisecondsArray(String endpoint, String contentType, + String accept, FloatMillisecondsDurationArrayProperty body, RequestContext requestContext); + + Response int32SecondsLargerUnit(String endpoint, String contentType, + String accept, Int32SecondsLargerUnitDurationProperty body, RequestContext requestContext); + + Response floatSecondsLargerUnit(String endpoint, String contentType, + String accept, FloatSecondsLargerUnitDurationProperty body, RequestContext requestContext); + + Response int32MillisecondsLargerUnit(String endpoint, + String contentType, String accept, Int32MillisecondsLargerUnitDurationProperty body, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/duration/property/float-milliseconds-larger-unit", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response floatMillisecondsLargerUnit( - @HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @HeaderParam("Accept") String accept, - @BodyParam("application/json") FloatMillisecondsLargerUnitDurationProperty body, + Response floatMillisecondsLargerUnit(String endpoint, + String contentType, String accept, FloatMillisecondsLargerUnitDurationProperty body, RequestContext requestContext); } + private static final class PropertiesServiceImpl implements PropertiesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(PropertiesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private PropertiesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response defaultMethod(String endpoint, String contentType, String accept, + DefaultDurationProperty body, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/encode/duration/property/default"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, DefaultDurationProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response iso8601(String endpoint, String contentType, String accept, + ISO8601DurationProperty body, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/encode/duration/property/iso8601"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, ISO8601DurationProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response int32Seconds(String endpoint, String contentType, String accept, + Int32SecondsDurationProperty body, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/encode/duration/property/int32-seconds"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, Int32SecondsDurationProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response floatSeconds(String endpoint, String contentType, String accept, + FloatSecondsDurationProperty body, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/encode/duration/property/float-seconds"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, FloatSecondsDurationProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response float64Seconds(String endpoint, String contentType, + String accept, Float64SecondsDurationProperty body, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/encode/duration/property/float64-seconds"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, Float64SecondsDurationProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response int32Milliseconds(String endpoint, String contentType, + String accept, Int32MillisecondsDurationProperty body, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/encode/duration/property/int32-milliseconds"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, Int32MillisecondsDurationProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response floatMilliseconds(String endpoint, String contentType, + String accept, FloatMillisecondsDurationProperty body, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/encode/duration/property/float-milliseconds"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, FloatMillisecondsDurationProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response float64Milliseconds(String endpoint, String contentType, + String accept, Float64MillisecondsDurationProperty body, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/encode/duration/property/float64-milliseconds"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, Float64MillisecondsDurationProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response floatSecondsArray(String endpoint, String contentType, + String accept, FloatSecondsDurationArrayProperty body, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/encode/duration/property/float-seconds-array"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, FloatSecondsDurationArrayProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response floatMillisecondsArray(String endpoint, + String contentType, String accept, FloatMillisecondsDurationArrayProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/encode/duration/property/float-milliseconds-array"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, FloatMillisecondsDurationArrayProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response int32SecondsLargerUnit(String endpoint, + String contentType, String accept, Int32SecondsLargerUnitDurationProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/encode/duration/property/int32-seconds-larger-unit"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, Int32SecondsLargerUnitDurationProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response floatSecondsLargerUnit(String endpoint, + String contentType, String accept, FloatSecondsLargerUnitDurationProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/encode/duration/property/float-seconds-larger-unit"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, FloatSecondsLargerUnitDurationProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response int32MillisecondsLargerUnit(String endpoint, + String contentType, String accept, Int32MillisecondsLargerUnitDurationProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/encode/duration/property/int32-milliseconds-larger-unit"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, + Int32MillisecondsLargerUnitDurationProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response floatMillisecondsLargerUnit(String endpoint, + String contentType, String accept, FloatMillisecondsLargerUnitDurationProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/encode/duration/property/float-milliseconds-larger-unit"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, + FloatMillisecondsLargerUnitDurationProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } + } + /** * The defaultMethod operation. * diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/duration/implementation/QueriesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/duration/implementation/QueriesImpl.java index e99d644ce8b..5a4a01035fc 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/duration/implementation/QueriesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/duration/implementation/QueriesImpl.java @@ -1,20 +1,20 @@ package encode.duration.implementation; 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.QueryParam; -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 io.clientcore.core.instrumentation.logging.ClientLogger; import io.clientcore.core.models.binarydata.BinaryData; -import java.lang.reflect.InvocationTargetException; +import io.clientcore.core.serialization.json.JsonSerializer; +import io.clientcore.core.serialization.xml.XmlSerializer; +import io.clientcore.core.utils.GeneratedCodeUtils; +import io.clientcore.core.utils.UriBuilder; import java.time.Duration; import java.util.List; import java.util.stream.Collectors; @@ -50,133 +50,309 @@ public final class QueriesImpl { } /** - * The interface defining all the services for DurationClientQueries to be used by the proxy service to perform REST - * calls. + * The HTTP operations for DurationClientQueries. */ - @ServiceInterface(name = "DurationClientQueries", host = "{endpoint}") public interface QueriesService { static QueriesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("encode.duration.implementation.QueriesServiceImpl"); - return (QueriesService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new QueriesServiceImpl(pipeline); + } + + Response defaultMethod(String endpoint, Duration input, RequestContext requestContext); + + Response iso8601(String endpoint, Duration input, RequestContext requestContext); + + Response int32Seconds(String endpoint, long input, RequestContext requestContext); + + Response int32SecondsLargerUnit(String endpoint, long input, RequestContext requestContext); + + Response floatSeconds(String endpoint, double input, RequestContext requestContext); + + Response floatSecondsLargerUnit(String endpoint, double input, RequestContext requestContext); + + Response float64Seconds(String endpoint, double input, RequestContext requestContext); + + Response int32Milliseconds(String endpoint, long input, RequestContext requestContext); + + Response int32MillisecondsLargerUnit(String endpoint, long input, RequestContext requestContext); + + Response floatMilliseconds(String endpoint, double input, RequestContext requestContext); + + Response floatMillisecondsLargerUnit(String endpoint, double input, RequestContext requestContext); + + Response float64Milliseconds(String endpoint, double input, RequestContext requestContext); + + Response int32SecondsArray(String endpoint, String input, RequestContext requestContext); + + Response int32MillisecondsArray(String endpoint, String input, RequestContext requestContext); + } + + private static final class QueriesServiceImpl implements QueriesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(QueriesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private QueriesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response defaultMethod(String endpoint, Duration input, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/encode/duration/query/default"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "input", true, input, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 iso8601(String endpoint, Duration input, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/encode/duration/query/iso8601"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "input", true, input, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 int32Seconds(String endpoint, long input, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/encode/duration/query/int32-seconds"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "input", true, input, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 int32SecondsLargerUnit(String endpoint, long input, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/encode/duration/query/int32-seconds-larger-unit"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "input", true, input, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 floatSeconds(String endpoint, double input, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/encode/duration/query/float-seconds"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "input", true, input, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 floatSecondsLargerUnit(String endpoint, double input, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/encode/duration/query/float-seconds-larger-unit"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "input", true, input, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 float64Seconds(String endpoint, double input, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/encode/duration/query/float64-seconds"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "input", true, input, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 int32Milliseconds(String endpoint, long input, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/encode/duration/query/int32-milliseconds"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "input", true, input, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 int32MillisecondsLargerUnit(String endpoint, long input, RequestContext requestContext) { + UriBuilder uriBuilder + = UriBuilder.parse(endpoint + "/encode/duration/query/int32-milliseconds-larger-unit"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "input", true, input, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 floatMilliseconds(String endpoint, double input, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/encode/duration/query/float-milliseconds"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "input", true, input, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 floatMillisecondsLargerUnit(String endpoint, double input, + RequestContext requestContext) { + UriBuilder uriBuilder + = UriBuilder.parse(endpoint + "/encode/duration/query/float-milliseconds-larger-unit"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "input", true, input, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/duration/query/default", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response defaultMethod(@HostParam("endpoint") String endpoint, @QueryParam("input") Duration input, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/duration/query/iso8601", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response iso8601(@HostParam("endpoint") String endpoint, @QueryParam("input") Duration input, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/duration/query/int32-seconds", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response int32Seconds(@HostParam("endpoint") String endpoint, @QueryParam("input") long input, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/duration/query/int32-seconds-larger-unit", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response int32SecondsLargerUnit(@HostParam("endpoint") String endpoint, @QueryParam("input") long input, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/duration/query/float-seconds", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response floatSeconds(@HostParam("endpoint") String endpoint, @QueryParam("input") double input, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/duration/query/float-seconds-larger-unit", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response floatSecondsLargerUnit(@HostParam("endpoint") String endpoint, @QueryParam("input") double input, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/duration/query/float64-seconds", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response float64Seconds(@HostParam("endpoint") String endpoint, @QueryParam("input") double input, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/duration/query/int32-milliseconds", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response int32Milliseconds(@HostParam("endpoint") String endpoint, @QueryParam("input") long input, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/duration/query/int32-milliseconds-larger-unit", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response int32MillisecondsLargerUnit(@HostParam("endpoint") String endpoint, - @QueryParam("input") long input, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/duration/query/float-milliseconds", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response floatMilliseconds(@HostParam("endpoint") String endpoint, @QueryParam("input") double input, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/duration/query/float-milliseconds-larger-unit", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response floatMillisecondsLargerUnit(@HostParam("endpoint") String endpoint, - @QueryParam("input") double input, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/duration/query/float64-milliseconds", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response float64Milliseconds(@HostParam("endpoint") String endpoint, @QueryParam("input") double input, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/duration/query/int32-seconds-array", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response int32SecondsArray(@HostParam("endpoint") String endpoint, @QueryParam("input") String input, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/encode/duration/query/int32-milliseconds-array", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response int32MillisecondsArray(@HostParam("endpoint") String endpoint, @QueryParam("input") String input, - RequestContext requestContext); + @Override + public Response float64Milliseconds(String endpoint, double input, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/encode/duration/query/float64-milliseconds"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "input", true, input, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 int32SecondsArray(String endpoint, String input, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/encode/duration/query/int32-seconds-array"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "input", true, input, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 int32MillisecondsArray(String endpoint, String input, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/encode/duration/query/int32-milliseconds-array"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "input", true, input, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/numeric/implementation/PropertiesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/numeric/implementation/PropertiesImpl.java index 48ebe64db32..480d32f4c1c 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/numeric/implementation/PropertiesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/numeric/implementation/PropertiesImpl.java @@ -4,20 +4,23 @@ import encode.numeric.property.Uint32AsStringProperty; import encode.numeric.property.Uint8AsStringProperty; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; /** * An instance of this class provides access to all the operations defined in Properties. @@ -50,48 +53,137 @@ public final class PropertiesImpl { } /** - * The interface defining all the services for NumericClientProperties to be used by the proxy service to perform - * REST calls. + * The HTTP operations for NumericClientProperties. */ - @ServiceInterface(name = "NumericClientProperties", host = "{endpoint}") public interface PropertiesService { static PropertiesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("encode.numeric.implementation.PropertiesServiceImpl"); - return (PropertiesService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new PropertiesServiceImpl(pipeline); + } + + Response safeintAsString(String endpoint, String contentType, String accept, + SafeintAsStringProperty value, RequestContext requestContext); + + Response uint32AsStringOptional(String endpoint, String contentType, String accept, + Uint32AsStringProperty value, RequestContext requestContext); + + Response uint8AsString(String endpoint, String contentType, String accept, + Uint8AsStringProperty value, RequestContext requestContext); + } + + private static final class PropertiesServiceImpl implements PropertiesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(PropertiesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private PropertiesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response safeintAsString(String endpoint, String contentType, String accept, + SafeintAsStringProperty value, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/encode/numeric/property/safeint"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (value != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(value, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, SafeintAsStringProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response uint32AsStringOptional(String endpoint, String contentType, + String accept, Uint32AsStringProperty value, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/encode/numeric/property/uint32"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (value != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(value, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, Uint32AsStringProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); } - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/numeric/property/safeint", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response safeintAsString(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") SafeintAsStringProperty value, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/numeric/property/uint32", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response uint32AsStringOptional(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") Uint32AsStringProperty value, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/encode/numeric/property/uint8", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response uint8AsString(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") Uint8AsStringProperty value, RequestContext requestContext); + @Override + public Response uint8AsString(String endpoint, String contentType, String accept, + Uint8AsStringProperty value, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/encode/numeric/property/uint8"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (value != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(value, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, Uint8AsStringProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/basic/implementation/ExplicitBodiesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/basic/implementation/ExplicitBodiesImpl.java index ad6398b2c70..bf7ccce7a63 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/basic/implementation/ExplicitBodiesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/basic/implementation/ExplicitBodiesImpl.java @@ -1,20 +1,22 @@ package parameters.basic.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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 parameters.basic.explicitbody.User; /** @@ -48,30 +50,52 @@ public final class ExplicitBodiesImpl { } /** - * The interface defining all the services for BasicClientExplicitBodies to be used by the proxy service to perform - * REST calls. + * The HTTP operations for BasicClientExplicitBodies. */ - @ServiceInterface(name = "BasicClientExplicitBodies", host = "{endpoint}") public interface ExplicitBodiesService { static ExplicitBodiesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("parameters.basic.implementation.ExplicitBodiesServiceImpl"); - return (ExplicitBodiesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new ExplicitBodiesServiceImpl(pipeline); + } + + Response simple(String endpoint, String contentType, User body, RequestContext requestContext); + } + + private static final class ExplicitBodiesServiceImpl implements ExplicitBodiesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ExplicitBodiesService.class); + + private final HttpPipeline httpPipeline; + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ExplicitBodiesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/parameters/basic/explicit-body/simple", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response simple(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") User body, RequestContext requestContext); + @Override + public Response simple(String endpoint, String contentType, User body, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/parameters/basic/explicit-body/simple"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/basic/implementation/ImplicitBodiesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/basic/implementation/ImplicitBodiesImpl.java index ac7c2c454d1..f2539697def 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/basic/implementation/ImplicitBodiesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/basic/implementation/ImplicitBodiesImpl.java @@ -1,20 +1,22 @@ package parameters.basic.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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 parameters.basic.implicitbody.implementation.SimpleRequest; /** @@ -48,30 +50,54 @@ public final class ImplicitBodiesImpl { } /** - * The interface defining all the services for BasicClientImplicitBodies to be used by the proxy service to perform - * REST calls. + * The HTTP operations for BasicClientImplicitBodies. */ - @ServiceInterface(name = "BasicClientImplicitBodies", host = "{endpoint}") public interface ImplicitBodiesService { static ImplicitBodiesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("parameters.basic.implementation.ImplicitBodiesServiceImpl"); - return (ImplicitBodiesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new ImplicitBodiesServiceImpl(pipeline); + } + + Response simple(String endpoint, String contentType, SimpleRequest simpleRequest, + RequestContext requestContext); + } + + private static final class ImplicitBodiesServiceImpl implements ImplicitBodiesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ImplicitBodiesService.class); + + private final HttpPipeline httpPipeline; + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ImplicitBodiesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/parameters/basic/implicit-body/simple", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response simple(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") SimpleRequest simpleRequest, RequestContext requestContext); + @Override + public Response simple(String endpoint, String contentType, SimpleRequest simpleRequest, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/parameters/basic/implicit-body/simple"); + if (simpleRequest != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(simpleRequest, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/bodyoptionality/implementation/BodyOptionalityClientImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/bodyoptionality/implementation/BodyOptionalityClientImpl.java index 483f956ad63..5a1335d9945 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/bodyoptionality/implementation/BodyOptionalityClientImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/bodyoptionality/implementation/BodyOptionalityClientImpl.java @@ -1,20 +1,22 @@ package parameters.bodyoptionality.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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 parameters.bodyoptionality.BodyModel; /** @@ -98,43 +100,84 @@ public BodyOptionalityClientImpl(HttpPipeline httpPipeline, Instrumentation inst } /** - * The interface defining all the services for BodyOptionalityClient to be used by the proxy service to perform REST - * calls. + * The HTTP operations for BodyOptionalityClient. */ - @ServiceInterface(name = "BodyOptionalityClient", host = "{endpoint}") public interface BodyOptionalityClientService { static BodyOptionalityClientService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz - = Class.forName("parameters.bodyoptionality.implementation.BodyOptionalityClientServiceImpl"); - return (BodyOptionalityClientService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new BodyOptionalityClientServiceImpl(pipeline); } - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/parameters/body-optionality/required-explicit", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response requiredExplicit(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") BodyModel body, + Response requiredExplicit(String endpoint, String contentType, BodyModel body, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/parameters/body-optionality/required-implicit", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response requiredImplicit(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") BodyModel bodyModel, + Response requiredImplicit(String endpoint, String contentType, BodyModel bodyModel, RequestContext requestContext); } + private static final class BodyOptionalityClientServiceImpl implements BodyOptionalityClientService { + private static final ClientLogger HTTP_LOG = new ClientLogger(BodyOptionalityClientService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private BodyOptionalityClientServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response requiredExplicit(String endpoint, String contentType, BodyModel body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/parameters/body-optionality/required-explicit"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 requiredImplicit(String endpoint, String contentType, BodyModel bodyModel, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/parameters/body-optionality/required-implicit"); + if (bodyModel != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(bodyModel, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + } + /** * The requiredExplicit operation. * diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/bodyoptionality/implementation/OptionalExplicitsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/bodyoptionality/implementation/OptionalExplicitsImpl.java index 024e9407d03..1533e9bec8b 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/bodyoptionality/implementation/OptionalExplicitsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/bodyoptionality/implementation/OptionalExplicitsImpl.java @@ -1,19 +1,22 @@ package parameters.bodyoptionality.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -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.HttpHeaderName; 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.SerializationFormat; +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 parameters.bodyoptionality.BodyModel; /** @@ -47,39 +50,78 @@ public final class OptionalExplicitsImpl { } /** - * The interface defining all the services for BodyOptionalityClientOptionalExplicits to be used by the proxy - * service to perform REST calls. + * The HTTP operations for BodyOptionalityClientOptionalExplicits. */ - @ServiceInterface(name = "BodyOptionalityClientOptionalExplicits", host = "{endpoint}") public interface OptionalExplicitsService { static OptionalExplicitsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz - = Class.forName("parameters.bodyoptionality.implementation.OptionalExplicitsServiceImpl"); - return (OptionalExplicitsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new OptionalExplicitsServiceImpl(pipeline); + } + + Response set(String endpoint, BodyModel body, RequestContext requestContext); + + Response omit(String endpoint, BodyModel body, RequestContext requestContext); + } + + private static final class OptionalExplicitsServiceImpl implements OptionalExplicitsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(OptionalExplicitsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private OptionalExplicitsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response set(String endpoint, BodyModel body, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/parameters/body-optionality/optional-explicit/set"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, "application/json"); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/parameters/body-optionality/optional-explicit/set", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response set(@HostParam("endpoint") String endpoint, @BodyParam("application/json") BodyModel body, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/parameters/body-optionality/optional-explicit/omit", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response omit(@HostParam("endpoint") String endpoint, @BodyParam("application/json") BodyModel body, - RequestContext requestContext); + @Override + public Response omit(String endpoint, BodyModel body, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/parameters/body-optionality/optional-explicit/omit"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, "application/json"); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/bodyroot/implementation/BodyRootClientImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/bodyroot/implementation/BodyRootClientImpl.java index 51f1961f020..dcd30be65e1 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/bodyroot/implementation/BodyRootClientImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/bodyroot/implementation/BodyRootClientImpl.java @@ -1,20 +1,22 @@ package parameters.bodyroot.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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 parameters.bodyroot.BodyRootModel; /** @@ -83,29 +85,54 @@ public BodyRootClientImpl(HttpPipeline httpPipeline, Instrumentation instrumenta } /** - * The interface defining all the services for BodyRootClient to be used by the proxy service to perform REST calls. + * The HTTP operations for BodyRootClient. */ - @ServiceInterface(name = "BodyRootClient", host = "{endpoint}") public interface BodyRootClientService { static BodyRootClientService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("parameters.bodyroot.implementation.BodyRootClientServiceImpl"); - return (BodyRootClientService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new BodyRootClientServiceImpl(pipeline); + } + + Response nested(String endpoint, String contentType, BodyRootModel bodyRootParameters, + RequestContext requestContext); + } + + private static final class BodyRootClientServiceImpl implements BodyRootClientService { + private static final ClientLogger HTTP_LOG = new ClientLogger(BodyRootClientService.class); + + private final HttpPipeline httpPipeline; + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private BodyRootClientServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/parameters/body-root/nested", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response nested(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") BodyRootModel bodyRootParameters, RequestContext requestContext); + @Override + public Response nested(String endpoint, String contentType, BodyRootModel bodyRootParameters, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/parameters/body-root/nested"); + if (bodyRootParameters != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(bodyRootParameters, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/collectionformat/implementation/HeadersImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/collectionformat/implementation/HeadersImpl.java index 35e8adf7449..931d6a8bb3c 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/collectionformat/implementation/HeadersImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/collectionformat/implementation/HeadersImpl.java @@ -1,19 +1,20 @@ package parameters.collectionformat.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.GeneratedCodeUtils; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; @@ -49,29 +50,48 @@ public final class HeadersImpl { } /** - * The interface defining all the services for CollectionFormatClientHeaders to be used by the proxy service to - * perform REST calls. + * The HTTP operations for CollectionFormatClientHeaders. */ - @ServiceInterface(name = "CollectionFormatClientHeaders", host = "{endpoint}") public interface HeadersService { static HeadersService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("parameters.collectionformat.implementation.HeadersServiceImpl"); - return (HeadersService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new HeadersServiceImpl(pipeline); + } + + Response csv(String endpoint, String colors, RequestContext requestContext); + } + + private static final class HeadersServiceImpl implements HeadersService { + private static final ClientLogger HTTP_LOG = new ClientLogger(HeadersService.class); + + private final HttpPipeline httpPipeline; + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private HeadersServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/parameters/collection-format/header/csv", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response csv(@HostParam("endpoint") String endpoint, @HeaderParam("colors") String colors, - RequestContext requestContext); + @Override + public Response csv(String endpoint, String colors, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/parameters/collection-format/header/csv"); + if (colors != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("colors"), colors); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/collectionformat/implementation/QueriesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/collectionformat/implementation/QueriesImpl.java index 495e262a8e1..6575770837f 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/collectionformat/implementation/QueriesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/collectionformat/implementation/QueriesImpl.java @@ -1,19 +1,20 @@ package parameters.collectionformat.implementation; 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.QueryParam; -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.GeneratedCodeUtils; +import io.clientcore.core.utils.UriBuilder; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; @@ -49,54 +50,106 @@ public final class QueriesImpl { } /** - * The interface defining all the services for CollectionFormatClientQueries to be used by the proxy service to - * perform REST calls. + * The HTTP operations for CollectionFormatClientQueries. */ - @ServiceInterface(name = "CollectionFormatClientQueries", host = "{endpoint}") public interface QueriesService { static QueriesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("parameters.collectionformat.implementation.QueriesServiceImpl"); - return (QueriesService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new QueriesServiceImpl(pipeline); + } + + Response multi(String endpoint, List colors, RequestContext requestContext); + + Response ssv(String endpoint, String colors, RequestContext requestContext); + + Response pipes(String endpoint, String colors, RequestContext requestContext); + + Response csv(String endpoint, String colors, RequestContext requestContext); + } + + private static final class QueriesServiceImpl implements QueriesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(QueriesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private QueriesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response multi(String endpoint, List colors, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/parameters/collection-format/query/multi"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "colors", true, colors, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 ssv(String endpoint, String colors, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/parameters/collection-format/query/ssv"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "colors", true, colors, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 pipes(String endpoint, String colors, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/parameters/collection-format/query/pipes"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "colors", true, colors, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/parameters/collection-format/query/multi", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response multi(@HostParam("endpoint") String endpoint, - @QueryParam(value = "colors", multipleQueryParams = true) List colors, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/parameters/collection-format/query/ssv", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response ssv(@HostParam("endpoint") String endpoint, @QueryParam("colors") String colors, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/parameters/collection-format/query/pipes", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response pipes(@HostParam("endpoint") String endpoint, @QueryParam("colors") String colors, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/parameters/collection-format/query/csv", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response csv(@HostParam("endpoint") String endpoint, @QueryParam("colors") String colors, - RequestContext requestContext); + @Override + public Response csv(String endpoint, String colors, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/parameters/collection-format/query/csv"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "colors", true, colors, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/path/implementation/PathClientImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/path/implementation/PathClientImpl.java index 2b34aa8ac05..8c36a0b3fdf 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/path/implementation/PathClientImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/path/implementation/PathClientImpl.java @@ -1,19 +1,20 @@ package parameters.path.implementation; 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.PathParam; -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.implementation.utils.UriEscapers; 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.GeneratedCodeUtils; /** * Initializes a new instance of the PathClient type. @@ -81,36 +82,64 @@ public PathClientImpl(HttpPipeline httpPipeline, Instrumentation instrumentation } /** - * The interface defining all the services for PathClient to be used by the proxy service to perform REST calls. + * The HTTP operations for PathClient. */ - @ServiceInterface(name = "PathClient", host = "{endpoint}") public interface PathClientService { static PathClientService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("parameters.path.implementation.PathClientServiceImpl"); - return (PathClientService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new PathClientServiceImpl(pipeline); + } + + Response normal(String endpoint, String name, RequestContext requestContext); + + Response optional(String endpoint, String name, RequestContext requestContext); + } + + private static final class PathClientServiceImpl implements PathClientService { + private static final ClientLogger HTTP_LOG = new ClientLogger(PathClientService.class); + + private final HttpPipeline httpPipeline; + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private PathClientServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/parameters/path/normal/{name}", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response normal(@HostParam("endpoint") String endpoint, @PathParam("name") String name, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/parameters/path/optional{name}", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response optional(@HostParam("endpoint") String endpoint, @PathParam("name") String name, - RequestContext requestContext); + @Override + public Response normal(String endpoint, String name, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/parameters/path/normal/" + UriEscapers.PATH_ESCAPER.escape(name)); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 optional(String endpoint, String name, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/parameters/path/optional" + UriEscapers.PATH_ESCAPER.escape(name)); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/query/implementation/ConstantsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/query/implementation/ConstantsImpl.java index 5f8668684b5..fd071c07632 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/query/implementation/ConstantsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/query/implementation/ConstantsImpl.java @@ -1,19 +1,20 @@ package parameters.query.implementation; 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.QueryParam; -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.GeneratedCodeUtils; +import io.clientcore.core.utils.UriBuilder; /** * An instance of this class provides access to all the operations defined in Constants. @@ -46,29 +47,46 @@ public final class ConstantsImpl { } /** - * The interface defining all the services for QueryClientConstants to be used by the proxy service to perform REST - * calls. + * The HTTP operations for QueryClientConstants. */ - @ServiceInterface(name = "QueryClientConstants", host = "{endpoint}") public interface ConstantsService { static ConstantsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("parameters.query.implementation.ConstantsServiceImpl"); - return (ConstantsService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new ConstantsServiceImpl(pipeline); + } + + Response post(String endpoint, String queryParam, RequestContext requestContext); + } + + private static final class ConstantsServiceImpl implements ConstantsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ConstantsService.class); + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ConstantsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/parameters/query/constant", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response post(@HostParam("endpoint") String endpoint, @QueryParam("queryParam") String queryParam, - RequestContext requestContext); + @Override + public Response post(String endpoint, String queryParam, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/parameters/query/constant"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "queryParam", true, queryParam, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/query/implementation/SpecialCharsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/query/implementation/SpecialCharsImpl.java index 2539ef0aa75..e1f25a2b53f 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/query/implementation/SpecialCharsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/query/implementation/SpecialCharsImpl.java @@ -1,19 +1,20 @@ package parameters.query.implementation; 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.QueryParam; -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.GeneratedCodeUtils; +import io.clientcore.core.utils.UriBuilder; /** * An instance of this class provides access to all the operations defined in SpecialChars. @@ -46,30 +47,46 @@ public final class SpecialCharsImpl { } /** - * The interface defining all the services for QueryClientSpecialChars to be used by the proxy service to perform - * REST calls. + * The HTTP operations for QueryClientSpecialChars. */ - @ServiceInterface(name = "QueryClientSpecialChars", host = "{endpoint}") public interface SpecialCharsService { static SpecialCharsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("parameters.query.implementation.SpecialCharsServiceImpl"); - return (SpecialCharsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new SpecialCharsServiceImpl(pipeline); + } + + Response dollarSign(String endpoint, String filter, RequestContext requestContext); + } + + private static final class SpecialCharsServiceImpl implements SpecialCharsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(SpecialCharsService.class); + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private SpecialCharsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/parameters/query/special-char/dollar-sign", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response dollarSign(@HostParam("endpoint") String endpoint, @QueryParam("$filter") String filter, - RequestContext requestContext); + @Override + public Response dollarSign(String endpoint, String filter, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/parameters/query/special-char/dollar-sign"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "$filter", true, filter, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/spread/implementation/AliasImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/spread/implementation/AliasImpl.java index 92aee7d9ab4..a2565441b32 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/spread/implementation/AliasImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/spread/implementation/AliasImpl.java @@ -1,21 +1,23 @@ package parameters.spread.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -import io.clientcore.core.http.annotations.HostParam; -import io.clientcore.core.http.annotations.HttpRequestInformation; -import io.clientcore.core.http.annotations.PathParam; -import io.clientcore.core.http.annotations.UnexpectedResponseExceptionDetail; +import io.clientcore.core.http.models.HttpHeaderName; 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.implementation.utils.UriEscapers; 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.SerializationFormat; +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.List; import parameters.spread.alias.implementation.SpreadAsRequestBodyRequest; @@ -50,73 +52,190 @@ public final class AliasImpl { } /** - * The interface defining all the services for SpreadClientAlias to be used by the proxy service to perform REST - * calls. + * The HTTP operations for SpreadClientAlias. */ - @ServiceInterface(name = "SpreadClientAlias", host = "{endpoint}") public interface AliasService { static AliasService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("parameters.spread.implementation.AliasServiceImpl"); - return (AliasService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new AliasServiceImpl(pipeline); } - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/parameters/spread/alias/request-body", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response spreadAsRequestBody(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") SpreadAsRequestBodyRequest spreadAsRequestBodyRequest, - RequestContext requestContext); + Response spreadAsRequestBody(String endpoint, String contentType, + SpreadAsRequestBodyRequest spreadAsRequestBodyRequest, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/parameters/spread/alias/inner-model-parameter/{id}", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response spreadParameterWithInnerModel(@HostParam("endpoint") String endpoint, @PathParam("id") String id, - @HeaderParam("x-ms-test-header") String xMsTestHeader, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") SpreadParameterWithInnerModelRequest spreadParameterWithInnerModelRequest, + Response spreadParameterWithInnerModel(String endpoint, String id, String xMsTestHeader, + String contentType, SpreadParameterWithInnerModelRequest spreadParameterWithInnerModelRequest, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/parameters/spread/alias/request-parameter/{id}", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response spreadAsRequestParameter(@HostParam("endpoint") String endpoint, @PathParam("id") String id, - @HeaderParam("x-ms-test-header") String xMsTestHeader, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") SpreadAsRequestParameterRequest spreadAsRequestParameterRequest, - RequestContext requestContext); + Response spreadAsRequestParameter(String endpoint, String id, String xMsTestHeader, String contentType, + SpreadAsRequestParameterRequest spreadAsRequestParameterRequest, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/parameters/spread/alias/multiple-parameters/{id}", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response spreadWithMultipleParameters(@HostParam("endpoint") String endpoint, @PathParam("id") String id, - @HeaderParam("x-ms-test-header") String xMsTestHeader, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") SpreadWithMultipleParametersRequest spreadWithMultipleParametersRequest, + Response spreadWithMultipleParameters(String endpoint, String id, String xMsTestHeader, + String contentType, SpreadWithMultipleParametersRequest spreadWithMultipleParametersRequest, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/parameters/spread/alias/inner-alias-parameter/{id}", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response spreadParameterWithInnerAlias(@HostParam("endpoint") String endpoint, @PathParam("id") String id, - @HeaderParam("x-ms-test-header") String xMsTestHeader, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") SpreadParameterWithInnerAliasRequest spreadParameterWithInnerAliasRequest, + Response spreadParameterWithInnerAlias(String endpoint, String id, String xMsTestHeader, + String contentType, SpreadParameterWithInnerAliasRequest spreadParameterWithInnerAliasRequest, RequestContext requestContext); } + private static final class AliasServiceImpl implements AliasService { + private static final ClientLogger HTTP_LOG = new ClientLogger(AliasService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private AliasServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response spreadAsRequestBody(String endpoint, String contentType, + SpreadAsRequestBodyRequest spreadAsRequestBodyRequest, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/parameters/spread/alias/request-body"); + if (spreadAsRequestBodyRequest != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(spreadAsRequestBodyRequest, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 spreadParameterWithInnerModel(String endpoint, String id, String xMsTestHeader, + String contentType, SpreadParameterWithInnerModelRequest spreadParameterWithInnerModelRequest, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri( + endpoint + "/parameters/spread/alias/inner-model-parameter/" + UriEscapers.PATH_ESCAPER.escape(id)); + if (xMsTestHeader != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("x-ms-test-header"), xMsTestHeader); + } + if (spreadParameterWithInnerModelRequest != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(spreadParameterWithInnerModelRequest, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 spreadAsRequestParameter(String endpoint, String id, String xMsTestHeader, + String contentType, SpreadAsRequestParameterRequest spreadAsRequestParameterRequest, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/parameters/spread/alias/request-parameter/" + UriEscapers.PATH_ESCAPER.escape(id)); + if (xMsTestHeader != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("x-ms-test-header"), xMsTestHeader); + } + if (spreadAsRequestParameterRequest != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(spreadAsRequestParameterRequest, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 spreadWithMultipleParameters(String endpoint, String id, String xMsTestHeader, + String contentType, SpreadWithMultipleParametersRequest spreadWithMultipleParametersRequest, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri( + endpoint + "/parameters/spread/alias/multiple-parameters/" + UriEscapers.PATH_ESCAPER.escape(id)); + if (xMsTestHeader != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("x-ms-test-header"), xMsTestHeader); + } + if (spreadWithMultipleParametersRequest != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(spreadWithMultipleParametersRequest, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 spreadParameterWithInnerAlias(String endpoint, String id, String xMsTestHeader, + String contentType, SpreadParameterWithInnerAliasRequest spreadParameterWithInnerAliasRequest, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri( + endpoint + "/parameters/spread/alias/inner-alias-parameter/" + UriEscapers.PATH_ESCAPER.escape(id)); + if (xMsTestHeader != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("x-ms-test-header"), xMsTestHeader); + } + if (spreadParameterWithInnerAliasRequest != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(spreadParameterWithInnerAliasRequest, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + } + /** * The spreadAsRequestBody operation. * diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/spread/implementation/ModelsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/spread/implementation/ModelsImpl.java index 4e2c4d5bb71..c033879f8fb 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/spread/implementation/ModelsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/parameters/spread/implementation/ModelsImpl.java @@ -1,21 +1,23 @@ package parameters.spread.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -import io.clientcore.core.http.annotations.HostParam; -import io.clientcore.core.http.annotations.HttpRequestInformation; -import io.clientcore.core.http.annotations.PathParam; -import io.clientcore.core.http.annotations.UnexpectedResponseExceptionDetail; +import io.clientcore.core.http.models.HttpHeaderName; 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.implementation.utils.UriEscapers; 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.SerializationFormat; +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 parameters.spread.model.BodyParameter; /** @@ -49,67 +51,172 @@ public final class ModelsImpl { } /** - * The interface defining all the services for SpreadClientModels to be used by the proxy service to perform REST - * calls. + * The HTTP operations for SpreadClientModels. */ - @ServiceInterface(name = "SpreadClientModels", host = "{endpoint}") public interface ModelsService { static ModelsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("parameters.spread.implementation.ModelsServiceImpl"); - return (ModelsService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new ModelsServiceImpl(pipeline); } - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/parameters/spread/model/request-body", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response spreadAsRequestBody(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") BodyParameter bodyParameter, + Response spreadAsRequestBody(String endpoint, String contentType, BodyParameter bodyParameter, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/parameters/spread/model/composite-request-only-with-body", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response spreadCompositeRequestOnlyWithBody(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") BodyParameter body, + Response spreadCompositeRequestOnlyWithBody(String endpoint, String contentType, BodyParameter body, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/parameters/spread/model/composite-request-without-body/{name}", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response spreadCompositeRequestWithoutBody(@HostParam("endpoint") String endpoint, - @PathParam("name") String name, @HeaderParam("test-header") String testHeader, + Response spreadCompositeRequestWithoutBody(String endpoint, String name, String testHeader, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/parameters/spread/model/composite-request/{name}", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response spreadCompositeRequest(@HostParam("endpoint") String endpoint, @PathParam("name") String name, - @HeaderParam("test-header") String testHeader, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") BodyParameter body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/parameters/spread/model/composite-request-mix/{name}", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response spreadCompositeRequestMix(@HostParam("endpoint") String endpoint, @PathParam("name") String name, - @HeaderParam("test-header") String testHeader, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") SpreadCompositeRequestMixRequest spreadCompositeRequestMixRequest, - RequestContext requestContext); + Response spreadCompositeRequest(String endpoint, String name, String testHeader, String contentType, + BodyParameter body, RequestContext requestContext); + + Response spreadCompositeRequestMix(String endpoint, String name, String testHeader, String contentType, + SpreadCompositeRequestMixRequest spreadCompositeRequestMixRequest, RequestContext requestContext); + } + + private static final class ModelsServiceImpl implements ModelsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ModelsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ModelsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response spreadAsRequestBody(String endpoint, String contentType, BodyParameter bodyParameter, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/parameters/spread/model/request-body"); + if (bodyParameter != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(bodyParameter, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 spreadCompositeRequestOnlyWithBody(String endpoint, String contentType, + BodyParameter body, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/parameters/spread/model/composite-request-only-with-body"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 spreadCompositeRequestWithoutBody(String endpoint, String name, String testHeader, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/parameters/spread/model/composite-request-without-body/" + + UriEscapers.PATH_ESCAPER.escape(name)); + if (testHeader != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("test-header"), testHeader); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 spreadCompositeRequest(String endpoint, String name, String testHeader, + String contentType, BodyParameter body, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri( + endpoint + "/parameters/spread/model/composite-request/" + UriEscapers.PATH_ESCAPER.escape(name)); + if (testHeader != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("test-header"), testHeader); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 spreadCompositeRequestMix(String endpoint, String name, String testHeader, + String contentType, SpreadCompositeRequestMixRequest spreadCompositeRequestMixRequest, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/parameters/spread/model/composite-request-mix/" + + UriEscapers.PATH_ESCAPER.escape(name)); + if (testHeader != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("test-header"), testHeader); + } + if (spreadCompositeRequestMixRequest != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(spreadCompositeRequestMixRequest, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/contentnegotiation/implementation/DifferentBodiesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/contentnegotiation/implementation/DifferentBodiesImpl.java index 42eb3130223..60f22d9f533 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/contentnegotiation/implementation/DifferentBodiesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/contentnegotiation/implementation/DifferentBodiesImpl.java @@ -1,20 +1,23 @@ package payload.contentnegotiation.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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 io.clientcore.core.instrumentation.logging.ClientLogger; import io.clientcore.core.models.binarydata.BinaryData; -import java.lang.reflect.InvocationTargetException; +import io.clientcore.core.serialization.SerializationFormat; +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.lang.reflect.ParameterizedType; import payload.contentnegotiation.differentbody.PngImageAsJson; /** @@ -48,38 +51,83 @@ public final class DifferentBodiesImpl { } /** - * The interface defining all the services for ContentNegotiationClientDifferentBodies to be used by the proxy - * service to perform REST calls. + * The HTTP operations for ContentNegotiationClientDifferentBodies. */ - @ServiceInterface(name = "ContentNegotiationClientDifferentBodies", host = "{endpoint}") public interface DifferentBodiesService { static DifferentBodiesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("payload.contentnegotiation.implementation.DifferentBodiesServiceImpl"); - return (DifferentBodiesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new DifferentBodiesServiceImpl(pipeline); + } + + Response getAvatarAsPng(String endpoint, String accept, RequestContext requestContext); + + Response getAvatarAsJson(String endpoint, String accept, RequestContext requestContext); + } + + private static final class DifferentBodiesServiceImpl implements DifferentBodiesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(DifferentBodiesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private DifferentBodiesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response getAvatarAsPng(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/content-negotiation/different-body"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return networkResponse; + } + @Override + public Response getAvatarAsJson(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/content-negotiation/different-body"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, PngImageAsJson.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/content-negotiation/different-body", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getAvatarAsPng(@HostParam("endpoint") String endpoint, - @HeaderParam("accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/content-negotiation/different-body", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getAvatarAsJson(@HostParam("endpoint") String endpoint, - @HeaderParam("accept") String accept, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/contentnegotiation/implementation/SameBodiesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/contentnegotiation/implementation/SameBodiesImpl.java index 43b4f59ff6b..cd5569d0b60 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/contentnegotiation/implementation/SameBodiesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/contentnegotiation/implementation/SameBodiesImpl.java @@ -1,20 +1,20 @@ package payload.contentnegotiation.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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 io.clientcore.core.instrumentation.logging.ClientLogger; import io.clientcore.core.models.binarydata.BinaryData; -import java.lang.reflect.InvocationTargetException; +import io.clientcore.core.serialization.json.JsonSerializer; +import io.clientcore.core.serialization.xml.XmlSerializer; +import io.clientcore.core.utils.GeneratedCodeUtils; /** * An instance of this class provides access to all the operations defined in SameBodies. @@ -47,37 +47,68 @@ public final class SameBodiesImpl { } /** - * The interface defining all the services for ContentNegotiationClientSameBodies to be used by the proxy service to - * perform REST calls. + * The HTTP operations for ContentNegotiationClientSameBodies. */ - @ServiceInterface(name = "ContentNegotiationClientSameBodies", host = "{endpoint}") public interface SameBodiesService { static SameBodiesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("payload.contentnegotiation.implementation.SameBodiesServiceImpl"); - return (SameBodiesService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new SameBodiesServiceImpl(pipeline); + } + + Response getAvatarAsPng(String endpoint, String accept, RequestContext requestContext); + + Response getAvatarAsJpeg(String endpoint, String accept, RequestContext requestContext); + } + + private static final class SameBodiesServiceImpl implements SameBodiesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(SameBodiesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private SameBodiesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response getAvatarAsPng(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/content-negotiation/same-body"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return networkResponse; } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/content-negotiation/same-body", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getAvatarAsPng(@HostParam("endpoint") String endpoint, - @HeaderParam("accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/content-negotiation/same-body", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getAvatarAsJpeg(@HostParam("endpoint") String endpoint, - @HeaderParam("accept") String accept, RequestContext requestContext); + @Override + public Response getAvatarAsJpeg(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/content-negotiation/same-body"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return networkResponse; + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/head/implementation/HeadClientImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/head/implementation/HeadClientImpl.java index b096a3e54a1..a83fc9f3981 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/head/implementation/HeadClientImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/head/implementation/HeadClientImpl.java @@ -1,18 +1,19 @@ package payload.head.implementation; 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.GeneratedCodeUtils; /** * Initializes a new instance of the HeadClient type. @@ -80,28 +81,45 @@ public HeadClientImpl(HttpPipeline httpPipeline, Instrumentation instrumentation } /** - * The interface defining all the services for HeadClient to be used by the proxy service to perform REST calls. + * The HTTP operations for HeadClient. */ - @ServiceInterface(name = "HeadClient", host = "{endpoint}") public interface HeadClientService { static HeadClientService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("payload.head.implementation.HeadClientServiceImpl"); - return (HeadClientService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new HeadClientServiceImpl(pipeline); + } + + Response contentTypeHeaderInResponse(String endpoint, RequestContext requestContext); + } + + private static final class HeadClientServiceImpl implements HeadClientService { + private static final ClientLogger HTTP_LOG = new ClientLogger(HeadClientService.class); + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private HeadClientServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation( - method = HttpMethod.HEAD, - path = "/payload/head/content-type-header-in-response", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response contentTypeHeaderInResponse(@HostParam("endpoint") String endpoint, - RequestContext requestContext); + @Override + public Response contentTypeHeaderInResponse(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.HEAD) + .setUri(endpoint + "/payload/head/content-type-header-in-response"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response networkResponse = this.httpPipeline.send(httpRequest)) { + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), null); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/jsonmergepatch/implementation/JsonMergePatchClientImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/jsonmergepatch/implementation/JsonMergePatchClientImpl.java index 6607251c7d7..c59963b5a8d 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/jsonmergepatch/implementation/JsonMergePatchClientImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/jsonmergepatch/implementation/JsonMergePatchClientImpl.java @@ -1,20 +1,23 @@ package payload.jsonmergepatch.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import payload.jsonmergepatch.Resource; import payload.jsonmergepatch.ResourcePatch; @@ -84,49 +87,137 @@ public JsonMergePatchClientImpl(HttpPipeline httpPipeline, Instrumentation instr } /** - * The interface defining all the services for JsonMergePatchClient to be used by the proxy service to perform REST - * calls. + * The HTTP operations for JsonMergePatchClient. */ - @ServiceInterface(name = "JsonMergePatchClient", host = "{endpoint}") public interface JsonMergePatchClientService { static JsonMergePatchClientService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("payload.jsonmergepatch.implementation.JsonMergePatchClientServiceImpl"); - return (JsonMergePatchClientService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new JsonMergePatchClientServiceImpl(pipeline); + } + + Response createResource(String endpoint, String contentType, String accept, Resource body, + RequestContext requestContext); + + Response updateResource(String endpoint, String contentType, String accept, ResourcePatch body, + RequestContext requestContext); + + Response updateOptionalResource(String endpoint, String accept, ResourcePatch body, + RequestContext requestContext); + } + + private static final class JsonMergePatchClientServiceImpl implements JsonMergePatchClientService { + private static final ClientLogger HTTP_LOG = new ClientLogger(JsonMergePatchClientService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private JsonMergePatchClientServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response createResource(String endpoint, String contentType, String accept, Resource body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/json-merge-patch/create/resource"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, Resource.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response updateResource(String endpoint, String contentType, String accept, ResourcePatch body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PATCH).setUri(endpoint + "/json-merge-patch/update/resource"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, Resource.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); } - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/json-merge-patch/create/resource", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response createResource(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") Resource body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PATCH, - path = "/json-merge-patch/update/resource", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response updateResource(@HostParam("endpoint") String endpoint, - @HeaderParam("content-type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/merge-patch+json") ResourcePatch body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PATCH, - path = "/json-merge-patch/update/resource/optional", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response updateOptionalResource(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, @BodyParam("application/merge-patch+json") ResourcePatch body, - RequestContext requestContext); + @Override + public Response updateOptionalResource(String endpoint, String accept, ResourcePatch body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PATCH) + .setUri(endpoint + "/json-merge-patch/update/resource/optional"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, "application/merge-patch+json"); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, Resource.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/mediatype/implementation/StringBodiesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/mediatype/implementation/StringBodiesImpl.java index 629948f01ee..825324ef30e 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/mediatype/implementation/StringBodiesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/mediatype/implementation/StringBodiesImpl.java @@ -1,20 +1,23 @@ package payload.mediatype.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; /** * An instance of this class provides access to all the operations defined in StringBodies. @@ -47,56 +50,137 @@ public final class StringBodiesImpl { } /** - * The interface defining all the services for MediaTypeClientStringBodies to be used by the proxy service to - * perform REST calls. + * The HTTP operations for MediaTypeClientStringBodies. */ - @ServiceInterface(name = "MediaTypeClientStringBodies", host = "{endpoint}") public interface StringBodiesService { static StringBodiesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("payload.mediatype.implementation.StringBodiesServiceImpl"); - return (StringBodiesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new StringBodiesServiceImpl(pipeline); + } + + Response sendAsText(String endpoint, String contentType, String text, RequestContext requestContext); + + Response getAsText(String endpoint, String accept, RequestContext requestContext); + + Response sendAsJson(String endpoint, String contentType, String text, RequestContext requestContext); + + Response getAsJson(String endpoint, String accept, RequestContext requestContext); + } + + private static final class StringBodiesServiceImpl implements StringBodiesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(StringBodiesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private StringBodiesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response sendAsText(String endpoint, String contentType, String text, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/payload/media-type/string-body/sendAsText"); + if (text != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + httpRequest.setBody(BinaryData.fromString(text)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response networkResponse = this.httpPipeline.send(httpRequest)) { + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + 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 getAsText(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/payload/media-type/string-body/getAsText"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, String.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response sendAsJson(String endpoint, String contentType, String text, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/payload/media-type/string-body/sendAsJson"); + if (text != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(text, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response networkResponse = this.httpPipeline.send(httpRequest)) { + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + 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 getAsJson(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/payload/media-type/string-body/getAsJson"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, String.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); } - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/payload/media-type/string-body/sendAsText", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response sendAsText(@HostParam("endpoint") String endpoint, - @HeaderParam("content-type") String contentType, @BodyParam("text/plain") String text, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/payload/media-type/string-body/getAsText", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getAsText(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/payload/media-type/string-body/sendAsJson", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response sendAsJson(@HostParam("endpoint") String endpoint, - @HeaderParam("content-type") String contentType, @BodyParam("application/json") String text, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/payload/media-type/string-body/getAsJson", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getAsJson(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/multipart/implementation/FormDataFilesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/multipart/implementation/FormDataFilesImpl.java index be4950cff90..d9e094eefea 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/multipart/implementation/FormDataFilesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/multipart/implementation/FormDataFilesImpl.java @@ -1,20 +1,22 @@ package payload.multipart.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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 payload.multipart.formdata.file.UploadFileArrayRequest; import payload.multipart.formdata.file.UploadFileRequiredFilenameRequest; import payload.multipart.formdata.file.UploadFileSpecificContentTypeRequest; @@ -50,52 +52,110 @@ public final class FormDataFilesImpl { } /** - * The interface defining all the services for MultiPartClientFormDataFiles to be used by the proxy service to - * perform REST calls. + * The HTTP operations for MultiPartClientFormDataFiles. */ - @ServiceInterface(name = "MultiPartClientFormDataFiles", host = "{endpoint}") public interface FormDataFilesService { static FormDataFilesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("payload.multipart.implementation.FormDataFilesServiceImpl"); - return (FormDataFilesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new FormDataFilesServiceImpl(pipeline); + } + + Response uploadFileSpecificContentType(String endpoint, String contentType, + UploadFileSpecificContentTypeRequest body, RequestContext requestContext); + + Response uploadFileRequiredFilename(String endpoint, String contentType, + UploadFileRequiredFilenameRequest body, RequestContext requestContext); + + Response uploadFileArray(String endpoint, String contentType, UploadFileArrayRequest body, + RequestContext requestContext); + } + + private static final class FormDataFilesServiceImpl implements FormDataFilesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(FormDataFilesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private FormDataFilesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response uploadFileSpecificContentType(String endpoint, String contentType, + UploadFileSpecificContentTypeRequest body, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/multipart/form-data/file/specific-content-type"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 uploadFileRequiredFilename(String endpoint, String contentType, + UploadFileRequiredFilenameRequest body, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/multipart/form-data/file/required-filename"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - // @Multipart not supported by RestProxy - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/multipart/form-data/file/specific-content-type", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response uploadFileSpecificContentType(@HostParam("endpoint") String endpoint, - @HeaderParam("content-type") String contentType, - @BodyParam("multipart/form-data") UploadFileSpecificContentTypeRequest body, RequestContext requestContext); - - // @Multipart not supported by RestProxy - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/multipart/form-data/file/required-filename", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response uploadFileRequiredFilename(@HostParam("endpoint") String endpoint, - @HeaderParam("content-type") String contentType, - @BodyParam("multipart/form-data") UploadFileRequiredFilenameRequest body, RequestContext requestContext); - - // @Multipart not supported by RestProxy - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/multipart/form-data/file/file-array", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response uploadFileArray(@HostParam("endpoint") String endpoint, - @HeaderParam("content-type") String contentType, - @BodyParam("multipart/form-data") UploadFileArrayRequest body, RequestContext requestContext); + @Override + public Response uploadFileArray(String endpoint, String contentType, UploadFileArrayRequest body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/multipart/form-data/file/file-array"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/multipart/implementation/FormDataHttpPartsContentTypesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/multipart/implementation/FormDataHttpPartsContentTypesImpl.java index 83768b8bd18..85b4f8504be 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/multipart/implementation/FormDataHttpPartsContentTypesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/multipart/implementation/FormDataHttpPartsContentTypesImpl.java @@ -1,20 +1,22 @@ package payload.multipart.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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 payload.multipart.FileWithHttpPartOptionalContentTypeRequest; import payload.multipart.FileWithHttpPartRequiredContentTypeRequest; import payload.multipart.FileWithHttpPartSpecificContentTypeRequest; @@ -50,56 +52,111 @@ public final class FormDataHttpPartsContentTypesImpl { } /** - * The interface defining all the services for MultiPartClientFormDataHttpPartsContentTypes to be used by the proxy - * service to perform REST calls. + * The HTTP operations for MultiPartClientFormDataHttpPartsContentTypes. */ - @ServiceInterface(name = "MultiPartClientFormDataHttpPartsContentTypes", host = "{endpoint}") public interface FormDataHttpPartsContentTypesService { static FormDataHttpPartsContentTypesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz - = Class.forName("payload.multipart.implementation.FormDataHttpPartsContentTypesServiceImpl"); - return (FormDataHttpPartsContentTypesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new FormDataHttpPartsContentTypesServiceImpl(pipeline); + } + + Response imageJpegContentType(String endpoint, String contentType, + FileWithHttpPartSpecificContentTypeRequest body, RequestContext requestContext); + + Response requiredContentType(String endpoint, String contentType, + FileWithHttpPartRequiredContentTypeRequest body, RequestContext requestContext); + + Response optionalContentType(String endpoint, String contentType, + FileWithHttpPartOptionalContentTypeRequest body, RequestContext requestContext); + } + + private static final class FormDataHttpPartsContentTypesServiceImpl + implements FormDataHttpPartsContentTypesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(FormDataHttpPartsContentTypesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private FormDataHttpPartsContentTypesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response imageJpegContentType(String endpoint, String contentType, + FileWithHttpPartSpecificContentTypeRequest body, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/multipart/form-data/check-filename-and-specific-content-type-with-httppart"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 requiredContentType(String endpoint, String contentType, + FileWithHttpPartRequiredContentTypeRequest body, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/multipart/form-data/check-filename-and-required-content-type-with-httppart"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - // @Multipart not supported by RestProxy - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/multipart/form-data/check-filename-and-specific-content-type-with-httppart", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response imageJpegContentType(@HostParam("endpoint") String endpoint, - @HeaderParam("content-type") String contentType, - @BodyParam("multipart/form-data") FileWithHttpPartSpecificContentTypeRequest body, - RequestContext requestContext); - - // @Multipart not supported by RestProxy - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/multipart/form-data/check-filename-and-required-content-type-with-httppart", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response requiredContentType(@HostParam("endpoint") String endpoint, - @HeaderParam("content-type") String contentType, - @BodyParam("multipart/form-data") FileWithHttpPartRequiredContentTypeRequest body, - RequestContext requestContext); - - // @Multipart not supported by RestProxy - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/multipart/form-data/file-with-http-part-optional-content-type", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response optionalContentType(@HostParam("endpoint") String endpoint, - @HeaderParam("content-type") String contentType, - @BodyParam("multipart/form-data") FileWithHttpPartOptionalContentTypeRequest body, - RequestContext requestContext); + @Override + public Response optionalContentType(String endpoint, String contentType, + FileWithHttpPartOptionalContentTypeRequest body, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/multipart/form-data/file-with-http-part-optional-content-type"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/multipart/implementation/FormDataHttpPartsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/multipart/implementation/FormDataHttpPartsImpl.java index 0737ff3de66..818683861b4 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/multipart/implementation/FormDataHttpPartsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/multipart/implementation/FormDataHttpPartsImpl.java @@ -1,20 +1,22 @@ package payload.multipart.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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 payload.multipart.ComplexHttpPartsModelRequest; /** @@ -48,32 +50,54 @@ public final class FormDataHttpPartsImpl { } /** - * The interface defining all the services for MultiPartClientFormDataHttpParts to be used by the proxy service to - * perform REST calls. + * The HTTP operations for MultiPartClientFormDataHttpParts. */ - @ServiceInterface(name = "MultiPartClientFormDataHttpParts", host = "{endpoint}") public interface FormDataHttpPartsService { static FormDataHttpPartsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("payload.multipart.implementation.FormDataHttpPartsServiceImpl"); - return (FormDataHttpPartsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new FormDataHttpPartsServiceImpl(pipeline); + } + + Response jsonArrayAndFileArray(String endpoint, String contentType, ComplexHttpPartsModelRequest body, + RequestContext requestContext); + } + + private static final class FormDataHttpPartsServiceImpl implements FormDataHttpPartsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(FormDataHttpPartsService.class); + + private final HttpPipeline httpPipeline; + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private FormDataHttpPartsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - // @Multipart not supported by RestProxy - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/multipart/form-data/complex-parts-with-httppart", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response jsonArrayAndFileArray(@HostParam("endpoint") String endpoint, - @HeaderParam("content-type") String contentType, - @BodyParam("multipart/form-data") ComplexHttpPartsModelRequest body, RequestContext requestContext); + @Override + public Response jsonArrayAndFileArray(String endpoint, String contentType, + ComplexHttpPartsModelRequest body, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/multipart/form-data/complex-parts-with-httppart"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/multipart/implementation/FormDataHttpPartsNonStringsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/multipart/implementation/FormDataHttpPartsNonStringsImpl.java index a47c15b5e6b..c9fdcaa8652 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/multipart/implementation/FormDataHttpPartsNonStringsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/multipart/implementation/FormDataHttpPartsNonStringsImpl.java @@ -1,20 +1,22 @@ package payload.multipart.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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 payload.multipart.formdata.httpparts.nonstring.FloatRequest; /** @@ -48,35 +50,56 @@ public final class FormDataHttpPartsNonStringsImpl { } /** - * The interface defining all the services for MultiPartClientFormDataHttpPartsNonStrings to be used by the proxy - * service to perform REST calls. + * The HTTP operations for MultiPartClientFormDataHttpPartsNonStrings. */ - @ServiceInterface(name = "MultiPartClientFormDataHttpPartsNonStrings", host = "{endpoint}") public interface FormDataHttpPartsNonStringsService { static FormDataHttpPartsNonStringsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz - = Class.forName("payload.multipart.implementation.FormDataHttpPartsNonStringsServiceImpl"); - return (FormDataHttpPartsNonStringsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new FormDataHttpPartsNonStringsServiceImpl(pipeline); } - // @Multipart not supported by RestProxy - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/multipart/form-data/non-string-float", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response floatMethod(@HostParam("endpoint") String endpoint, - @HeaderParam("content-type") String contentType, @BodyParam("multipart/form-data") FloatRequest body, + Response floatMethod(String endpoint, String contentType, FloatRequest body, RequestContext requestContext); } + private static final class FormDataHttpPartsNonStringsServiceImpl implements FormDataHttpPartsNonStringsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(FormDataHttpPartsNonStringsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private FormDataHttpPartsNonStringsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response floatMethod(String endpoint, String contentType, FloatRequest body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/multipart/form-data/non-string-float"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + } + /** * Test content-type: multipart/form-data for non string. * diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/multipart/implementation/FormDatasImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/multipart/implementation/FormDatasImpl.java index c81fb158cf3..81cfee9991b 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/multipart/implementation/FormDatasImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/multipart/implementation/FormDatasImpl.java @@ -1,20 +1,22 @@ package payload.multipart.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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 payload.multipart.BinaryArrayPartsRequest; import payload.multipart.ComplexPartsRequest; import payload.multipart.JsonPartRequest; @@ -55,109 +57,277 @@ public final class FormDatasImpl { } /** - * The interface defining all the services for MultiPartClientFormDatas to be used by the proxy service to perform - * REST calls. + * The HTTP operations for MultiPartClientFormDatas. */ - @ServiceInterface(name = "MultiPartClientFormDatas", host = "{endpoint}") public interface FormDatasService { static FormDatasService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("payload.multipart.implementation.FormDatasServiceImpl"); - return (FormDatasService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new FormDatasServiceImpl(pipeline); } - // @Multipart not supported by RestProxy - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/multipart/form-data/mixed-parts", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response basic(@HostParam("endpoint") String endpoint, @HeaderParam("content-type") String contentType, - @BodyParam("multipart/form-data") MultiPartRequest body, RequestContext requestContext); - - // @Multipart not supported by RestProxy - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/multipart/form-data/mixed-parts-with-wire-name", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withWireName(@HostParam("endpoint") String endpoint, - @HeaderParam("content-type") String contentType, - @BodyParam("multipart/form-data") MultiPartRequestWithWireName body, RequestContext requestContext); - - // @Multipart not supported by RestProxy - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/multipart/form-data/optional-parts", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response optionalParts(@HostParam("endpoint") String endpoint, - @HeaderParam("content-type") String contentType, - @BodyParam("multipart/form-data") MultiPartOptionalRequest body, RequestContext requestContext); - - // @Multipart not supported by RestProxy - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/multipart/form-data/complex-parts", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response fileArrayAndBasic(@HostParam("endpoint") String endpoint, - @HeaderParam("content-type") String contentType, @BodyParam("multipart/form-data") ComplexPartsRequest body, + Response basic(String endpoint, String contentType, MultiPartRequest body, RequestContext requestContext); + + Response withWireName(String endpoint, String contentType, MultiPartRequestWithWireName body, + RequestContext requestContext); + + Response optionalParts(String endpoint, String contentType, MultiPartOptionalRequest body, + RequestContext requestContext); + + Response fileArrayAndBasic(String endpoint, String contentType, ComplexPartsRequest body, RequestContext requestContext); - // @Multipart not supported by RestProxy - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/multipart/form-data/json-part", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response jsonPart(@HostParam("endpoint") String endpoint, @HeaderParam("content-type") String contentType, - @BodyParam("multipart/form-data") JsonPartRequest body, RequestContext requestContext); - - // @Multipart not supported by RestProxy - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/multipart/form-data/binary-array-parts", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response binaryArrayParts(@HostParam("endpoint") String endpoint, - @HeaderParam("content-type") String contentType, - @BodyParam("multipart/form-data") BinaryArrayPartsRequest body, RequestContext requestContext); - - // @Multipart not supported by RestProxy - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/multipart/form-data/multi-binary-parts", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response multiBinaryParts(@HostParam("endpoint") String endpoint, - @HeaderParam("content-type") String contentType, - @BodyParam("multipart/form-data") MultiBinaryPartsRequest body, RequestContext requestContext); - - // @Multipart not supported by RestProxy - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/multipart/form-data/check-filename-and-content-type", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response checkFileNameAndContentType(@HostParam("endpoint") String endpoint, - @HeaderParam("content-type") String contentType, @BodyParam("multipart/form-data") MultiPartRequest body, + Response jsonPart(String endpoint, String contentType, JsonPartRequest body, RequestContext requestContext); - // @Multipart not supported by RestProxy - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/multipart/form-data/anonymous-model", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response anonymousModel(@HostParam("endpoint") String endpoint, - @HeaderParam("content-type") String contentType, - @BodyParam("multipart/form-data") AnonymousModelRequest body, RequestContext requestContext); + Response binaryArrayParts(String endpoint, String contentType, BinaryArrayPartsRequest body, + RequestContext requestContext); + + Response multiBinaryParts(String endpoint, String contentType, MultiBinaryPartsRequest body, + RequestContext requestContext); + + Response checkFileNameAndContentType(String endpoint, String contentType, MultiPartRequest body, + RequestContext requestContext); + + Response anonymousModel(String endpoint, String contentType, AnonymousModelRequest body, + RequestContext requestContext); + } + + private static final class FormDatasServiceImpl implements FormDatasService { + private static final ClientLogger HTTP_LOG = new ClientLogger(FormDatasService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private FormDatasServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response basic(String endpoint, String contentType, MultiPartRequest body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/multipart/form-data/mixed-parts"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withWireName(String endpoint, String contentType, MultiPartRequestWithWireName body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/multipart/form-data/mixed-parts-with-wire-name"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 optionalParts(String endpoint, String contentType, MultiPartOptionalRequest body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/multipart/form-data/optional-parts"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 fileArrayAndBasic(String endpoint, String contentType, ComplexPartsRequest body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/multipart/form-data/complex-parts"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 jsonPart(String endpoint, String contentType, JsonPartRequest body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/multipart/form-data/json-part"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 binaryArrayParts(String endpoint, String contentType, BinaryArrayPartsRequest body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/multipart/form-data/binary-array-parts"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 multiBinaryParts(String endpoint, String contentType, MultiBinaryPartsRequest body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/multipart/form-data/multi-binary-parts"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 checkFileNameAndContentType(String endpoint, String contentType, MultiPartRequest body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/multipart/form-data/check-filename-and-content-type"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 anonymousModel(String endpoint, String contentType, AnonymousModelRequest body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/multipart/form-data/anonymous-model"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/pageable/implementation/PageSizesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/pageable/implementation/PageSizesImpl.java index 7252ed76fe1..d6eb6f799fd 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/pageable/implementation/PageSizesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/pageable/implementation/PageSizesImpl.java @@ -1,14 +1,10 @@ package payload.pageable.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.HeaderParam; -import io.clientcore.core.http.annotations.HostParam; -import io.clientcore.core.http.annotations.HttpRequestInformation; -import io.clientcore.core.http.annotations.QueryParam; -import io.clientcore.core.http.annotations.UnexpectedResponseExceptionDetail; +import io.clientcore.core.http.models.HttpHeaderName; 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; @@ -17,7 +13,14 @@ import io.clientcore.core.http.pipeline.HttpPipeline; import io.clientcore.core.instrumentation.Instrumentation; import io.clientcore.core.instrumentation.logging.ClientLogger; -import java.lang.reflect.InvocationTargetException; +import io.clientcore.core.models.binarydata.BinaryData; +import io.clientcore.core.serialization.SerializationFormat; +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 io.clientcore.core.utils.UriBuilder; +import java.lang.reflect.ParameterizedType; import payload.pageable.Pet; import payload.pageable.pagesize.implementation.ListWithPageSizeResponse; import payload.pageable.pagesize.implementation.ListWithoutContinuationResponse; @@ -53,40 +56,93 @@ public final class PageSizesImpl { } /** - * The interface defining all the services for PageableClientPageSizes to be used by the proxy service to perform - * REST calls. + * The HTTP operations for PageableClientPageSizes. */ - @ServiceInterface(name = "PageableClientPageSizes", host = "{endpoint}") public interface PageSizesService { static PageSizesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("payload.pageable.implementation.PageSizesServiceImpl"); - return (PageSizesService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new PageSizesServiceImpl(pipeline); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/payload/pageable/pagesize/without-continuation", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response listWithoutContinuation(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); + Response listWithoutContinuation(String endpoint, String accept, + RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/payload/pageable/pagesize/list", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response listWithPageSize(@HostParam("endpoint") String endpoint, - @QueryParam("pageSize") Integer pageSize, @HeaderParam("Accept") String accept, + Response listWithPageSize(String endpoint, Integer pageSize, String accept, RequestContext requestContext); } + private static final class PageSizesServiceImpl implements PageSizesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(PageSizesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private PageSizesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response listWithoutContinuation(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/payload/pageable/pagesize/without-continuation"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, ListWithoutContinuationResponse.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response listWithPageSize(String endpoint, Integer pageSize, String accept, + RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/payload/pageable/pagesize/list"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "pageSize", true, pageSize, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, ListWithPageSizeResponse.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } + } + /** * The listWithoutContinuation operation. * diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/pageable/implementation/ServerDrivenPaginationAlternateInitialVerbsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/pageable/implementation/ServerDrivenPaginationAlternateInitialVerbsImpl.java index 6b712ffdde3..b0dffe701f6 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/pageable/implementation/ServerDrivenPaginationAlternateInitialVerbsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/pageable/implementation/ServerDrivenPaginationAlternateInitialVerbsImpl.java @@ -1,15 +1,10 @@ package payload.pageable.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -import io.clientcore.core.http.annotations.HostParam; -import io.clientcore.core.http.annotations.HttpRequestInformation; -import io.clientcore.core.http.annotations.PathParam; -import io.clientcore.core.http.annotations.UnexpectedResponseExceptionDetail; +import io.clientcore.core.http.models.HttpHeaderName; 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; @@ -18,7 +13,13 @@ import io.clientcore.core.http.pipeline.HttpPipeline; import io.clientcore.core.instrumentation.Instrumentation; import io.clientcore.core.instrumentation.logging.ClientLogger; -import java.lang.reflect.InvocationTargetException; +import io.clientcore.core.models.binarydata.BinaryData; +import io.clientcore.core.serialization.SerializationFormat; +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.lang.reflect.ParameterizedType; import payload.pageable.Pet; import payload.pageable.serverdrivenpagination.alternateinitialverb.Filter; import payload.pageable.serverdrivenpagination.alternateinitialverb.implementation.PostResponse; @@ -55,38 +56,95 @@ public final class ServerDrivenPaginationAlternateInitialVerbsImpl { } /** - * The interface defining all the services for PageableClientServerDrivenPaginationAlternateInitialVerbs to be used - * by the proxy service to perform REST calls. + * The HTTP operations for PageableClientServerDrivenPaginationAlternateInitialVerbs. */ - @ServiceInterface(name = "PageableClientServerDrivenPaginationAlternateInitialVerbs", host = "{endpoint}") public interface ServerDrivenPaginationAlternateInitialVerbsService { static ServerDrivenPaginationAlternateInitialVerbsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class - .forName("payload.pageable.implementation.ServerDrivenPaginationAlternateInitialVerbsServiceImpl"); - return (ServerDrivenPaginationAlternateInitialVerbsService) clazz - .getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new ServerDrivenPaginationAlternateInitialVerbsServiceImpl(pipeline); + } + + Response post(String endpoint, String accept, Filter body, RequestContext requestContext); + + Response postNext(String nextLink, String endpoint, String accept, RequestContext requestContext); + } + + private static final class ServerDrivenPaginationAlternateInitialVerbsServiceImpl + implements ServerDrivenPaginationAlternateInitialVerbsService { + private static final ClientLogger HTTP_LOG + = new ClientLogger(ServerDrivenPaginationAlternateInitialVerbsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ServerDrivenPaginationAlternateInitialVerbsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + @Override + public Response post(String endpoint, String accept, Filter body, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/payload/pageable/server-driven-pagination/link/initial-post"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, "application/json"); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, PostResponse.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); } - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/payload/pageable/server-driven-pagination/link/initial-post", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response post(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - @BodyParam("application/json") Filter body, RequestContext requestContext); + @Override + public Response postNext(String nextLink, String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(nextLink); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, PostResponse.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } - @HttpRequestInformation(method = HttpMethod.GET, path = "{nextLink}", expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response postNext(@PathParam(value = "nextLink", encoded = true) String nextLink, - @HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/pageable/implementation/ServerDrivenPaginationContinuationTokensImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/pageable/implementation/ServerDrivenPaginationContinuationTokensImpl.java index 0f1b506ab86..048b1801807 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/pageable/implementation/ServerDrivenPaginationContinuationTokensImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/pageable/implementation/ServerDrivenPaginationContinuationTokensImpl.java @@ -1,15 +1,10 @@ package payload.pageable.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.HeaderParam; -import io.clientcore.core.http.annotations.HostParam; -import io.clientcore.core.http.annotations.HttpRequestInformation; -import io.clientcore.core.http.annotations.QueryParam; -import io.clientcore.core.http.annotations.UnexpectedResponseExceptionDetail; import io.clientcore.core.http.models.HttpHeaderName; 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; @@ -18,7 +13,14 @@ import io.clientcore.core.http.pipeline.HttpPipeline; import io.clientcore.core.instrumentation.Instrumentation; import io.clientcore.core.instrumentation.logging.ClientLogger; -import java.lang.reflect.InvocationTargetException; +import io.clientcore.core.models.binarydata.BinaryData; +import io.clientcore.core.serialization.SerializationFormat; +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 io.clientcore.core.utils.UriBuilder; +import java.lang.reflect.ParameterizedType; import payload.pageable.Pet; import payload.pageable.serverdrivenpagination.continuationtoken.implementation.RequestHeaderNestedResponseBodyResponse; import payload.pageable.serverdrivenpagination.continuationtoken.implementation.RequestHeaderResponseBodyResponse; @@ -56,78 +58,238 @@ public final class ServerDrivenPaginationContinuationTokensImpl { } /** - * The interface defining all the services for PageableClientServerDrivenPaginationContinuationTokens to be used by - * the proxy service to perform REST calls. + * The HTTP operations for PageableClientServerDrivenPaginationContinuationTokens. */ - @ServiceInterface(name = "PageableClientServerDrivenPaginationContinuationTokens", host = "{endpoint}") public interface ServerDrivenPaginationContinuationTokensService { static ServerDrivenPaginationContinuationTokensService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class - .forName("payload.pageable.implementation.ServerDrivenPaginationContinuationTokensServiceImpl"); - return (ServerDrivenPaginationContinuationTokensService) clazz - .getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new ServerDrivenPaginationContinuationTokensServiceImpl(pipeline); + } + + Response requestQueryResponseBody(String endpoint, String token, String foo, + String bar, String accept, RequestContext requestContext); + + Response requestHeaderResponseBody(String endpoint, String token, String foo, + String bar, String accept, RequestContext requestContext); + + Response requestQueryResponseHeader(String endpoint, String token, + String foo, String bar, String accept, RequestContext requestContext); + + Response requestHeaderResponseHeader(String endpoint, String token, + String foo, String bar, String accept, RequestContext requestContext); + + Response requestQueryNestedResponseBody(String endpoint, String token, + String foo, String bar, String accept, RequestContext requestContext); + + Response requestHeaderNestedResponseBody(String endpoint, String token, + String foo, String bar, String accept, RequestContext requestContext); + } + + private static final class ServerDrivenPaginationContinuationTokensServiceImpl + implements ServerDrivenPaginationContinuationTokensService { + private static final ClientLogger HTTP_LOG + = new ClientLogger(ServerDrivenPaginationContinuationTokensService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ServerDrivenPaginationContinuationTokensServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response requestQueryResponseBody(String endpoint, String token, + String foo, String bar, String accept, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse( + endpoint + "/payload/pageable/server-driven-pagination/continuationtoken/request-query-response-body"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "token", true, token, true); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "bar", true, bar, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + if (foo != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("foo"), foo); } + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, RequestQueryResponseBodyResponse.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response requestHeaderResponseBody(String endpoint, String token, + String foo, String bar, String accept, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse( + endpoint + "/payload/pageable/server-driven-pagination/continuationtoken/request-header-response-body"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "bar", true, bar, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + if (token != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("token"), token); + } + if (foo != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("foo"), foo); + } + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, RequestHeaderResponseBodyResponse.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/payload/pageable/server-driven-pagination/continuationtoken/request-query-response-body", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response requestQueryResponseBody(@HostParam("endpoint") String endpoint, - @QueryParam("token") String token, @HeaderParam("foo") String foo, @QueryParam("bar") String bar, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/payload/pageable/server-driven-pagination/continuationtoken/request-header-response-body", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response requestHeaderResponseBody(@HostParam("endpoint") String endpoint, - @HeaderParam("token") String token, @HeaderParam("foo") String foo, @QueryParam("bar") String bar, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/payload/pageable/server-driven-pagination/continuationtoken/request-query-response-header", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response requestQueryResponseHeader(@HostParam("endpoint") String endpoint, - @QueryParam("token") String token, @HeaderParam("foo") String foo, @QueryParam("bar") String bar, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/payload/pageable/server-driven-pagination/continuationtoken/request-header-response-header", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response requestHeaderResponseHeader( - @HostParam("endpoint") String endpoint, @HeaderParam("token") String token, @HeaderParam("foo") String foo, - @QueryParam("bar") String bar, @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/payload/pageable/server-driven-pagination/continuationtoken/request-query-nested-response-body", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response requestQueryNestedResponseBody( - @HostParam("endpoint") String endpoint, @QueryParam("token") String token, @HeaderParam("foo") String foo, - @QueryParam("bar") String bar, @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/payload/pageable/server-driven-pagination/continuationtoken/request-header-nested-response-body", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response requestHeaderNestedResponseBody( - @HostParam("endpoint") String endpoint, @HeaderParam("token") String token, @HeaderParam("foo") String foo, - @QueryParam("bar") String bar, @HeaderParam("Accept") String accept, RequestContext requestContext); + @Override + public Response requestQueryResponseHeader(String endpoint, String token, + String foo, String bar, String accept, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + + "/payload/pageable/server-driven-pagination/continuationtoken/request-query-response-header"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "token", true, token, true); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "bar", true, bar, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + if (foo != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("foo"), foo); + } + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, RequestQueryResponseHeaderResponse.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response requestHeaderResponseHeader(String endpoint, String token, + String foo, String bar, String accept, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + + "/payload/pageable/server-driven-pagination/continuationtoken/request-header-response-header"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "bar", true, bar, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + if (token != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("token"), token); + } + if (foo != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("foo"), foo); + } + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, RequestHeaderResponseHeaderResponse.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response requestQueryNestedResponseBody(String endpoint, + String token, String foo, String bar, String accept, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + + "/payload/pageable/server-driven-pagination/continuationtoken/request-query-nested-response-body"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "token", true, token, true); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "bar", true, bar, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + if (foo != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("foo"), foo); + } + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, RequestQueryNestedResponseBodyResponse.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response requestHeaderNestedResponseBody(String endpoint, + String token, String foo, String bar, String accept, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + + "/payload/pageable/server-driven-pagination/continuationtoken/request-header-nested-response-body"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "bar", true, bar, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + if (token != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("token"), token); + } + if (foo != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("foo"), foo); + } + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, RequestHeaderNestedResponseBodyResponse.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/pageable/implementation/ServerDrivenPaginationsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/pageable/implementation/ServerDrivenPaginationsImpl.java index 766c2e45222..de0ece2fe00 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/pageable/implementation/ServerDrivenPaginationsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/pageable/implementation/ServerDrivenPaginationsImpl.java @@ -1,14 +1,10 @@ package payload.pageable.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.HeaderParam; -import io.clientcore.core.http.annotations.HostParam; -import io.clientcore.core.http.annotations.HttpRequestInformation; -import io.clientcore.core.http.annotations.PathParam; -import io.clientcore.core.http.annotations.UnexpectedResponseExceptionDetail; +import io.clientcore.core.http.models.HttpHeaderName; 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; @@ -17,7 +13,13 @@ import io.clientcore.core.http.pipeline.HttpPipeline; import io.clientcore.core.instrumentation.Instrumentation; import io.clientcore.core.instrumentation.logging.ClientLogger; -import java.lang.reflect.InvocationTargetException; +import io.clientcore.core.models.binarydata.BinaryData; +import io.clientcore.core.serialization.SerializationFormat; +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.lang.reflect.ParameterizedType; import payload.pageable.Pet; import payload.pageable.serverdrivenpagination.implementation.LinkResponse; import payload.pageable.serverdrivenpagination.implementation.LinkStringResponse; @@ -54,66 +56,186 @@ public final class ServerDrivenPaginationsImpl { } /** - * The interface defining all the services for PageableClientServerDrivenPaginations to be used by the proxy service - * to perform REST calls. + * The HTTP operations for PageableClientServerDrivenPaginations. */ - @ServiceInterface(name = "PageableClientServerDrivenPaginations", host = "{endpoint}") public interface ServerDrivenPaginationsService { static ServerDrivenPaginationsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("payload.pageable.implementation.ServerDrivenPaginationsServiceImpl"); - return (ServerDrivenPaginationsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new ServerDrivenPaginationsServiceImpl(pipeline); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/payload/pageable/server-driven-pagination/link", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response link(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); + Response link(String endpoint, String accept, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/payload/pageable/server-driven-pagination/link-string", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response linkString(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/payload/pageable/server-driven-pagination/nested-link", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response nestedLink(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation(method = HttpMethod.GET, path = "{nextLink}", expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response linkNext(@PathParam(value = "nextLink", encoded = true) String nextLink, - @HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); + Response linkString(String endpoint, String accept, RequestContext requestContext); + + Response nestedLink(String endpoint, String accept, RequestContext requestContext); + + Response linkNext(String nextLink, String endpoint, String accept, RequestContext requestContext); - @HttpRequestInformation(method = HttpMethod.GET, path = "{nextLink}", expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response linkStringNext(@PathParam(value = "nextLink", encoded = true) String nextLink, - @HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, + Response linkStringNext(String nextLink, String endpoint, String accept, RequestContext requestContext); - @HttpRequestInformation(method = HttpMethod.GET, path = "{nextLink}", expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response nestedLinkNext(@PathParam(value = "nextLink", encoded = true) String nextLink, - @HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, + Response nestedLinkNext(String nextLink, String endpoint, String accept, RequestContext requestContext); } + private static final class ServerDrivenPaginationsServiceImpl implements ServerDrivenPaginationsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ServerDrivenPaginationsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ServerDrivenPaginationsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response link(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/payload/pageable/server-driven-pagination/link"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, LinkResponse.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response linkString(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/payload/pageable/server-driven-pagination/link-string"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, LinkStringResponse.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response nestedLink(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/payload/pageable/server-driven-pagination/nested-link"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, NestedLinkResponse.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response linkNext(String nextLink, String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(nextLink); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, LinkResponse.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response linkStringNext(String nextLink, String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(nextLink); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, LinkStringResponse.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response nestedLinkNext(String nextLink, String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(nextLink); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, NestedLinkResponse.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } + } + /** * The link operation. * diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/pageable/implementation/XmlPaginationsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/pageable/implementation/XmlPaginationsImpl.java index fb93662a0b0..fee22efda75 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/pageable/implementation/XmlPaginationsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/pageable/implementation/XmlPaginationsImpl.java @@ -1,15 +1,10 @@ package payload.pageable.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.HeaderParam; -import io.clientcore.core.http.annotations.HostParam; -import io.clientcore.core.http.annotations.HttpRequestInformation; -import io.clientcore.core.http.annotations.PathParam; -import io.clientcore.core.http.annotations.QueryParam; -import io.clientcore.core.http.annotations.UnexpectedResponseExceptionDetail; +import io.clientcore.core.http.models.HttpHeaderName; 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; @@ -18,7 +13,14 @@ import io.clientcore.core.http.pipeline.HttpPipeline; import io.clientcore.core.instrumentation.Instrumentation; import io.clientcore.core.instrumentation.logging.ClientLogger; -import java.lang.reflect.InvocationTargetException; +import io.clientcore.core.models.binarydata.BinaryData; +import io.clientcore.core.serialization.SerializationFormat; +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 io.clientcore.core.utils.UriBuilder; +import java.lang.reflect.ParameterizedType; import payload.pageable.XmlPet; /** @@ -52,44 +54,116 @@ public final class XmlPaginationsImpl { } /** - * The interface defining all the services for PageableClientXmlPaginations to be used by the proxy service to - * perform REST calls. + * The HTTP operations for PageableClientXmlPaginations. */ - @ServiceInterface(name = "PageableClientXmlPaginations", host = "{endpoint}") public interface XmlPaginationsService { static XmlPaginationsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("payload.pageable.implementation.XmlPaginationsServiceImpl"); - return (XmlPaginationsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new XmlPaginationsServiceImpl(pipeline); + } + + Response listWithContinuation(String endpoint, String marker, String accept, + RequestContext requestContext); + + Response listWithNextLink(String endpoint, String accept, + RequestContext requestContext); + + Response listWithNextLinkNext(String nextLink, String endpoint, String accept, + RequestContext requestContext); + } + private static final class XmlPaginationsServiceImpl implements XmlPaginationsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(XmlPaginationsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private XmlPaginationsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/payload/pageable/xml/list-with-continuation", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response listWithContinuation(@HostParam("endpoint") String endpoint, - @QueryParam("marker") String marker, @HeaderParam("Accept") String accept, RequestContext requestContext); + @Override + public Response listWithContinuation(String endpoint, String marker, String accept, + RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/payload/pageable/xml/list-with-continuation"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "marker", true, marker, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, XmlPetListResult.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/payload/pageable/xml/list-with-next-link", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response listWithNextLink(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); + @Override + public Response listWithNextLink(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/payload/pageable/xml/list-with-next-link"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, XmlPetListResultWithNextLink.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } - @HttpRequestInformation(method = HttpMethod.GET, path = "{nextLink}", expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response listWithNextLinkNext( - @PathParam(value = "nextLink", encoded = true) String nextLink, @HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); + @Override + public Response listWithNextLinkNext(String nextLink, String endpoint, + String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(nextLink); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, XmlPetListResultWithNextLink.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithArrayOfModelValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithArrayOfModelValuesImpl.java index 933e90f228b..a091718a38e 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithArrayOfModelValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithArrayOfModelValuesImpl.java @@ -1,20 +1,23 @@ package payload.xml.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import payload.xml.ModelWithArrayOfModel; /** @@ -48,38 +51,90 @@ public final class ModelWithArrayOfModelValuesImpl { } /** - * The interface defining all the services for XmlClientModelWithArrayOfModelValues to be used by the proxy service - * to perform REST calls. + * The HTTP operations for XmlClientModelWithArrayOfModelValues. */ - @ServiceInterface(name = "XmlClientModelWithArrayOfModelValues", host = "{endpoint}") public interface ModelWithArrayOfModelValuesService { static ModelWithArrayOfModelValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("payload.xml.implementation.ModelWithArrayOfModelValuesServiceImpl"); - return (ModelWithArrayOfModelValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new ModelWithArrayOfModelValuesServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, ModelWithArrayOfModel input, + RequestContext requestContext); + } + + private static final class ModelWithArrayOfModelValuesServiceImpl implements ModelWithArrayOfModelValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ModelWithArrayOfModelValuesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ModelWithArrayOfModelValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/payload/xml/modelWithArrayOfModel"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, ModelWithArrayOfModel.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, ModelWithArrayOfModel input, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/payload/xml/modelWithArrayOfModel"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/payload/xml/modelWithArrayOfModel", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/payload/xml/modelWithArrayOfModel", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("content-type") String contentType, - @BodyParam("application/xml") ModelWithArrayOfModel input, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithAttributesValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithAttributesValuesImpl.java index 5a76bf14cfd..681481913b2 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithAttributesValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithAttributesValuesImpl.java @@ -1,20 +1,23 @@ package payload.xml.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import payload.xml.ModelWithAttributes; /** @@ -48,38 +51,90 @@ public final class ModelWithAttributesValuesImpl { } /** - * The interface defining all the services for XmlClientModelWithAttributesValues to be used by the proxy service to - * perform REST calls. + * The HTTP operations for XmlClientModelWithAttributesValues. */ - @ServiceInterface(name = "XmlClientModelWithAttributesValues", host = "{endpoint}") public interface ModelWithAttributesValuesService { static ModelWithAttributesValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("payload.xml.implementation.ModelWithAttributesValuesServiceImpl"); - return (ModelWithAttributesValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new ModelWithAttributesValuesServiceImpl(pipeline); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/payload/xml/modelWithAttributes", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, ModelWithAttributes input, RequestContext requestContext); + } + + private static final class ModelWithAttributesValuesServiceImpl implements ModelWithAttributesValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ModelWithAttributesValuesService.class); + + private final HttpPipeline httpPipeline; - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/payload/xml/modelWithAttributes", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("content-type") String contentType, - @BodyParam("application/xml") ModelWithAttributes input, RequestContext requestContext); + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ModelWithAttributesValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/payload/xml/modelWithAttributes"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, ModelWithAttributes.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response put(String endpoint, String contentType, ModelWithAttributes input, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/payload/xml/modelWithAttributes"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithDatetimeValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithDatetimeValuesImpl.java index cfc0382ef6b..1fd271c6a27 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithDatetimeValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithDatetimeValuesImpl.java @@ -1,20 +1,23 @@ package payload.xml.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import payload.xml.ModelWithDatetime; /** @@ -48,38 +51,89 @@ public final class ModelWithDatetimeValuesImpl { } /** - * The interface defining all the services for XmlClientModelWithDatetimeValues to be used by the proxy service to - * perform REST calls. + * The HTTP operations for XmlClientModelWithDatetimeValues. */ - @ServiceInterface(name = "XmlClientModelWithDatetimeValues", host = "{endpoint}") public interface ModelWithDatetimeValuesService { static ModelWithDatetimeValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("payload.xml.implementation.ModelWithDatetimeValuesServiceImpl"); - return (ModelWithDatetimeValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new ModelWithDatetimeValuesServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, ModelWithDatetime input, RequestContext requestContext); + } + + private static final class ModelWithDatetimeValuesServiceImpl implements ModelWithDatetimeValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ModelWithDatetimeValuesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ModelWithDatetimeValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/payload/xml/modelWithDatetime"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, ModelWithDatetime.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, ModelWithDatetime input, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/payload/xml/modelWithDatetime"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/payload/xml/modelWithDatetime", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/payload/xml/modelWithDatetime", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("content-type") String contentType, - @BodyParam("application/xml") ModelWithDatetime input, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithDictionaryValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithDictionaryValuesImpl.java index 6b2289ee88b..06e35806b00 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithDictionaryValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithDictionaryValuesImpl.java @@ -1,20 +1,23 @@ package payload.xml.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import payload.xml.ModelWithDictionary; /** @@ -48,38 +51,90 @@ public final class ModelWithDictionaryValuesImpl { } /** - * The interface defining all the services for XmlClientModelWithDictionaryValues to be used by the proxy service to - * perform REST calls. + * The HTTP operations for XmlClientModelWithDictionaryValues. */ - @ServiceInterface(name = "XmlClientModelWithDictionaryValues", host = "{endpoint}") public interface ModelWithDictionaryValuesService { static ModelWithDictionaryValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("payload.xml.implementation.ModelWithDictionaryValuesServiceImpl"); - return (ModelWithDictionaryValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new ModelWithDictionaryValuesServiceImpl(pipeline); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/payload/xml/modelWithDictionary", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, ModelWithDictionary input, RequestContext requestContext); + } + + private static final class ModelWithDictionaryValuesServiceImpl implements ModelWithDictionaryValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ModelWithDictionaryValuesService.class); + + private final HttpPipeline httpPipeline; - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/payload/xml/modelWithDictionary", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("content-type") String contentType, - @BodyParam("application/xml") ModelWithDictionary input, RequestContext requestContext); + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ModelWithDictionaryValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/payload/xml/modelWithDictionary"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, ModelWithDictionary.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response put(String endpoint, String contentType, ModelWithDictionary input, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/payload/xml/modelWithDictionary"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithEmptyArrayValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithEmptyArrayValuesImpl.java index 8d2f1330e28..1ef869432d1 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithEmptyArrayValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithEmptyArrayValuesImpl.java @@ -1,20 +1,23 @@ package payload.xml.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import payload.xml.ModelWithEmptyArray; /** @@ -48,38 +51,90 @@ public final class ModelWithEmptyArrayValuesImpl { } /** - * The interface defining all the services for XmlClientModelWithEmptyArrayValues to be used by the proxy service to - * perform REST calls. + * The HTTP operations for XmlClientModelWithEmptyArrayValues. */ - @ServiceInterface(name = "XmlClientModelWithEmptyArrayValues", host = "{endpoint}") public interface ModelWithEmptyArrayValuesService { static ModelWithEmptyArrayValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("payload.xml.implementation.ModelWithEmptyArrayValuesServiceImpl"); - return (ModelWithEmptyArrayValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new ModelWithEmptyArrayValuesServiceImpl(pipeline); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/payload/xml/modelWithEmptyArray", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, ModelWithEmptyArray input, RequestContext requestContext); + } + + private static final class ModelWithEmptyArrayValuesServiceImpl implements ModelWithEmptyArrayValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ModelWithEmptyArrayValuesService.class); + + private final HttpPipeline httpPipeline; - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/payload/xml/modelWithEmptyArray", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("content-type") String contentType, - @BodyParam("application/xml") ModelWithEmptyArray input, RequestContext requestContext); + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ModelWithEmptyArrayValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/payload/xml/modelWithEmptyArray"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, ModelWithEmptyArray.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response put(String endpoint, String contentType, ModelWithEmptyArray input, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/payload/xml/modelWithEmptyArray"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithEncodedNamesValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithEncodedNamesValuesImpl.java index 7ad9ae53c5a..009d19a05cc 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithEncodedNamesValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithEncodedNamesValuesImpl.java @@ -1,20 +1,23 @@ package payload.xml.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import payload.xml.ModelWithEncodedNames; /** @@ -48,38 +51,90 @@ public final class ModelWithEncodedNamesValuesImpl { } /** - * The interface defining all the services for XmlClientModelWithEncodedNamesValues to be used by the proxy service - * to perform REST calls. + * The HTTP operations for XmlClientModelWithEncodedNamesValues. */ - @ServiceInterface(name = "XmlClientModelWithEncodedNamesValues", host = "{endpoint}") public interface ModelWithEncodedNamesValuesService { static ModelWithEncodedNamesValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("payload.xml.implementation.ModelWithEncodedNamesValuesServiceImpl"); - return (ModelWithEncodedNamesValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new ModelWithEncodedNamesValuesServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, ModelWithEncodedNames input, + RequestContext requestContext); + } + + private static final class ModelWithEncodedNamesValuesServiceImpl implements ModelWithEncodedNamesValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ModelWithEncodedNamesValuesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ModelWithEncodedNamesValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/payload/xml/modelWithEncodedNames"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, ModelWithEncodedNames.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, ModelWithEncodedNames input, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/payload/xml/modelWithEncodedNames"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/payload/xml/modelWithEncodedNames", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/payload/xml/modelWithEncodedNames", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("content-type") String contentType, - @BodyParam("application/xml") ModelWithEncodedNames input, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithEnumValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithEnumValuesImpl.java index 5455261977f..99b99a13446 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithEnumValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithEnumValuesImpl.java @@ -1,20 +1,23 @@ package payload.xml.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import payload.xml.ModelWithEnum; /** @@ -48,38 +51,89 @@ public final class ModelWithEnumValuesImpl { } /** - * The interface defining all the services for XmlClientModelWithEnumValues to be used by the proxy service to - * perform REST calls. + * The HTTP operations for XmlClientModelWithEnumValues. */ - @ServiceInterface(name = "XmlClientModelWithEnumValues", host = "{endpoint}") public interface ModelWithEnumValuesService { static ModelWithEnumValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("payload.xml.implementation.ModelWithEnumValuesServiceImpl"); - return (ModelWithEnumValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new ModelWithEnumValuesServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, ModelWithEnum input, RequestContext requestContext); + } + + private static final class ModelWithEnumValuesServiceImpl implements ModelWithEnumValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ModelWithEnumValuesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ModelWithEnumValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/payload/xml/modelWithEnum"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, ModelWithEnum.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, ModelWithEnum input, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/payload/xml/modelWithEnum"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/payload/xml/modelWithEnum", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/payload/xml/modelWithEnum", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("content-type") String contentType, - @BodyParam("application/xml") ModelWithEnum input, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithNamespaceOnPropertiesValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithNamespaceOnPropertiesValuesImpl.java index 6954b9b693c..302290a2408 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithNamespaceOnPropertiesValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithNamespaceOnPropertiesValuesImpl.java @@ -1,20 +1,23 @@ package payload.xml.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import payload.xml.ModelWithNamespaceOnProperties; /** @@ -48,40 +51,93 @@ public final class ModelWithNamespaceOnPropertiesValuesImpl { } /** - * The interface defining all the services for XmlClientModelWithNamespaceOnPropertiesValues to be used by the proxy - * service to perform REST calls. + * The HTTP operations for XmlClientModelWithNamespaceOnPropertiesValues. */ - @ServiceInterface(name = "XmlClientModelWithNamespaceOnPropertiesValues", host = "{endpoint}") public interface ModelWithNamespaceOnPropertiesValuesService { static ModelWithNamespaceOnPropertiesValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz - = Class.forName("payload.xml.implementation.ModelWithNamespaceOnPropertiesValuesServiceImpl"); - return (ModelWithNamespaceOnPropertiesValuesService) clazz - .getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new ModelWithNamespaceOnPropertiesValuesServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, ModelWithNamespaceOnProperties input, + RequestContext requestContext); + } + + private static final class ModelWithNamespaceOnPropertiesValuesServiceImpl + implements ModelWithNamespaceOnPropertiesValuesService { + private static final ClientLogger HTTP_LOG + = new ClientLogger(ModelWithNamespaceOnPropertiesValuesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ModelWithNamespaceOnPropertiesValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/payload/xml/modelWithNamespaceOnProperties"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, ModelWithNamespaceOnProperties.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, ModelWithNamespaceOnProperties input, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/payload/xml/modelWithNamespaceOnProperties"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/payload/xml/modelWithNamespaceOnProperties", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/payload/xml/modelWithNamespaceOnProperties", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("content-type") String contentType, - @BodyParam("application/xml") ModelWithNamespaceOnProperties input, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithNamespaceValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithNamespaceValuesImpl.java index d92e3a74134..86722ab8992 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithNamespaceValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithNamespaceValuesImpl.java @@ -1,20 +1,23 @@ package payload.xml.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import payload.xml.ModelWithNamespace; /** @@ -48,38 +51,90 @@ public final class ModelWithNamespaceValuesImpl { } /** - * The interface defining all the services for XmlClientModelWithNamespaceValues to be used by the proxy service to - * perform REST calls. + * The HTTP operations for XmlClientModelWithNamespaceValues. */ - @ServiceInterface(name = "XmlClientModelWithNamespaceValues", host = "{endpoint}") public interface ModelWithNamespaceValuesService { static ModelWithNamespaceValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("payload.xml.implementation.ModelWithNamespaceValuesServiceImpl"); - return (ModelWithNamespaceValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new ModelWithNamespaceValuesServiceImpl(pipeline); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/payload/xml/modelWithNamespace", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, ModelWithNamespace input, RequestContext requestContext); + } + + private static final class ModelWithNamespaceValuesServiceImpl implements ModelWithNamespaceValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ModelWithNamespaceValuesService.class); + + private final HttpPipeline httpPipeline; - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/payload/xml/modelWithNamespace", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("content-type") String contentType, - @BodyParam("application/xml") ModelWithNamespace input, RequestContext requestContext); + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ModelWithNamespaceValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/payload/xml/modelWithNamespace"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, ModelWithNamespace.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response put(String endpoint, String contentType, ModelWithNamespace input, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/payload/xml/modelWithNamespace"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithNestedModelValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithNestedModelValuesImpl.java index d8b0049ab67..a4562210730 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithNestedModelValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithNestedModelValuesImpl.java @@ -1,20 +1,23 @@ package payload.xml.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import payload.xml.ModelWithNestedModel; /** @@ -48,38 +51,90 @@ public final class ModelWithNestedModelValuesImpl { } /** - * The interface defining all the services for XmlClientModelWithNestedModelValues to be used by the proxy service - * to perform REST calls. + * The HTTP operations for XmlClientModelWithNestedModelValues. */ - @ServiceInterface(name = "XmlClientModelWithNestedModelValues", host = "{endpoint}") public interface ModelWithNestedModelValuesService { static ModelWithNestedModelValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("payload.xml.implementation.ModelWithNestedModelValuesServiceImpl"); - return (ModelWithNestedModelValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new ModelWithNestedModelValuesServiceImpl(pipeline); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/payload/xml/modelWithNestedModel", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, ModelWithNestedModel input, RequestContext requestContext); + } + + private static final class ModelWithNestedModelValuesServiceImpl implements ModelWithNestedModelValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ModelWithNestedModelValuesService.class); + + private final HttpPipeline httpPipeline; - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/payload/xml/modelWithNestedModel", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("content-type") String contentType, - @BodyParam("application/xml") ModelWithNestedModel input, RequestContext requestContext); + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ModelWithNestedModelValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/payload/xml/modelWithNestedModel"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, ModelWithNestedModel.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response put(String endpoint, String contentType, ModelWithNestedModel input, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/payload/xml/modelWithNestedModel"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithOptionalFieldValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithOptionalFieldValuesImpl.java index ce0ce4a72a1..09cb9721949 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithOptionalFieldValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithOptionalFieldValuesImpl.java @@ -1,20 +1,23 @@ package payload.xml.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import payload.xml.ModelWithOptionalField; /** @@ -48,38 +51,90 @@ public final class ModelWithOptionalFieldValuesImpl { } /** - * The interface defining all the services for XmlClientModelWithOptionalFieldValues to be used by the proxy service - * to perform REST calls. + * The HTTP operations for XmlClientModelWithOptionalFieldValues. */ - @ServiceInterface(name = "XmlClientModelWithOptionalFieldValues", host = "{endpoint}") public interface ModelWithOptionalFieldValuesService { static ModelWithOptionalFieldValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("payload.xml.implementation.ModelWithOptionalFieldValuesServiceImpl"); - return (ModelWithOptionalFieldValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new ModelWithOptionalFieldValuesServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, ModelWithOptionalField input, + RequestContext requestContext); + } + + private static final class ModelWithOptionalFieldValuesServiceImpl implements ModelWithOptionalFieldValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ModelWithOptionalFieldValuesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ModelWithOptionalFieldValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/payload/xml/modelWithOptionalField"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, ModelWithOptionalField.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, ModelWithOptionalField input, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/payload/xml/modelWithOptionalField"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/payload/xml/modelWithOptionalField", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/payload/xml/modelWithOptionalField", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("content-type") String contentType, - @BodyParam("application/xml") ModelWithOptionalField input, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithRenamedArraysValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithRenamedArraysValuesImpl.java index 1a3d6ebd070..80d881ca95a 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithRenamedArraysValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithRenamedArraysValuesImpl.java @@ -1,20 +1,23 @@ package payload.xml.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import payload.xml.ModelWithRenamedArrays; /** @@ -48,38 +51,90 @@ public final class ModelWithRenamedArraysValuesImpl { } /** - * The interface defining all the services for XmlClientModelWithRenamedArraysValues to be used by the proxy service - * to perform REST calls. + * The HTTP operations for XmlClientModelWithRenamedArraysValues. */ - @ServiceInterface(name = "XmlClientModelWithRenamedArraysValues", host = "{endpoint}") public interface ModelWithRenamedArraysValuesService { static ModelWithRenamedArraysValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("payload.xml.implementation.ModelWithRenamedArraysValuesServiceImpl"); - return (ModelWithRenamedArraysValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new ModelWithRenamedArraysValuesServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, ModelWithRenamedArrays input, + RequestContext requestContext); + } + + private static final class ModelWithRenamedArraysValuesServiceImpl implements ModelWithRenamedArraysValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ModelWithRenamedArraysValuesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ModelWithRenamedArraysValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/payload/xml/modelWithRenamedArrays"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, ModelWithRenamedArrays.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, ModelWithRenamedArrays input, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/payload/xml/modelWithRenamedArrays"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/payload/xml/modelWithRenamedArrays", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/payload/xml/modelWithRenamedArrays", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("content-type") String contentType, - @BodyParam("application/xml") ModelWithRenamedArrays input, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithRenamedAttributeValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithRenamedAttributeValuesImpl.java index 409a8cc4521..3c7c4ff75a9 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithRenamedAttributeValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithRenamedAttributeValuesImpl.java @@ -1,20 +1,23 @@ package payload.xml.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import payload.xml.ModelWithRenamedAttribute; /** @@ -48,38 +51,91 @@ public final class ModelWithRenamedAttributeValuesImpl { } /** - * The interface defining all the services for XmlClientModelWithRenamedAttributeValues to be used by the proxy - * service to perform REST calls. + * The HTTP operations for XmlClientModelWithRenamedAttributeValues. */ - @ServiceInterface(name = "XmlClientModelWithRenamedAttributeValues", host = "{endpoint}") public interface ModelWithRenamedAttributeValuesService { static ModelWithRenamedAttributeValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("payload.xml.implementation.ModelWithRenamedAttributeValuesServiceImpl"); - return (ModelWithRenamedAttributeValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new ModelWithRenamedAttributeValuesServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, ModelWithRenamedAttribute input, + RequestContext requestContext); + } + + private static final class ModelWithRenamedAttributeValuesServiceImpl + implements ModelWithRenamedAttributeValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ModelWithRenamedAttributeValuesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ModelWithRenamedAttributeValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/payload/xml/modelWithRenamedAttribute"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, ModelWithRenamedAttribute.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, ModelWithRenamedAttribute input, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/payload/xml/modelWithRenamedAttribute"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/payload/xml/modelWithRenamedAttribute", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/payload/xml/modelWithRenamedAttribute", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("content-type") String contentType, - @BodyParam("application/xml") ModelWithRenamedAttribute input, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithRenamedFieldsValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithRenamedFieldsValuesImpl.java index 92d4c20b7da..99ab8d14441 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithRenamedFieldsValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithRenamedFieldsValuesImpl.java @@ -1,20 +1,23 @@ package payload.xml.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import payload.xml.ModelWithRenamedFields; /** @@ -48,38 +51,90 @@ public final class ModelWithRenamedFieldsValuesImpl { } /** - * The interface defining all the services for XmlClientModelWithRenamedFieldsValues to be used by the proxy service - * to perform REST calls. + * The HTTP operations for XmlClientModelWithRenamedFieldsValues. */ - @ServiceInterface(name = "XmlClientModelWithRenamedFieldsValues", host = "{endpoint}") public interface ModelWithRenamedFieldsValuesService { static ModelWithRenamedFieldsValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("payload.xml.implementation.ModelWithRenamedFieldsValuesServiceImpl"); - return (ModelWithRenamedFieldsValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new ModelWithRenamedFieldsValuesServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, ModelWithRenamedFields input, + RequestContext requestContext); + } + + private static final class ModelWithRenamedFieldsValuesServiceImpl implements ModelWithRenamedFieldsValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ModelWithRenamedFieldsValuesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ModelWithRenamedFieldsValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/payload/xml/modelWithRenamedFields"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, ModelWithRenamedFields.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, ModelWithRenamedFields input, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/payload/xml/modelWithRenamedFields"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/payload/xml/modelWithRenamedFields", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/payload/xml/modelWithRenamedFields", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("content-type") String contentType, - @BodyParam("application/xml") ModelWithRenamedFields input, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithRenamedNestedModelValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithRenamedNestedModelValuesImpl.java index d0972aeaf97..ed9157d7d92 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithRenamedNestedModelValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithRenamedNestedModelValuesImpl.java @@ -1,20 +1,23 @@ package payload.xml.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import payload.xml.ModelWithRenamedNestedModel; /** @@ -48,39 +51,92 @@ public final class ModelWithRenamedNestedModelValuesImpl { } /** - * The interface defining all the services for XmlClientModelWithRenamedNestedModelValues to be used by the proxy - * service to perform REST calls. + * The HTTP operations for XmlClientModelWithRenamedNestedModelValues. */ - @ServiceInterface(name = "XmlClientModelWithRenamedNestedModelValues", host = "{endpoint}") public interface ModelWithRenamedNestedModelValuesService { static ModelWithRenamedNestedModelValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz - = Class.forName("payload.xml.implementation.ModelWithRenamedNestedModelValuesServiceImpl"); - return (ModelWithRenamedNestedModelValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new ModelWithRenamedNestedModelValuesServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, ModelWithRenamedNestedModel input, + RequestContext requestContext); + } + + private static final class ModelWithRenamedNestedModelValuesServiceImpl + implements ModelWithRenamedNestedModelValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ModelWithRenamedNestedModelValuesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ModelWithRenamedNestedModelValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/payload/xml/modelWithRenamedNestedModel"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, ModelWithRenamedNestedModel.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, ModelWithRenamedNestedModel input, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/payload/xml/modelWithRenamedNestedModel"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/payload/xml/modelWithRenamedNestedModel", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/payload/xml/modelWithRenamedNestedModel", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("content-type") String contentType, - @BodyParam("application/xml") ModelWithRenamedNestedModel input, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithRenamedPropertyValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithRenamedPropertyValuesImpl.java index ecb76372770..65c2a36c6a0 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithRenamedPropertyValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithRenamedPropertyValuesImpl.java @@ -1,20 +1,23 @@ package payload.xml.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import payload.xml.ModelWithRenamedProperty; /** @@ -48,38 +51,91 @@ public final class ModelWithRenamedPropertyValuesImpl { } /** - * The interface defining all the services for XmlClientModelWithRenamedPropertyValues to be used by the proxy - * service to perform REST calls. + * The HTTP operations for XmlClientModelWithRenamedPropertyValues. */ - @ServiceInterface(name = "XmlClientModelWithRenamedPropertyValues", host = "{endpoint}") public interface ModelWithRenamedPropertyValuesService { static ModelWithRenamedPropertyValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("payload.xml.implementation.ModelWithRenamedPropertyValuesServiceImpl"); - return (ModelWithRenamedPropertyValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new ModelWithRenamedPropertyValuesServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, ModelWithRenamedProperty input, + RequestContext requestContext); + } + + private static final class ModelWithRenamedPropertyValuesServiceImpl + implements ModelWithRenamedPropertyValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ModelWithRenamedPropertyValuesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ModelWithRenamedPropertyValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/payload/xml/modelWithRenamedProperty"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, ModelWithRenamedProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, ModelWithRenamedProperty input, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/payload/xml/modelWithRenamedProperty"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/payload/xml/modelWithRenamedProperty", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/payload/xml/modelWithRenamedProperty", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("content-type") String contentType, - @BodyParam("application/xml") ModelWithRenamedProperty input, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithRenamedUnwrappedModelArrayValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithRenamedUnwrappedModelArrayValuesImpl.java index 70b0c0a2dbd..d41da3e27a9 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithRenamedUnwrappedModelArrayValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithRenamedUnwrappedModelArrayValuesImpl.java @@ -1,20 +1,23 @@ package payload.xml.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import payload.xml.ModelWithRenamedUnwrappedModelArray; /** @@ -48,40 +51,94 @@ public final class ModelWithRenamedUnwrappedModelArrayValuesImpl { } /** - * The interface defining all the services for XmlClientModelWithRenamedUnwrappedModelArrayValues to be used by the - * proxy service to perform REST calls. + * The HTTP operations for XmlClientModelWithRenamedUnwrappedModelArrayValues. */ - @ServiceInterface(name = "XmlClientModelWithRenamedUnwrappedModelArrayValues", host = "{endpoint}") public interface ModelWithRenamedUnwrappedModelArrayValuesService { static ModelWithRenamedUnwrappedModelArrayValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz - = Class.forName("payload.xml.implementation.ModelWithRenamedUnwrappedModelArrayValuesServiceImpl"); - return (ModelWithRenamedUnwrappedModelArrayValuesService) clazz - .getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new ModelWithRenamedUnwrappedModelArrayValuesServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, + RequestContext requestContext); + + Response put(String endpoint, String contentType, ModelWithRenamedUnwrappedModelArray input, + RequestContext requestContext); + } + + private static final class ModelWithRenamedUnwrappedModelArrayValuesServiceImpl + implements ModelWithRenamedUnwrappedModelArrayValuesService { + private static final ClientLogger HTTP_LOG + = new ClientLogger(ModelWithRenamedUnwrappedModelArrayValuesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ModelWithRenamedUnwrappedModelArrayValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/payload/xml/modelWithRenamedUnwrappedModelArray"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, ModelWithRenamedUnwrappedModelArray.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, ModelWithRenamedUnwrappedModelArray input, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/payload/xml/modelWithRenamedUnwrappedModelArray"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/payload/xml/modelWithRenamedUnwrappedModelArray", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/payload/xml/modelWithRenamedUnwrappedModelArray", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("content-type") String contentType, - @BodyParam("application/xml") ModelWithRenamedUnwrappedModelArray input, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithRenamedWrappedAndItemModelArrayValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithRenamedWrappedAndItemModelArrayValuesImpl.java index 25696709af9..390216c1d65 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithRenamedWrappedAndItemModelArrayValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithRenamedWrappedAndItemModelArrayValuesImpl.java @@ -1,20 +1,23 @@ package payload.xml.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import payload.xml.ModelWithRenamedWrappedAndItemModelArray; /** @@ -49,41 +52,94 @@ public final class ModelWithRenamedWrappedAndItemModelArrayValuesImpl { } /** - * The interface defining all the services for XmlClientModelWithRenamedWrappedAndItemModelArrayValues to be used by - * the proxy service to perform REST calls. + * The HTTP operations for XmlClientModelWithRenamedWrappedAndItemModelArrayValues. */ - @ServiceInterface(name = "XmlClientModelWithRenamedWrappedAndItemModelArrayValues", host = "{endpoint}") public interface ModelWithRenamedWrappedAndItemModelArrayValuesService { static ModelWithRenamedWrappedAndItemModelArrayValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class - .forName("payload.xml.implementation.ModelWithRenamedWrappedAndItemModelArrayValuesServiceImpl"); - return (ModelWithRenamedWrappedAndItemModelArrayValuesService) clazz - .getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new ModelWithRenamedWrappedAndItemModelArrayValuesServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, + RequestContext requestContext); + + Response put(String endpoint, String contentType, ModelWithRenamedWrappedAndItemModelArray input, + RequestContext requestContext); + } + + private static final class ModelWithRenamedWrappedAndItemModelArrayValuesServiceImpl + implements ModelWithRenamedWrappedAndItemModelArrayValuesService { + private static final ClientLogger HTTP_LOG + = new ClientLogger(ModelWithRenamedWrappedAndItemModelArrayValuesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ModelWithRenamedWrappedAndItemModelArrayValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/payload/xml/modelWithRenamedWrappedAndItemModelArray"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, ModelWithRenamedWrappedAndItemModelArray.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, ModelWithRenamedWrappedAndItemModelArray input, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/payload/xml/modelWithRenamedWrappedAndItemModelArray"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/payload/xml/modelWithRenamedWrappedAndItemModelArray", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/payload/xml/modelWithRenamedWrappedAndItemModelArray", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("content-type") String contentType, - @BodyParam("application/xml") ModelWithRenamedWrappedAndItemModelArray input, - RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithRenamedWrappedModelArrayValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithRenamedWrappedModelArrayValuesImpl.java index f5243db600b..cf19a5fa413 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithRenamedWrappedModelArrayValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithRenamedWrappedModelArrayValuesImpl.java @@ -1,20 +1,23 @@ package payload.xml.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import payload.xml.ModelWithRenamedWrappedModelArray; /** @@ -48,40 +51,93 @@ public final class ModelWithRenamedWrappedModelArrayValuesImpl { } /** - * The interface defining all the services for XmlClientModelWithRenamedWrappedModelArrayValues to be used by the - * proxy service to perform REST calls. + * The HTTP operations for XmlClientModelWithRenamedWrappedModelArrayValues. */ - @ServiceInterface(name = "XmlClientModelWithRenamedWrappedModelArrayValues", host = "{endpoint}") public interface ModelWithRenamedWrappedModelArrayValuesService { static ModelWithRenamedWrappedModelArrayValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz - = Class.forName("payload.xml.implementation.ModelWithRenamedWrappedModelArrayValuesServiceImpl"); - return (ModelWithRenamedWrappedModelArrayValuesService) clazz - .getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new ModelWithRenamedWrappedModelArrayValuesServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, ModelWithRenamedWrappedModelArray input, + RequestContext requestContext); + } + + private static final class ModelWithRenamedWrappedModelArrayValuesServiceImpl + implements ModelWithRenamedWrappedModelArrayValuesService { + private static final ClientLogger HTTP_LOG + = new ClientLogger(ModelWithRenamedWrappedModelArrayValuesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ModelWithRenamedWrappedModelArrayValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/payload/xml/modelWithRenamedWrappedModelArray"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, ModelWithRenamedWrappedModelArray.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, ModelWithRenamedWrappedModelArray input, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/payload/xml/modelWithRenamedWrappedModelArray"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/payload/xml/modelWithRenamedWrappedModelArray", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/payload/xml/modelWithRenamedWrappedModelArray", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("content-type") String contentType, - @BodyParam("application/xml") ModelWithRenamedWrappedModelArray input, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithSimpleArraysValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithSimpleArraysValuesImpl.java index 7b6b8734f48..cd06d9346e7 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithSimpleArraysValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithSimpleArraysValuesImpl.java @@ -1,20 +1,23 @@ package payload.xml.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import payload.xml.ModelWithSimpleArrays; /** @@ -48,38 +51,90 @@ public final class ModelWithSimpleArraysValuesImpl { } /** - * The interface defining all the services for XmlClientModelWithSimpleArraysValues to be used by the proxy service - * to perform REST calls. + * The HTTP operations for XmlClientModelWithSimpleArraysValues. */ - @ServiceInterface(name = "XmlClientModelWithSimpleArraysValues", host = "{endpoint}") public interface ModelWithSimpleArraysValuesService { static ModelWithSimpleArraysValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("payload.xml.implementation.ModelWithSimpleArraysValuesServiceImpl"); - return (ModelWithSimpleArraysValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new ModelWithSimpleArraysValuesServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, ModelWithSimpleArrays input, + RequestContext requestContext); + } + + private static final class ModelWithSimpleArraysValuesServiceImpl implements ModelWithSimpleArraysValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ModelWithSimpleArraysValuesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ModelWithSimpleArraysValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/payload/xml/modelWithSimpleArrays"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, ModelWithSimpleArrays.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, ModelWithSimpleArrays input, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/payload/xml/modelWithSimpleArrays"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/payload/xml/modelWithSimpleArrays", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/payload/xml/modelWithSimpleArrays", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("content-type") String contentType, - @BodyParam("application/xml") ModelWithSimpleArrays input, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithTextValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithTextValuesImpl.java index 6c874932fa8..6e38a6ff725 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithTextValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithTextValuesImpl.java @@ -1,20 +1,23 @@ package payload.xml.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import payload.xml.ModelWithText; /** @@ -48,38 +51,89 @@ public final class ModelWithTextValuesImpl { } /** - * The interface defining all the services for XmlClientModelWithTextValues to be used by the proxy service to - * perform REST calls. + * The HTTP operations for XmlClientModelWithTextValues. */ - @ServiceInterface(name = "XmlClientModelWithTextValues", host = "{endpoint}") public interface ModelWithTextValuesService { static ModelWithTextValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("payload.xml.implementation.ModelWithTextValuesServiceImpl"); - return (ModelWithTextValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new ModelWithTextValuesServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, ModelWithText input, RequestContext requestContext); + } + + private static final class ModelWithTextValuesServiceImpl implements ModelWithTextValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ModelWithTextValuesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ModelWithTextValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/payload/xml/modelWithText"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, ModelWithText.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, ModelWithText input, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/payload/xml/modelWithText"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/payload/xml/modelWithText", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/payload/xml/modelWithText", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("content-type") String contentType, - @BodyParam("application/xml") ModelWithText input, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithUnwrappedArrayValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithUnwrappedArrayValuesImpl.java index 448d98ceb97..9299f06884e 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithUnwrappedArrayValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithUnwrappedArrayValuesImpl.java @@ -1,20 +1,23 @@ package payload.xml.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import payload.xml.ModelWithUnwrappedArray; /** @@ -48,38 +51,91 @@ public final class ModelWithUnwrappedArrayValuesImpl { } /** - * The interface defining all the services for XmlClientModelWithUnwrappedArrayValues to be used by the proxy - * service to perform REST calls. + * The HTTP operations for XmlClientModelWithUnwrappedArrayValues. */ - @ServiceInterface(name = "XmlClientModelWithUnwrappedArrayValues", host = "{endpoint}") public interface ModelWithUnwrappedArrayValuesService { static ModelWithUnwrappedArrayValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("payload.xml.implementation.ModelWithUnwrappedArrayValuesServiceImpl"); - return (ModelWithUnwrappedArrayValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new ModelWithUnwrappedArrayValuesServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, ModelWithUnwrappedArray input, + RequestContext requestContext); + } + + private static final class ModelWithUnwrappedArrayValuesServiceImpl + implements ModelWithUnwrappedArrayValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ModelWithUnwrappedArrayValuesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ModelWithUnwrappedArrayValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/payload/xml/modelWithUnwrappedArray"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, ModelWithUnwrappedArray.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, ModelWithUnwrappedArray input, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/payload/xml/modelWithUnwrappedArray"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/payload/xml/modelWithUnwrappedArray", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/payload/xml/modelWithUnwrappedArray", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("content-type") String contentType, - @BodyParam("application/xml") ModelWithUnwrappedArray input, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithUnwrappedModelArrayValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithUnwrappedModelArrayValuesImpl.java index 9dbcdc7f1aa..5ca4f64e9f7 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithUnwrappedModelArrayValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithUnwrappedModelArrayValuesImpl.java @@ -1,20 +1,23 @@ package payload.xml.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import payload.xml.ModelWithUnwrappedModelArray; /** @@ -48,39 +51,92 @@ public final class ModelWithUnwrappedModelArrayValuesImpl { } /** - * The interface defining all the services for XmlClientModelWithUnwrappedModelArrayValues to be used by the proxy - * service to perform REST calls. + * The HTTP operations for XmlClientModelWithUnwrappedModelArrayValues. */ - @ServiceInterface(name = "XmlClientModelWithUnwrappedModelArrayValues", host = "{endpoint}") public interface ModelWithUnwrappedModelArrayValuesService { static ModelWithUnwrappedModelArrayValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz - = Class.forName("payload.xml.implementation.ModelWithUnwrappedModelArrayValuesServiceImpl"); - return (ModelWithUnwrappedModelArrayValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new ModelWithUnwrappedModelArrayValuesServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, ModelWithUnwrappedModelArray input, + RequestContext requestContext); + } + + private static final class ModelWithUnwrappedModelArrayValuesServiceImpl + implements ModelWithUnwrappedModelArrayValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ModelWithUnwrappedModelArrayValuesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ModelWithUnwrappedModelArrayValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/payload/xml/modelWithUnwrappedModelArray"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, ModelWithUnwrappedModelArray.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, ModelWithUnwrappedModelArray input, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/payload/xml/modelWithUnwrappedModelArray"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/payload/xml/modelWithUnwrappedModelArray", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/payload/xml/modelWithUnwrappedModelArray", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("content-type") String contentType, - @BodyParam("application/xml") ModelWithUnwrappedModelArray input, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithWrappedPrimitiveCustomItemNamesValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithWrappedPrimitiveCustomItemNamesValuesImpl.java index 796c5801467..2898533bccf 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithWrappedPrimitiveCustomItemNamesValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/ModelWithWrappedPrimitiveCustomItemNamesValuesImpl.java @@ -1,20 +1,23 @@ package payload.xml.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import payload.xml.ModelWithWrappedPrimitiveCustomItemNames; /** @@ -49,41 +52,94 @@ public final class ModelWithWrappedPrimitiveCustomItemNamesValuesImpl { } /** - * The interface defining all the services for XmlClientModelWithWrappedPrimitiveCustomItemNamesValues to be used by - * the proxy service to perform REST calls. + * The HTTP operations for XmlClientModelWithWrappedPrimitiveCustomItemNamesValues. */ - @ServiceInterface(name = "XmlClientModelWithWrappedPrimitiveCustomItemNamesValues", host = "{endpoint}") public interface ModelWithWrappedPrimitiveCustomItemNamesValuesService { static ModelWithWrappedPrimitiveCustomItemNamesValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class - .forName("payload.xml.implementation.ModelWithWrappedPrimitiveCustomItemNamesValuesServiceImpl"); - return (ModelWithWrappedPrimitiveCustomItemNamesValuesService) clazz - .getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new ModelWithWrappedPrimitiveCustomItemNamesValuesServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, + RequestContext requestContext); + + Response put(String endpoint, String contentType, ModelWithWrappedPrimitiveCustomItemNames input, + RequestContext requestContext); + } + + private static final class ModelWithWrappedPrimitiveCustomItemNamesValuesServiceImpl + implements ModelWithWrappedPrimitiveCustomItemNamesValuesService { + private static final ClientLogger HTTP_LOG + = new ClientLogger(ModelWithWrappedPrimitiveCustomItemNamesValuesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ModelWithWrappedPrimitiveCustomItemNamesValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/payload/xml/modelWithWrappedPrimitiveCustomItemNames"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, ModelWithWrappedPrimitiveCustomItemNames.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, ModelWithWrappedPrimitiveCustomItemNames input, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/payload/xml/modelWithWrappedPrimitiveCustomItemNames"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/payload/xml/modelWithWrappedPrimitiveCustomItemNames", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/payload/xml/modelWithWrappedPrimitiveCustomItemNames", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("content-type") String contentType, - @BodyParam("application/xml") ModelWithWrappedPrimitiveCustomItemNames input, - RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/SimpleModelValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/SimpleModelValuesImpl.java index f0fbdbe02f1..af3b2f7c69a 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/SimpleModelValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/SimpleModelValuesImpl.java @@ -1,20 +1,23 @@ package payload.xml.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import payload.xml.SimpleModel; /** @@ -48,38 +51,89 @@ public final class SimpleModelValuesImpl { } /** - * The interface defining all the services for XmlClientSimpleModelValues to be used by the proxy service to perform - * REST calls. + * The HTTP operations for XmlClientSimpleModelValues. */ - @ServiceInterface(name = "XmlClientSimpleModelValues", host = "{endpoint}") public interface SimpleModelValuesService { static SimpleModelValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("payload.xml.implementation.SimpleModelValuesServiceImpl"); - return (SimpleModelValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new SimpleModelValuesServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, SimpleModel input, RequestContext requestContext); + } + + private static final class SimpleModelValuesServiceImpl implements SimpleModelValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(SimpleModelValuesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private SimpleModelValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/payload/xml/simpleModel"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, SimpleModel.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, SimpleModel input, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/payload/xml/simpleModel"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/payload/xml/simpleModel", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/payload/xml/simpleModel", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("content-type") String contentType, - @BodyParam("application/xml") SimpleModel input, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/XmlErrorValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/XmlErrorValuesImpl.java index 78d41aa2ed9..476de51f91a 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/XmlErrorValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/xml/implementation/XmlErrorValuesImpl.java @@ -1,21 +1,25 @@ package payload.xml.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; +import java.util.Map; import payload.xml.SimpleModel; -import payload.xml.XmlErrorBody; /** * An instance of this class provides access to all the operations defined in XmlErrorValues. @@ -48,28 +52,64 @@ public final class XmlErrorValuesImpl { } /** - * The interface defining all the services for XmlClientXmlErrorValues to be used by the proxy service to perform - * REST calls. + * The HTTP operations for XmlClientXmlErrorValues. */ - @ServiceInterface(name = "XmlClientXmlErrorValues", host = "{endpoint}") public interface XmlErrorValuesService { static XmlErrorValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("payload.xml.implementation.XmlErrorValuesServiceImpl"); - return (XmlErrorValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new XmlErrorValuesServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + } + + private static final class XmlErrorValuesServiceImpl implements XmlErrorValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(XmlErrorValuesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + private XmlErrorValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation(method = HttpMethod.GET, path = "/payload/xml/error", expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail(statusCode = { 400 }, exceptionBodyClass = XmlErrorBody.class) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/payload/xml/error"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, + Map.ofEntries(Map.entry(400, CoreUtils.createParameterizedType(payload.xml.XmlErrorBody.class))), + HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, SimpleModel.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/response/bodyornocontent/implementation/BodyOrNoContentClientImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/response/bodyornocontent/implementation/BodyOrNoContentClientImpl.java index 8851547e4cb..fcb6048f592 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/response/bodyornocontent/implementation/BodyOrNoContentClientImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/response/bodyornocontent/implementation/BodyOrNoContentClientImpl.java @@ -1,19 +1,23 @@ package response.bodyornocontent.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import response.bodyornocontent.BlobLayout; /** @@ -82,39 +86,86 @@ public BodyOrNoContentClientImpl(HttpPipeline httpPipeline, Instrumentation inst } /** - * The interface defining all the services for BodyOrNoContentClient to be used by the proxy service to perform REST - * calls. + * The HTTP operations for BodyOrNoContentClient. */ - @ServiceInterface(name = "BodyOrNoContentClient", host = "{endpoint}") public interface BodyOrNoContentClientService { static BodyOrNoContentClientService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz - = Class.forName("response.bodyornocontent.implementation.BodyOrNoContentClientServiceImpl"); - return (BodyOrNoContentClientService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new BodyOrNoContentClientServiceImpl(pipeline); + } + + Response getBody(String endpoint, String accept, RequestContext requestContext); + + Response getNoContent(String endpoint, String accept, RequestContext requestContext); + } + + private static final class BodyOrNoContentClientServiceImpl implements BodyOrNoContentClientService { + private static final ClientLogger HTTP_LOG = new ClientLogger(BodyOrNoContentClientService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private BodyOrNoContentClientServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response getBody(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/response/body-or-no-content/body"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200 || responseCode == 204; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, BlobLayout.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response getNoContent(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/response/body-or-no-content/no-content"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200 || responseCode == 204; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, BlobLayout.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/response/body-or-no-content/body", - expectedStatusCodes = { 200, 204 }) - @UnexpectedResponseExceptionDetail - Response getBody(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/response/body-or-no-content/no-content", - expectedStatusCodes = { 200, 204 }) - @UnexpectedResponseExceptionDetail - Response getNoContent(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/response/statuscoderange/implementation/StatusCodeRangeClientImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/response/statuscoderange/implementation/StatusCodeRangeClientImpl.java index a7ad820ffe6..02d34de093d 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/response/statuscoderange/implementation/StatusCodeRangeClientImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/response/statuscoderange/implementation/StatusCodeRangeClientImpl.java @@ -1,22 +1,21 @@ package response.statuscoderange.implementation; 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 response.statuscoderange.DefaultError; -import response.statuscoderange.ErrorInRange; -import response.statuscoderange.NotFoundError; -import response.statuscoderange.Standard4XXError; +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 StatusCodeRangeClient type. @@ -84,145 +83,281 @@ public StatusCodeRangeClientImpl(HttpPipeline httpPipeline, Instrumentation inst } /** - * The interface defining all the services for StatusCodeRangeClient to be used by the proxy service to perform REST - * calls. + * The HTTP operations for StatusCodeRangeClient. */ - @ServiceInterface(name = "StatusCodeRangeClient", host = "{endpoint}") public interface StatusCodeRangeClientService { static StatusCodeRangeClientService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz - = Class.forName("response.statuscoderange.implementation.StatusCodeRangeClientServiceImpl"); - return (StatusCodeRangeClientService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new StatusCodeRangeClientServiceImpl(pipeline); + } + + Response errorResponseStatusCodeInRange(String endpoint, RequestContext requestContext); + + Response errorResponseStatusCode404(String endpoint, RequestContext requestContext); + } + + private static final class StatusCodeRangeClientServiceImpl implements StatusCodeRangeClientService { + private static final ClientLogger HTTP_LOG = new ClientLogger(StatusCodeRangeClientService.class); + + private final HttpPipeline httpPipeline; + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private StatusCodeRangeClientServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/response/status-code-range/error-response-status-code-in-range", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail( - statusCode = { 494, 495, 496, 497, 498, 499 }, - exceptionBodyClass = ErrorInRange.class) - @UnexpectedResponseExceptionDetail(exceptionBodyClass = DefaultError.class) - Response errorResponseStatusCodeInRange(@HostParam("endpoint") String endpoint, - RequestContext requestContext); + @Override + public Response errorResponseStatusCodeInRange(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/response/status-code-range/error-response-status-code-in-range"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response networkResponse = this.httpPipeline.send(httpRequest)) { + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 204; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, + CoreUtils.createParameterizedType(response.statuscoderange.DefaultError.class), + Map.ofEntries( + Map.entry(494, + CoreUtils.createParameterizedType(response.statuscoderange.ErrorInRange.class)), + Map.entry(495, + CoreUtils.createParameterizedType(response.statuscoderange.ErrorInRange.class)), + Map.entry(496, + CoreUtils.createParameterizedType(response.statuscoderange.ErrorInRange.class)), + Map.entry(497, + CoreUtils.createParameterizedType(response.statuscoderange.ErrorInRange.class)), + Map.entry(498, + CoreUtils.createParameterizedType(response.statuscoderange.ErrorInRange.class)), + Map.entry(499, + CoreUtils.createParameterizedType(response.statuscoderange.ErrorInRange.class))), + HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), null); + } + } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/response/status-code-range/error-response-status-code-404", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail( - statusCode = { - 400, - 401, - 402, - 403, - 405, - 406, - 407, - 408, - 409, - 410, - 411, - 412, - 413, - 414, - 415, - 416, - 417, - 418, - 419, - 420, - 421, - 422, - 423, - 424, - 425, - 426, - 427, - 428, - 429, - 430, - 431, - 432, - 433, - 434, - 435, - 436, - 437, - 438, - 439, - 440, - 441, - 442, - 443, - 444, - 445, - 446, - 447, - 448, - 449, - 450, - 451, - 452, - 453, - 454, - 455, - 456, - 457, - 458, - 459, - 460, - 461, - 462, - 463, - 464, - 465, - 466, - 467, - 468, - 469, - 470, - 471, - 472, - 473, - 474, - 475, - 476, - 477, - 478, - 479, - 480, - 481, - 482, - 483, - 484, - 485, - 486, - 487, - 488, - 489, - 490, - 491, - 492, - 493, - 494, - 495, - 496, - 497, - 498, - 499 }, - exceptionBodyClass = Standard4XXError.class) - @UnexpectedResponseExceptionDetail(statusCode = { 404 }, exceptionBodyClass = NotFoundError.class) - @UnexpectedResponseExceptionDetail - Response errorResponseStatusCode404(@HostParam("endpoint") String endpoint, - RequestContext requestContext); + @Override + public Response errorResponseStatusCode404(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/response/status-code-range/error-response-status-code-404"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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(400, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(401, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(402, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(403, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(405, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(406, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(407, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(408, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(409, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(410, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(411, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(412, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(413, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(414, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(415, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(416, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(417, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(418, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(419, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(420, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(421, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(422, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(423, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(424, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(425, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(426, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(427, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(428, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(429, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(430, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(431, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(432, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(433, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(434, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(435, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(436, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(437, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(438, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(439, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(440, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(441, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(442, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(443, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(444, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(445, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(446, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(447, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(448, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(449, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(450, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(451, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(452, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(453, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(454, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(455, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(456, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(457, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(458, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(459, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(460, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(461, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(462, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(463, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(464, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(465, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(466, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(467, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(468, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(469, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(470, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(471, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(472, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(473, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(474, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(475, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(476, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(477, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(478, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(479, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(480, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(481, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(482, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(483, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(484, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(485, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(486, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(487, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(488, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(489, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(490, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(491, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(492, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(493, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(494, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(495, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(496, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(497, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(498, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(499, + CoreUtils.createParameterizedType(response.statuscoderange.Standard4XXError.class)), + Map.entry(404, + CoreUtils.createParameterizedType(response.statuscoderange.NotFoundError.class))), + HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), null); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/InInterfacesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/InInterfacesImpl.java index f5b166226b5..751a88b0bab 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/InInterfacesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/InInterfacesImpl.java @@ -1,18 +1,19 @@ package routes.implementation; 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.GeneratedCodeUtils; /** * An instance of this class provides access to all the operations defined in InInterfaces. @@ -45,29 +46,45 @@ public final class InInterfacesImpl { } /** - * The interface defining all the services for RoutesClientInInterfaces to be used by the proxy service to perform - * REST calls. + * The HTTP operations for RoutesClientInInterfaces. */ - @ServiceInterface(name = "RoutesClientInInterfaces", host = "{endpoint}") public interface InInterfacesService { static InInterfacesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("routes.implementation.InInterfacesServiceImpl"); - return (InInterfacesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new InInterfacesServiceImpl(pipeline); + } + + Response fixed(String endpoint, RequestContext requestContext); + } + + private static final class InInterfacesServiceImpl implements InInterfacesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(InInterfacesService.class); + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private InInterfacesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/in-interface/fixed", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response fixed(@HostParam("endpoint") String endpoint, RequestContext requestContext); + @Override + public Response fixed(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/routes/in-interface/fixed"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/PathParametersImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/PathParametersImpl.java index c86069d572f..e55fbd468f8 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/PathParametersImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/PathParametersImpl.java @@ -1,19 +1,20 @@ package routes.implementation; 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.PathParam; -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.implementation.utils.UriEscapers; 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.GeneratedCodeUtils; /** * An instance of this class provides access to all the operations defined in PathParameters. @@ -46,46 +47,83 @@ public final class PathParametersImpl { } /** - * The interface defining all the services for RoutesClientPathParameters to be used by the proxy service to perform - * REST calls. + * The HTTP operations for RoutesClientPathParameters. */ - @ServiceInterface(name = "RoutesClientPathParameters", host = "{endpoint}") public interface PathParametersService { static PathParametersService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("routes.implementation.PathParametersServiceImpl"); - return (PathParametersService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new PathParametersServiceImpl(pipeline); + } + + Response templateOnly(String endpoint, String param, RequestContext requestContext); + + Response explicit(String endpoint, String param, RequestContext requestContext); + + Response annotationOnly(String endpoint, String param, RequestContext requestContext); + } + + private static final class PathParametersServiceImpl implements PathParametersService { + private static final ClientLogger HTTP_LOG = new ClientLogger(PathParametersService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private PathParametersServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response templateOnly(String endpoint, String param, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/routes/path/template-only/" + UriEscapers.PATH_ESCAPER.escape(param)); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 explicit(String endpoint, String param, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/routes/path/explicit/" + UriEscapers.PATH_ESCAPER.escape(param)); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/path/template-only/{param}", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response templateOnly(@HostParam("endpoint") String endpoint, @PathParam("param") String param, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/path/explicit/{param}", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response explicit(@HostParam("endpoint") String endpoint, @PathParam("param") String param, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/path/annotation-only/{param}", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response annotationOnly(@HostParam("endpoint") String endpoint, @PathParam("param") String param, - RequestContext requestContext); + @Override + public Response annotationOnly(String endpoint, String param, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/routes/path/annotation-only/" + UriEscapers.PATH_ESCAPER.escape(param)); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/PathParametersLabelExpansionExplodesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/PathParametersLabelExpansionExplodesImpl.java index 9db13be7163..b831420b1e4 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/PathParametersLabelExpansionExplodesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/PathParametersLabelExpansionExplodesImpl.java @@ -1,19 +1,20 @@ package routes.implementation; 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.PathParam; -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.implementation.utils.UriEscapers; 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.GeneratedCodeUtils; import java.util.List; import java.util.Map; import java.util.Objects; @@ -50,47 +51,85 @@ public final class PathParametersLabelExpansionExplodesImpl { } /** - * The interface defining all the services for RoutesClientPathParametersLabelExpansionExplodes to be used by the - * proxy service to perform REST calls. + * The HTTP operations for RoutesClientPathParametersLabelExpansionExplodes. */ - @ServiceInterface(name = "RoutesClientPathParametersLabelExpansionExplodes", host = "{endpoint}") public interface PathParametersLabelExpansionExplodesService { static PathParametersLabelExpansionExplodesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("routes.implementation.PathParametersLabelExpansionExplodesServiceImpl"); - return (PathParametersLabelExpansionExplodesService) clazz - .getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new PathParametersLabelExpansionExplodesServiceImpl(pipeline); + } + + Response primitive(String endpoint, String param, RequestContext requestContext); + + Response array(String endpoint, String param, RequestContext requestContext); + + Response record(String endpoint, Map param, RequestContext requestContext); + } + + private static final class PathParametersLabelExpansionExplodesServiceImpl + implements PathParametersLabelExpansionExplodesService { + private static final ClientLogger HTTP_LOG + = new ClientLogger(PathParametersLabelExpansionExplodesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private PathParametersLabelExpansionExplodesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response primitive(String endpoint, String param, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/routes/path/label/explode/primitive" + UriEscapers.PATH_ESCAPER.escape(param)); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 array(String endpoint, String param, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/routes/path/label/explode/array" + UriEscapers.PATH_ESCAPER.escape(param)); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/path/label/explode/primitive{param}", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response primitive(@HostParam("endpoint") String endpoint, @PathParam("param") String param, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/path/label/explode/array{param}", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response array(@HostParam("endpoint") String endpoint, @PathParam("param") String param, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/path/label/explode/record{param}", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response record(@HostParam("endpoint") String endpoint, @PathParam("param") Map param, - RequestContext requestContext); + @Override + public Response record(String endpoint, Map param, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/routes/path/label/explode/record" + String.valueOf(param)); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/PathParametersLabelExpansionStandardsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/PathParametersLabelExpansionStandardsImpl.java index 8c58fd1d41b..6c1e737257a 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/PathParametersLabelExpansionStandardsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/PathParametersLabelExpansionStandardsImpl.java @@ -1,19 +1,20 @@ package routes.implementation; 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.PathParam; -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.implementation.utils.UriEscapers; 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.GeneratedCodeUtils; import java.util.List; import java.util.Map; import java.util.Objects; @@ -50,48 +51,85 @@ public final class PathParametersLabelExpansionStandardsImpl { } /** - * The interface defining all the services for RoutesClientPathParametersLabelExpansionStandards to be used by the - * proxy service to perform REST calls. + * The HTTP operations for RoutesClientPathParametersLabelExpansionStandards. */ - @ServiceInterface(name = "RoutesClientPathParametersLabelExpansionStandards", host = "{endpoint}") public interface PathParametersLabelExpansionStandardsService { static PathParametersLabelExpansionStandardsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz - = Class.forName("routes.implementation.PathParametersLabelExpansionStandardsServiceImpl"); - return (PathParametersLabelExpansionStandardsService) clazz - .getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new PathParametersLabelExpansionStandardsServiceImpl(pipeline); + } + + Response primitive(String endpoint, String param, RequestContext requestContext); + + Response array(String endpoint, String param, RequestContext requestContext); + + Response record(String endpoint, Map param, RequestContext requestContext); + } + + private static final class PathParametersLabelExpansionStandardsServiceImpl + implements PathParametersLabelExpansionStandardsService { + private static final ClientLogger HTTP_LOG + = new ClientLogger(PathParametersLabelExpansionStandardsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private PathParametersLabelExpansionStandardsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response primitive(String endpoint, String param, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/routes/path/label/standard/primitive" + UriEscapers.PATH_ESCAPER.escape(param)); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 array(String endpoint, String param, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/routes/path/label/standard/array" + UriEscapers.PATH_ESCAPER.escape(param)); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/path/label/standard/primitive{param}", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response primitive(@HostParam("endpoint") String endpoint, @PathParam("param") String param, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/path/label/standard/array{param}", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response array(@HostParam("endpoint") String endpoint, @PathParam("param") String param, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/path/label/standard/record{param}", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response record(@HostParam("endpoint") String endpoint, @PathParam("param") Map param, - RequestContext requestContext); + @Override + public Response record(String endpoint, Map param, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/routes/path/label/standard/record" + String.valueOf(param)); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/PathParametersMatrixExpansionExplodesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/PathParametersMatrixExpansionExplodesImpl.java index 99f1f6e71ac..f619ae5d457 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/PathParametersMatrixExpansionExplodesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/PathParametersMatrixExpansionExplodesImpl.java @@ -1,19 +1,20 @@ package routes.implementation; 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.PathParam; -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.implementation.utils.UriEscapers; 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.GeneratedCodeUtils; import java.util.List; import java.util.Map; import java.util.Objects; @@ -50,48 +51,85 @@ public final class PathParametersMatrixExpansionExplodesImpl { } /** - * The interface defining all the services for RoutesClientPathParametersMatrixExpansionExplodes to be used by the - * proxy service to perform REST calls. + * The HTTP operations for RoutesClientPathParametersMatrixExpansionExplodes. */ - @ServiceInterface(name = "RoutesClientPathParametersMatrixExpansionExplodes", host = "{endpoint}") public interface PathParametersMatrixExpansionExplodesService { static PathParametersMatrixExpansionExplodesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz - = Class.forName("routes.implementation.PathParametersMatrixExpansionExplodesServiceImpl"); - return (PathParametersMatrixExpansionExplodesService) clazz - .getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new PathParametersMatrixExpansionExplodesServiceImpl(pipeline); + } + + Response primitive(String endpoint, String param, RequestContext requestContext); + + Response array(String endpoint, String param, RequestContext requestContext); + + Response record(String endpoint, Map param, RequestContext requestContext); + } + + private static final class PathParametersMatrixExpansionExplodesServiceImpl + implements PathParametersMatrixExpansionExplodesService { + private static final ClientLogger HTTP_LOG + = new ClientLogger(PathParametersMatrixExpansionExplodesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private PathParametersMatrixExpansionExplodesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response primitive(String endpoint, String param, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/routes/path/matrix/explode/primitive" + UriEscapers.PATH_ESCAPER.escape(param)); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 array(String endpoint, String param, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/routes/path/matrix/explode/array" + UriEscapers.PATH_ESCAPER.escape(param)); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/path/matrix/explode/primitive{param}", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response primitive(@HostParam("endpoint") String endpoint, @PathParam("param") String param, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/path/matrix/explode/array{param}", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response array(@HostParam("endpoint") String endpoint, @PathParam("param") String param, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/path/matrix/explode/record{param}", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response record(@HostParam("endpoint") String endpoint, @PathParam("param") Map param, - RequestContext requestContext); + @Override + public Response record(String endpoint, Map param, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/routes/path/matrix/explode/record" + String.valueOf(param)); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/PathParametersMatrixExpansionStandardsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/PathParametersMatrixExpansionStandardsImpl.java index d9988083b70..409749fed18 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/PathParametersMatrixExpansionStandardsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/PathParametersMatrixExpansionStandardsImpl.java @@ -1,19 +1,20 @@ package routes.implementation; 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.PathParam; -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.implementation.utils.UriEscapers; 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.GeneratedCodeUtils; import java.util.List; import java.util.Map; import java.util.Objects; @@ -50,48 +51,85 @@ public final class PathParametersMatrixExpansionStandardsImpl { } /** - * The interface defining all the services for RoutesClientPathParametersMatrixExpansionStandards to be used by the - * proxy service to perform REST calls. + * The HTTP operations for RoutesClientPathParametersMatrixExpansionStandards. */ - @ServiceInterface(name = "RoutesClientPathParametersMatrixExpansionStandards", host = "{endpoint}") public interface PathParametersMatrixExpansionStandardsService { static PathParametersMatrixExpansionStandardsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz - = Class.forName("routes.implementation.PathParametersMatrixExpansionStandardsServiceImpl"); - return (PathParametersMatrixExpansionStandardsService) clazz - .getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new PathParametersMatrixExpansionStandardsServiceImpl(pipeline); + } + + Response primitive(String endpoint, String param, RequestContext requestContext); + + Response array(String endpoint, String param, RequestContext requestContext); + + Response record(String endpoint, Map param, RequestContext requestContext); + } + + private static final class PathParametersMatrixExpansionStandardsServiceImpl + implements PathParametersMatrixExpansionStandardsService { + private static final ClientLogger HTTP_LOG + = new ClientLogger(PathParametersMatrixExpansionStandardsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private PathParametersMatrixExpansionStandardsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response primitive(String endpoint, String param, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/routes/path/matrix/standard/primitive" + UriEscapers.PATH_ESCAPER.escape(param)); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 array(String endpoint, String param, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/routes/path/matrix/standard/array" + UriEscapers.PATH_ESCAPER.escape(param)); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/path/matrix/standard/primitive{param}", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response primitive(@HostParam("endpoint") String endpoint, @PathParam("param") String param, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/path/matrix/standard/array{param}", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response array(@HostParam("endpoint") String endpoint, @PathParam("param") String param, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/path/matrix/standard/record{param}", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response record(@HostParam("endpoint") String endpoint, @PathParam("param") Map param, - RequestContext requestContext); + @Override + public Response record(String endpoint, Map param, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/routes/path/matrix/standard/record" + String.valueOf(param)); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/PathParametersPathExpansionExplodesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/PathParametersPathExpansionExplodesImpl.java index 7e1a3f5ddeb..93798a2d564 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/PathParametersPathExpansionExplodesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/PathParametersPathExpansionExplodesImpl.java @@ -1,19 +1,20 @@ package routes.implementation; 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.PathParam; -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.implementation.utils.UriEscapers; 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.GeneratedCodeUtils; import java.util.List; import java.util.Map; import java.util.Objects; @@ -50,47 +51,84 @@ public final class PathParametersPathExpansionExplodesImpl { } /** - * The interface defining all the services for RoutesClientPathParametersPathExpansionExplodes to be used by the - * proxy service to perform REST calls. + * The HTTP operations for RoutesClientPathParametersPathExpansionExplodes. */ - @ServiceInterface(name = "RoutesClientPathParametersPathExpansionExplodes", host = "{endpoint}") public interface PathParametersPathExpansionExplodesService { static PathParametersPathExpansionExplodesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("routes.implementation.PathParametersPathExpansionExplodesServiceImpl"); - return (PathParametersPathExpansionExplodesService) clazz - .getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new PathParametersPathExpansionExplodesServiceImpl(pipeline); + } + + Response primitive(String endpoint, String param, RequestContext requestContext); + + Response array(String endpoint, String param, RequestContext requestContext); + + Response record(String endpoint, Map param, RequestContext requestContext); + } + + private static final class PathParametersPathExpansionExplodesServiceImpl + implements PathParametersPathExpansionExplodesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(PathParametersPathExpansionExplodesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private PathParametersPathExpansionExplodesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response primitive(String endpoint, String param, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/routes/path/path/explode/primitive" + UriEscapers.PATH_ESCAPER.escape(param)); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 array(String endpoint, String param, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/routes/path/path/explode/array" + UriEscapers.PATH_ESCAPER.escape(param)); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/path/path/explode/primitive{param}", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response primitive(@HostParam("endpoint") String endpoint, @PathParam("param") String param, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/path/path/explode/array{param}", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response array(@HostParam("endpoint") String endpoint, @PathParam("param") String param, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/path/path/explode/record{param}", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response record(@HostParam("endpoint") String endpoint, @PathParam("param") Map param, - RequestContext requestContext); + @Override + public Response record(String endpoint, Map param, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/routes/path/path/explode/record" + String.valueOf(param)); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/PathParametersPathExpansionStandardsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/PathParametersPathExpansionStandardsImpl.java index 538b94758eb..0201459e306 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/PathParametersPathExpansionStandardsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/PathParametersPathExpansionStandardsImpl.java @@ -1,19 +1,20 @@ package routes.implementation; 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.PathParam; -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.implementation.utils.UriEscapers; 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.GeneratedCodeUtils; import java.util.List; import java.util.Map; import java.util.Objects; @@ -50,47 +51,85 @@ public final class PathParametersPathExpansionStandardsImpl { } /** - * The interface defining all the services for RoutesClientPathParametersPathExpansionStandards to be used by the - * proxy service to perform REST calls. + * The HTTP operations for RoutesClientPathParametersPathExpansionStandards. */ - @ServiceInterface(name = "RoutesClientPathParametersPathExpansionStandards", host = "{endpoint}") public interface PathParametersPathExpansionStandardsService { static PathParametersPathExpansionStandardsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("routes.implementation.PathParametersPathExpansionStandardsServiceImpl"); - return (PathParametersPathExpansionStandardsService) clazz - .getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new PathParametersPathExpansionStandardsServiceImpl(pipeline); + } + + Response primitive(String endpoint, String param, RequestContext requestContext); + + Response array(String endpoint, String param, RequestContext requestContext); + + Response record(String endpoint, Map param, RequestContext requestContext); + } + + private static final class PathParametersPathExpansionStandardsServiceImpl + implements PathParametersPathExpansionStandardsService { + private static final ClientLogger HTTP_LOG + = new ClientLogger(PathParametersPathExpansionStandardsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private PathParametersPathExpansionStandardsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response primitive(String endpoint, String param, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/routes/path/path/standard/primitive" + UriEscapers.PATH_ESCAPER.escape(param)); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 array(String endpoint, String param, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/routes/path/path/standard/array" + UriEscapers.PATH_ESCAPER.escape(param)); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/path/path/standard/primitive{param}", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response primitive(@HostParam("endpoint") String endpoint, @PathParam("param") String param, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/path/path/standard/array{param}", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response array(@HostParam("endpoint") String endpoint, @PathParam("param") String param, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/path/path/standard/record{param}", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response record(@HostParam("endpoint") String endpoint, @PathParam("param") Map param, - RequestContext requestContext); + @Override + public Response record(String endpoint, Map param, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/routes/path/path/standard/record" + String.valueOf(param)); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/PathParametersReservedExpansionsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/PathParametersReservedExpansionsImpl.java index d662e65ca5c..558ef39c9a5 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/PathParametersReservedExpansionsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/PathParametersReservedExpansionsImpl.java @@ -1,19 +1,19 @@ package routes.implementation; 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.PathParam; -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.GeneratedCodeUtils; /** * An instance of this class provides access to all the operations defined in PathParametersReservedExpansions. @@ -46,38 +46,65 @@ public final class PathParametersReservedExpansionsImpl { } /** - * The interface defining all the services for RoutesClientPathParametersReservedExpansions to be used by the proxy - * service to perform REST calls. + * The HTTP operations for RoutesClientPathParametersReservedExpansions. */ - @ServiceInterface(name = "RoutesClientPathParametersReservedExpansions", host = "{endpoint}") public interface PathParametersReservedExpansionsService { static PathParametersReservedExpansionsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("routes.implementation.PathParametersReservedExpansionsServiceImpl"); - return (PathParametersReservedExpansionsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new PathParametersReservedExpansionsServiceImpl(pipeline); + } + + Response template(String endpoint, String param, RequestContext requestContext); + + Response annotation(String endpoint, String param, RequestContext requestContext); + } + + private static final class PathParametersReservedExpansionsServiceImpl + implements PathParametersReservedExpansionsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(PathParametersReservedExpansionsService.class); + + private final HttpPipeline httpPipeline; + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private PathParametersReservedExpansionsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/path/reserved-expansion/template/{param}", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response template(@HostParam("endpoint") String endpoint, - @PathParam(value = "param", encoded = true) String param, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/path/reserved-expansion/annotation/{param}", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response annotation(@HostParam("endpoint") String endpoint, - @PathParam(value = "param", encoded = true) String param, RequestContext requestContext); + @Override + public Response template(String endpoint, String param, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/routes/path/reserved-expansion/template/" + param); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 annotation(String endpoint, String param, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/routes/path/reserved-expansion/annotation/" + param); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/PathParametersSimpleExpansionExplodesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/PathParametersSimpleExpansionExplodesImpl.java index 28012b9e663..c194a6f7986 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/PathParametersSimpleExpansionExplodesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/PathParametersSimpleExpansionExplodesImpl.java @@ -1,19 +1,20 @@ package routes.implementation; 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.PathParam; -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.implementation.utils.UriEscapers; 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.GeneratedCodeUtils; import java.util.List; import java.util.Map; import java.util.Objects; @@ -50,48 +51,85 @@ public final class PathParametersSimpleExpansionExplodesImpl { } /** - * The interface defining all the services for RoutesClientPathParametersSimpleExpansionExplodes to be used by the - * proxy service to perform REST calls. + * The HTTP operations for RoutesClientPathParametersSimpleExpansionExplodes. */ - @ServiceInterface(name = "RoutesClientPathParametersSimpleExpansionExplodes", host = "{endpoint}") public interface PathParametersSimpleExpansionExplodesService { static PathParametersSimpleExpansionExplodesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz - = Class.forName("routes.implementation.PathParametersSimpleExpansionExplodesServiceImpl"); - return (PathParametersSimpleExpansionExplodesService) clazz - .getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new PathParametersSimpleExpansionExplodesServiceImpl(pipeline); + } + + Response primitive(String endpoint, String param, RequestContext requestContext); + + Response array(String endpoint, String param, RequestContext requestContext); + + Response record(String endpoint, Map param, RequestContext requestContext); + } + + private static final class PathParametersSimpleExpansionExplodesServiceImpl + implements PathParametersSimpleExpansionExplodesService { + private static final ClientLogger HTTP_LOG + = new ClientLogger(PathParametersSimpleExpansionExplodesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private PathParametersSimpleExpansionExplodesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response primitive(String endpoint, String param, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/routes/path/simple/explode/primitive" + UriEscapers.PATH_ESCAPER.escape(param)); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 array(String endpoint, String param, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/routes/path/simple/explode/array" + UriEscapers.PATH_ESCAPER.escape(param)); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/path/simple/explode/primitive{param}", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response primitive(@HostParam("endpoint") String endpoint, @PathParam("param") String param, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/path/simple/explode/array{param}", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response array(@HostParam("endpoint") String endpoint, @PathParam("param") String param, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/path/simple/explode/record{param}", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response record(@HostParam("endpoint") String endpoint, @PathParam("param") Map param, - RequestContext requestContext); + @Override + public Response record(String endpoint, Map param, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/routes/path/simple/explode/record" + String.valueOf(param)); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/PathParametersSimpleExpansionStandardsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/PathParametersSimpleExpansionStandardsImpl.java index 6197d12ea63..62af5852ca0 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/PathParametersSimpleExpansionStandardsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/PathParametersSimpleExpansionStandardsImpl.java @@ -1,19 +1,20 @@ package routes.implementation; 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.PathParam; -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.implementation.utils.UriEscapers; 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.GeneratedCodeUtils; import java.util.List; import java.util.Map; import java.util.Objects; @@ -50,48 +51,85 @@ public final class PathParametersSimpleExpansionStandardsImpl { } /** - * The interface defining all the services for RoutesClientPathParametersSimpleExpansionStandards to be used by the - * proxy service to perform REST calls. + * The HTTP operations for RoutesClientPathParametersSimpleExpansionStandards. */ - @ServiceInterface(name = "RoutesClientPathParametersSimpleExpansionStandards", host = "{endpoint}") public interface PathParametersSimpleExpansionStandardsService { static PathParametersSimpleExpansionStandardsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz - = Class.forName("routes.implementation.PathParametersSimpleExpansionStandardsServiceImpl"); - return (PathParametersSimpleExpansionStandardsService) clazz - .getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new PathParametersSimpleExpansionStandardsServiceImpl(pipeline); + } + + Response primitive(String endpoint, String param, RequestContext requestContext); + + Response array(String endpoint, String param, RequestContext requestContext); + + Response record(String endpoint, Map param, RequestContext requestContext); + } + + private static final class PathParametersSimpleExpansionStandardsServiceImpl + implements PathParametersSimpleExpansionStandardsService { + private static final ClientLogger HTTP_LOG + = new ClientLogger(PathParametersSimpleExpansionStandardsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private PathParametersSimpleExpansionStandardsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response primitive(String endpoint, String param, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/routes/path/simple/standard/primitive" + UriEscapers.PATH_ESCAPER.escape(param)); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 array(String endpoint, String param, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/routes/path/simple/standard/array" + UriEscapers.PATH_ESCAPER.escape(param)); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/path/simple/standard/primitive{param}", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response primitive(@HostParam("endpoint") String endpoint, @PathParam("param") String param, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/path/simple/standard/array{param}", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response array(@HostParam("endpoint") String endpoint, @PathParam("param") String param, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/path/simple/standard/record{param}", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response record(@HostParam("endpoint") String endpoint, @PathParam("param") Map param, - RequestContext requestContext); + @Override + public Response record(String endpoint, Map param, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/routes/path/simple/standard/record" + String.valueOf(param)); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/QueryParametersImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/QueryParametersImpl.java index 4104b09e40c..eab29fad413 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/QueryParametersImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/QueryParametersImpl.java @@ -1,19 +1,20 @@ package routes.implementation; 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.QueryParam; -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.GeneratedCodeUtils; +import io.clientcore.core.utils.UriBuilder; /** * An instance of this class provides access to all the operations defined in QueryParameters. @@ -46,43 +47,86 @@ public final class QueryParametersImpl { } /** - * The interface defining all the services for RoutesClientQueryParameters to be used by the proxy service to - * perform REST calls. + * The HTTP operations for RoutesClientQueryParameters. */ - @ServiceInterface(name = "RoutesClientQueryParameters", host = "{endpoint}") public interface QueryParametersService { static QueryParametersService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("routes.implementation.QueryParametersServiceImpl"); - return (QueryParametersService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new QueryParametersServiceImpl(pipeline); + } + + Response templateOnly(String endpoint, String param, RequestContext requestContext); + + Response explicit(String endpoint, String param, RequestContext requestContext); + + Response annotationOnly(String endpoint, String param, RequestContext requestContext); + } + + private static final class QueryParametersServiceImpl implements QueryParametersService { + private static final ClientLogger HTTP_LOG = new ClientLogger(QueryParametersService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private QueryParametersServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response templateOnly(String endpoint, String param, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/routes/query/template-only"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "param", true, param, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 explicit(String endpoint, String param, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/routes/query/explicit"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "param", true, param, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/query/template-only", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response templateOnly(@HostParam("endpoint") String endpoint, @QueryParam("param") String param, - RequestContext requestContext); - - @HttpRequestInformation(method = HttpMethod.GET, path = "/routes/query/explicit", expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response explicit(@HostParam("endpoint") String endpoint, @QueryParam("param") String param, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/query/annotation-only", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response annotationOnly(@HostParam("endpoint") String endpoint, @QueryParam("param") String param, - RequestContext requestContext); + @Override + public Response annotationOnly(String endpoint, String param, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/routes/query/annotation-only"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "param", true, param, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/QueryParametersQueryContinuationExplodesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/QueryParametersQueryContinuationExplodesImpl.java index 33917a733af..09a8f38e22f 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/QueryParametersQueryContinuationExplodesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/QueryParametersQueryContinuationExplodesImpl.java @@ -1,19 +1,20 @@ package routes.implementation; 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.QueryParam; -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.GeneratedCodeUtils; +import io.clientcore.core.utils.UriBuilder; import java.util.List; import java.util.Map; import java.util.Objects; @@ -51,56 +52,112 @@ public final class QueryParametersQueryContinuationExplodesImpl { } /** - * The interface defining all the services for RoutesClientQueryParametersQueryContinuationExplodes to be used by - * the proxy service to perform REST calls. + * The HTTP operations for RoutesClientQueryParametersQueryContinuationExplodes. */ - @ServiceInterface(name = "RoutesClientQueryParametersQueryContinuationExplodes", host = "{endpoint}") public interface QueryParametersQueryContinuationExplodesService { static QueryParametersQueryContinuationExplodesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz - = Class.forName("routes.implementation.QueryParametersQueryContinuationExplodesServiceImpl"); - return (QueryParametersQueryContinuationExplodesService) clazz - .getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new QueryParametersQueryContinuationExplodesServiceImpl(pipeline); + } + + Response primitive(String endpoint, String param, RequestContext requestContext); + + Response array(String endpoint, List param, RequestContext requestContext); + + Response record(String endpoint, Map param, RequestContext requestContext); + + Response model(String endpoint, ExpandParameters param, RequestContext requestContext); + } + + private static final class QueryParametersQueryContinuationExplodesServiceImpl + implements QueryParametersQueryContinuationExplodesService { + private static final ClientLogger HTTP_LOG + = new ClientLogger(QueryParametersQueryContinuationExplodesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private QueryParametersQueryContinuationExplodesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response primitive(String endpoint, String param, RequestContext requestContext) { + UriBuilder uriBuilder + = UriBuilder.parse(endpoint + "/routes/query/query-continuation/explode/primitive?fixed=true"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "param", true, param, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 array(String endpoint, List param, RequestContext requestContext) { + UriBuilder uriBuilder + = UriBuilder.parse(endpoint + "/routes/query/query-continuation/explode/array?fixed=true"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "param", true, param, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 record(String endpoint, Map param, RequestContext requestContext) { + UriBuilder uriBuilder + = UriBuilder.parse(endpoint + "/routes/query/query-continuation/explode/record?fixed=true"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "param", true, param, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/query/query-continuation/explode/primitive?fixed=true", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response primitive(@HostParam("endpoint") String endpoint, @QueryParam("param") String param, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/query/query-continuation/explode/array?fixed=true", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response array(@HostParam("endpoint") String endpoint, - @QueryParam(value = "param", multipleQueryParams = true) List param, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/query/query-continuation/explode/record?fixed=true", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response record(@HostParam("endpoint") String endpoint, @QueryParam("param") Map param, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/query/query-continuation/explode/model?fixed=true", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response model(@HostParam("endpoint") String endpoint, @QueryParam("param") ExpandParameters param, - RequestContext requestContext); + @Override + public Response model(String endpoint, ExpandParameters param, RequestContext requestContext) { + UriBuilder uriBuilder + = UriBuilder.parse(endpoint + "/routes/query/query-continuation/explode/model?fixed=true"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "param", true, param, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/QueryParametersQueryContinuationStandardsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/QueryParametersQueryContinuationStandardsImpl.java index ad55e0e2427..59339e71c09 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/QueryParametersQueryContinuationStandardsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/QueryParametersQueryContinuationStandardsImpl.java @@ -1,19 +1,20 @@ package routes.implementation; 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.QueryParam; -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.GeneratedCodeUtils; +import io.clientcore.core.utils.UriBuilder; import java.util.List; import java.util.Map; import java.util.Objects; @@ -51,56 +52,112 @@ public final class QueryParametersQueryContinuationStandardsImpl { } /** - * The interface defining all the services for RoutesClientQueryParametersQueryContinuationStandards to be used by - * the proxy service to perform REST calls. + * The HTTP operations for RoutesClientQueryParametersQueryContinuationStandards. */ - @ServiceInterface(name = "RoutesClientQueryParametersQueryContinuationStandards", host = "{endpoint}") public interface QueryParametersQueryContinuationStandardsService { static QueryParametersQueryContinuationStandardsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz - = Class.forName("routes.implementation.QueryParametersQueryContinuationStandardsServiceImpl"); - return (QueryParametersQueryContinuationStandardsService) clazz - .getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new QueryParametersQueryContinuationStandardsServiceImpl(pipeline); + } + + Response primitive(String endpoint, String param, RequestContext requestContext); + + Response array(String endpoint, String param, RequestContext requestContext); + + Response record(String endpoint, Map param, RequestContext requestContext); + + Response model(String endpoint, ExpandParameters param, RequestContext requestContext); + } + + private static final class QueryParametersQueryContinuationStandardsServiceImpl + implements QueryParametersQueryContinuationStandardsService { + private static final ClientLogger HTTP_LOG + = new ClientLogger(QueryParametersQueryContinuationStandardsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private QueryParametersQueryContinuationStandardsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response primitive(String endpoint, String param, RequestContext requestContext) { + UriBuilder uriBuilder + = UriBuilder.parse(endpoint + "/routes/query/query-continuation/standard/primitive?fixed=true"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "param", true, param, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 array(String endpoint, String param, RequestContext requestContext) { + UriBuilder uriBuilder + = UriBuilder.parse(endpoint + "/routes/query/query-continuation/standard/array?fixed=true"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "param", true, param, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 record(String endpoint, Map param, RequestContext requestContext) { + UriBuilder uriBuilder + = UriBuilder.parse(endpoint + "/routes/query/query-continuation/standard/record?fixed=true"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "param", true, param, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/query/query-continuation/standard/primitive?fixed=true", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response primitive(@HostParam("endpoint") String endpoint, @QueryParam("param") String param, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/query/query-continuation/standard/array?fixed=true", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response array(@HostParam("endpoint") String endpoint, @QueryParam("param") String param, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/query/query-continuation/standard/record?fixed=true", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response record(@HostParam("endpoint") String endpoint, @QueryParam("param") Map param, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/query/query-continuation/standard/model?fixed=true", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response model(@HostParam("endpoint") String endpoint, @QueryParam("param") ExpandParameters param, - RequestContext requestContext); + @Override + public Response model(String endpoint, ExpandParameters param, RequestContext requestContext) { + UriBuilder uriBuilder + = UriBuilder.parse(endpoint + "/routes/query/query-continuation/standard/model?fixed=true"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "param", true, param, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/QueryParametersQueryExpansionExplodesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/QueryParametersQueryExpansionExplodesImpl.java index f792a4e3a14..ae7b6b7470e 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/QueryParametersQueryExpansionExplodesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/QueryParametersQueryExpansionExplodesImpl.java @@ -1,19 +1,20 @@ package routes.implementation; 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.QueryParam; -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.GeneratedCodeUtils; +import io.clientcore.core.utils.UriBuilder; import java.util.List; import java.util.Map; import java.util.Objects; @@ -51,56 +52,108 @@ public final class QueryParametersQueryExpansionExplodesImpl { } /** - * The interface defining all the services for RoutesClientQueryParametersQueryExpansionExplodes to be used by the - * proxy service to perform REST calls. + * The HTTP operations for RoutesClientQueryParametersQueryExpansionExplodes. */ - @ServiceInterface(name = "RoutesClientQueryParametersQueryExpansionExplodes", host = "{endpoint}") public interface QueryParametersQueryExpansionExplodesService { static QueryParametersQueryExpansionExplodesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz - = Class.forName("routes.implementation.QueryParametersQueryExpansionExplodesServiceImpl"); - return (QueryParametersQueryExpansionExplodesService) clazz - .getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new QueryParametersQueryExpansionExplodesServiceImpl(pipeline); + } + + Response primitive(String endpoint, String param, RequestContext requestContext); + + Response array(String endpoint, List param, RequestContext requestContext); + + Response record(String endpoint, Map param, RequestContext requestContext); + + Response model(String endpoint, ExpandParameters param, RequestContext requestContext); + } + + private static final class QueryParametersQueryExpansionExplodesServiceImpl + implements QueryParametersQueryExpansionExplodesService { + private static final ClientLogger HTTP_LOG + = new ClientLogger(QueryParametersQueryExpansionExplodesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private QueryParametersQueryExpansionExplodesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response primitive(String endpoint, String param, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/routes/query/query-expansion/explode/primitive"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "param", true, param, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 array(String endpoint, List param, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/routes/query/query-expansion/explode/array"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "param", true, param, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 record(String endpoint, Map param, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/routes/query/query-expansion/explode/record"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "param", true, param, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/query/query-expansion/explode/primitive", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response primitive(@HostParam("endpoint") String endpoint, @QueryParam("param") String param, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/query/query-expansion/explode/array", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response array(@HostParam("endpoint") String endpoint, - @QueryParam(value = "param", multipleQueryParams = true) List param, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/query/query-expansion/explode/record", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response record(@HostParam("endpoint") String endpoint, @QueryParam("param") Map param, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/query/query-expansion/explode/model", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response model(@HostParam("endpoint") String endpoint, @QueryParam("param") ExpandParameters param, - RequestContext requestContext); + @Override + public Response model(String endpoint, ExpandParameters param, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/routes/query/query-expansion/explode/model"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "param", true, param, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/QueryParametersQueryExpansionStandardsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/QueryParametersQueryExpansionStandardsImpl.java index f3bc72bf45a..21bb8a4e3e4 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/QueryParametersQueryExpansionStandardsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/QueryParametersQueryExpansionStandardsImpl.java @@ -1,19 +1,20 @@ package routes.implementation; 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.QueryParam; -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.GeneratedCodeUtils; +import io.clientcore.core.utils.UriBuilder; import java.util.List; import java.util.Map; import java.util.Objects; @@ -51,56 +52,108 @@ public final class QueryParametersQueryExpansionStandardsImpl { } /** - * The interface defining all the services for RoutesClientQueryParametersQueryExpansionStandards to be used by the - * proxy service to perform REST calls. + * The HTTP operations for RoutesClientQueryParametersQueryExpansionStandards. */ - @ServiceInterface(name = "RoutesClientQueryParametersQueryExpansionStandards", host = "{endpoint}") public interface QueryParametersQueryExpansionStandardsService { static QueryParametersQueryExpansionStandardsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz - = Class.forName("routes.implementation.QueryParametersQueryExpansionStandardsServiceImpl"); - return (QueryParametersQueryExpansionStandardsService) clazz - .getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new QueryParametersQueryExpansionStandardsServiceImpl(pipeline); + } + + Response primitive(String endpoint, String param, RequestContext requestContext); + + Response array(String endpoint, String param, RequestContext requestContext); + + Response record(String endpoint, Map param, RequestContext requestContext); + + Response model(String endpoint, ExpandParameters param, RequestContext requestContext); + } + + private static final class QueryParametersQueryExpansionStandardsServiceImpl + implements QueryParametersQueryExpansionStandardsService { + private static final ClientLogger HTTP_LOG + = new ClientLogger(QueryParametersQueryExpansionStandardsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private QueryParametersQueryExpansionStandardsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response primitive(String endpoint, String param, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/routes/query/query-expansion/standard/primitive"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "param", true, param, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 array(String endpoint, String param, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/routes/query/query-expansion/standard/array"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "param", true, param, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 record(String endpoint, Map param, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/routes/query/query-expansion/standard/record"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "param", true, param, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/query/query-expansion/standard/primitive", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response primitive(@HostParam("endpoint") String endpoint, @QueryParam("param") String param, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/query/query-expansion/standard/array", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response array(@HostParam("endpoint") String endpoint, @QueryParam("param") String param, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/query/query-expansion/standard/record", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response record(@HostParam("endpoint") String endpoint, @QueryParam("param") Map param, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/routes/query/query-expansion/standard/model", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response model(@HostParam("endpoint") String endpoint, @QueryParam("param") ExpandParameters param, - RequestContext requestContext); + @Override + public Response model(String endpoint, ExpandParameters param, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/routes/query/query-expansion/standard/model"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "param", true, param, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/RoutesClientImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/RoutesClientImpl.java index 0be2e7614f4..94d9e380b8f 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/RoutesClientImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/routes/implementation/RoutesClientImpl.java @@ -1,18 +1,19 @@ package routes.implementation; 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.GeneratedCodeUtils; /** * Initializes a new instance of the RoutesClient type. @@ -320,25 +321,44 @@ public RoutesClientImpl(HttpPipeline httpPipeline, Instrumentation instrumentati } /** - * The interface defining all the services for RoutesClient to be used by the proxy service to perform REST calls. + * The HTTP operations for RoutesClient. */ - @ServiceInterface(name = "RoutesClient", host = "{endpoint}") public interface RoutesClientService { static RoutesClientService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("routes.implementation.RoutesClientServiceImpl"); - return (RoutesClientService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new RoutesClientServiceImpl(pipeline); + } + + Response fixed(String endpoint, RequestContext requestContext); + } + + private static final class RoutesClientServiceImpl implements RoutesClientService { + private static final ClientLogger HTTP_LOG = new ClientLogger(RoutesClientService.class); + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private RoutesClientServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation(method = HttpMethod.GET, path = "/routes/fixed", expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response fixed(@HostParam("endpoint") String endpoint, RequestContext requestContext); + @Override + public Response fixed(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/routes/fixed"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/serialization/encodedname/json/implementation/PropertiesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/serialization/encodedname/json/implementation/PropertiesImpl.java index 18c989d346d..8e662313b8b 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/serialization/encodedname/json/implementation/PropertiesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/serialization/encodedname/json/implementation/PropertiesImpl.java @@ -1,20 +1,23 @@ package serialization.encodedname.json.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import serialization.encodedname.json.property.JsonEncodedNameModel; /** @@ -48,37 +51,90 @@ public final class PropertiesImpl { } /** - * The interface defining all the services for JsonClientProperties to be used by the proxy service to perform REST - * calls. + * The HTTP operations for JsonClientProperties. */ - @ServiceInterface(name = "JsonClientProperties", host = "{endpoint}") public interface PropertiesService { static PropertiesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("serialization.encodedname.json.implementation.PropertiesServiceImpl"); - return (PropertiesService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new PropertiesServiceImpl(pipeline); + } + + Response send(String endpoint, String contentType, JsonEncodedNameModel body, + RequestContext requestContext); + + Response get(String endpoint, String accept, RequestContext requestContext); + } + + private static final class PropertiesServiceImpl implements PropertiesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(PropertiesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private PropertiesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response send(String endpoint, String contentType, JsonEncodedNameModel body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/serialization/encoded-name/json/property"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/serialization/encoded-name/json/property"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, JsonEncodedNameModel.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); } - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/serialization/encoded-name/json/property", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response send(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") JsonEncodedNameModel body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/serialization/encoded-name/json/property", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/server/endpoint/notdefined/implementation/NotDefinedClientImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/server/endpoint/notdefined/implementation/NotDefinedClientImpl.java index ad469a0744c..999a51c78d2 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/server/endpoint/notdefined/implementation/NotDefinedClientImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/server/endpoint/notdefined/implementation/NotDefinedClientImpl.java @@ -1,18 +1,19 @@ package server.endpoint.notdefined.implementation; 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.GeneratedCodeUtils; /** * Initializes a new instance of the NotDefinedClient type. @@ -80,29 +81,45 @@ public NotDefinedClientImpl(HttpPipeline httpPipeline, Instrumentation instrumen } /** - * The interface defining all the services for NotDefinedClient to be used by the proxy service to perform REST - * calls. + * The HTTP operations for NotDefinedClient. */ - @ServiceInterface(name = "NotDefinedClient", host = "{endpoint}") public interface NotDefinedClientService { static NotDefinedClientService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("server.endpoint.notdefined.implementation.NotDefinedClientServiceImpl"); - return (NotDefinedClientService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new NotDefinedClientServiceImpl(pipeline); + } + + Response valid(String endpoint, RequestContext requestContext); + } + + private static final class NotDefinedClientServiceImpl implements NotDefinedClientService { + private static final ClientLogger HTTP_LOG = new ClientLogger(NotDefinedClientService.class); + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private NotDefinedClientServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation( - method = HttpMethod.HEAD, - path = "/server/endpoint/not-defined/valid", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response valid(@HostParam("endpoint") String endpoint, RequestContext requestContext); + @Override + public Response valid(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.HEAD).setUri(endpoint + "/server/endpoint/not-defined/valid"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response networkResponse = this.httpPipeline.send(httpRequest)) { + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), null); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/server/path/multiple/implementation/MultipleClientImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/server/path/multiple/implementation/MultipleClientImpl.java index d34ce0c7820..462136ce5fe 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/server/path/multiple/implementation/MultipleClientImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/server/path/multiple/implementation/MultipleClientImpl.java @@ -1,19 +1,20 @@ package server.path.multiple.implementation; 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.PathParam; -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.implementation.utils.UriEscapers; 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.GeneratedCodeUtils; import server.path.multiple.MultipleServiceVersion; /** @@ -99,34 +100,69 @@ public MultipleClientImpl(HttpPipeline httpPipeline, Instrumentation instrumenta } /** - * The interface defining all the services for MultipleClient to be used by the proxy service to perform REST calls. + * The HTTP operations for MultipleClient. */ - @ServiceInterface(name = "MultipleClient", host = "{endpoint}/server/path/multiple/{apiVersion}") public interface MultipleClientService { static MultipleClientService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("server.path.multiple.implementation.MultipleClientServiceImpl"); - return (MultipleClientService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new MultipleClientServiceImpl(pipeline); } - @HttpRequestInformation(method = HttpMethod.GET, path = "/", expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response noOperationParams(@HostParam("endpoint") String endpoint, - @HostParam("apiVersion") String apiVersion, RequestContext requestContext); + Response noOperationParams(String endpoint, String apiVersion, RequestContext requestContext); - @HttpRequestInformation(method = HttpMethod.GET, path = "/{keyword}", expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withOperationPathParam(@HostParam("endpoint") String endpoint, - @HostParam("apiVersion") String apiVersion, @PathParam("keyword") String keyword, + Response withOperationPathParam(String endpoint, String apiVersion, String keyword, RequestContext requestContext); } + private static final class MultipleClientServiceImpl implements MultipleClientService { + private static final ClientLogger HTTP_LOG = new ClientLogger(MultipleClientService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private MultipleClientServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response noOperationParams(String endpoint, String apiVersion, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/server/path/multiple/" + apiVersion + "/"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withOperationPathParam(String endpoint, String apiVersion, String keyword, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri( + endpoint + "/server/path/multiple/" + apiVersion + "/" + UriEscapers.PATH_ESCAPER.escape(keyword)); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + } + /** * The noOperationParams operation. * diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/server/path/single/implementation/SingleClientImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/server/path/single/implementation/SingleClientImpl.java index 09dd1a6326f..7a7d0afe32d 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/server/path/single/implementation/SingleClientImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/server/path/single/implementation/SingleClientImpl.java @@ -1,18 +1,19 @@ package server.path.single.implementation; 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.GeneratedCodeUtils; /** * Initializes a new instance of the SingleClient type. @@ -80,28 +81,45 @@ public SingleClientImpl(HttpPipeline httpPipeline, Instrumentation instrumentati } /** - * The interface defining all the services for SingleClient to be used by the proxy service to perform REST calls. + * The HTTP operations for SingleClient. */ - @ServiceInterface(name = "SingleClient", host = "{endpoint}") public interface SingleClientService { static SingleClientService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("server.path.single.implementation.SingleClientServiceImpl"); - return (SingleClientService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new SingleClientServiceImpl(pipeline); + } + + Response myOp(String endpoint, RequestContext requestContext); + } + + private static final class SingleClientServiceImpl implements SingleClientService { + private static final ClientLogger HTTP_LOG = new ClientLogger(SingleClientService.class); + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private SingleClientServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation( - method = HttpMethod.HEAD, - path = "/server/path/single/myOp", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response myOp(@HostParam("endpoint") String endpoint, RequestContext requestContext); + @Override + public Response myOp(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.HEAD).setUri(endpoint + "/server/path/single/myOp"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response networkResponse = this.httpPipeline.send(httpRequest)) { + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), null); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/server/versions/notversioned/implementation/NotVersionedClientImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/server/versions/notversioned/implementation/NotVersionedClientImpl.java index c60b2191495..77da900ca91 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/server/versions/notversioned/implementation/NotVersionedClientImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/server/versions/notversioned/implementation/NotVersionedClientImpl.java @@ -1,20 +1,21 @@ package server.versions.notversioned.implementation; 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.PathParam; -import io.clientcore.core.http.annotations.QueryParam; -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.implementation.utils.UriEscapers; 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.GeneratedCodeUtils; +import io.clientcore.core.utils.UriBuilder; /** * Initializes a new instance of the NotVersionedClient type. @@ -82,46 +83,86 @@ public NotVersionedClientImpl(HttpPipeline httpPipeline, Instrumentation instrum } /** - * The interface defining all the services for NotVersionedClient to be used by the proxy service to perform REST - * calls. + * The HTTP operations for NotVersionedClient. */ - @ServiceInterface(name = "NotVersionedClient", host = "{endpoint}") public interface NotVersionedClientService { static NotVersionedClientService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz - = Class.forName("server.versions.notversioned.implementation.NotVersionedClientServiceImpl"); - return (NotVersionedClientService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new NotVersionedClientServiceImpl(pipeline); + } + + Response withoutApiVersion(String endpoint, RequestContext requestContext); + + Response withQueryApiVersion(String endpoint, String apiVersion, RequestContext requestContext); + + Response withPathApiVersion(String endpoint, String apiVersion, RequestContext requestContext); + } + + private static final class NotVersionedClientServiceImpl implements NotVersionedClientService { + private static final ClientLogger HTTP_LOG = new ClientLogger(NotVersionedClientService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private NotVersionedClientServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response withoutApiVersion(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.HEAD) + .setUri(endpoint + "/server/versions/not-versioned/without-api-version"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response networkResponse = this.httpPipeline.send(httpRequest)) { + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + 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 withQueryApiVersion(String endpoint, String apiVersion, RequestContext requestContext) { + UriBuilder uriBuilder + = UriBuilder.parse(endpoint + "/server/versions/not-versioned/with-query-api-version"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "api-version", true, apiVersion, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.HEAD).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response networkResponse = this.httpPipeline.send(httpRequest)) { + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), null); + } } - @HttpRequestInformation( - method = HttpMethod.HEAD, - path = "/server/versions/not-versioned/without-api-version", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response withoutApiVersion(@HostParam("endpoint") String endpoint, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.HEAD, - path = "/server/versions/not-versioned/with-query-api-version", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response withQueryApiVersion(@HostParam("endpoint") String endpoint, - @QueryParam("api-version") String apiVersion, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.HEAD, - path = "/server/versions/not-versioned/with-path-api-version/{apiVersion}", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response withPathApiVersion(@HostParam("endpoint") String endpoint, - @PathParam("apiVersion") String apiVersion, RequestContext requestContext); + @Override + public Response withPathApiVersion(String endpoint, String apiVersion, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.HEAD) + .setUri(endpoint + "/server/versions/not-versioned/with-path-api-version/" + + UriEscapers.PATH_ESCAPER.escape(apiVersion)); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response networkResponse = this.httpPipeline.send(httpRequest)) { + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), null); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/server/versions/versioned/implementation/VersionedClientImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/server/versions/versioned/implementation/VersionedClientImpl.java index 4e1e1afd2a0..761e262046c 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/server/versions/versioned/implementation/VersionedClientImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/server/versions/versioned/implementation/VersionedClientImpl.java @@ -1,20 +1,21 @@ package server.versions.versioned.implementation; 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.PathParam; -import io.clientcore.core.http.annotations.QueryParam; -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.implementation.utils.UriEscapers; 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.GeneratedCodeUtils; +import io.clientcore.core.utils.UriBuilder; import server.versions.versioned.VersionedServiceVersion; /** @@ -100,53 +101,107 @@ public VersionedClientImpl(HttpPipeline httpPipeline, Instrumentation instrument } /** - * The interface defining all the services for VersionedClient to be used by the proxy service to perform REST - * calls. + * The HTTP operations for VersionedClient. */ - @ServiceInterface(name = "VersionedClient", host = "{endpoint}") public interface VersionedClientService { static VersionedClientService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("server.versions.versioned.implementation.VersionedClientServiceImpl"); - return (VersionedClientService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new VersionedClientServiceImpl(pipeline); + } + + Response withoutApiVersion(String endpoint, RequestContext requestContext); + + Response withQueryApiVersion(String endpoint, String apiVersion, RequestContext requestContext); + + Response withPathApiVersion(String endpoint, String apiVersion, RequestContext requestContext); + + Response withQueryOldApiVersion(String endpoint, String apiVersion, RequestContext requestContext); + } + + private static final class VersionedClientServiceImpl implements VersionedClientService { + private static final ClientLogger HTTP_LOG = new ClientLogger(VersionedClientService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private VersionedClientServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response withoutApiVersion(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.HEAD) + .setUri(endpoint + "/server/versions/versioned/without-api-version"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response networkResponse = this.httpPipeline.send(httpRequest)) { + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + 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 withQueryApiVersion(String endpoint, String apiVersion, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/server/versions/versioned/with-query-api-version"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "api-version", true, apiVersion, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.HEAD).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response networkResponse = this.httpPipeline.send(httpRequest)) { + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + 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 withPathApiVersion(String endpoint, String apiVersion, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.HEAD) + .setUri(endpoint + "/server/versions/versioned/with-path-api-version/" + + UriEscapers.PATH_ESCAPER.escape(apiVersion)); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response networkResponse = this.httpPipeline.send(httpRequest)) { + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), null); + } } - @HttpRequestInformation( - method = HttpMethod.HEAD, - path = "/server/versions/versioned/without-api-version", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response withoutApiVersion(@HostParam("endpoint") String endpoint, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.HEAD, - path = "/server/versions/versioned/with-query-api-version", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response withQueryApiVersion(@HostParam("endpoint") String endpoint, - @QueryParam("api-version") String apiVersion, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.HEAD, - path = "/server/versions/versioned/with-path-api-version/{apiVersion}", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response withPathApiVersion(@HostParam("endpoint") String endpoint, - @PathParam("apiVersion") String apiVersion, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.HEAD, - path = "/server/versions/versioned/with-query-old-api-version", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response withQueryOldApiVersion(@HostParam("endpoint") String endpoint, - @QueryParam("api-version") String apiVersion, RequestContext requestContext); + @Override + public Response withQueryOldApiVersion(String endpoint, String apiVersion, + RequestContext requestContext) { + UriBuilder uriBuilder + = UriBuilder.parse(endpoint + "/server/versions/versioned/with-query-old-api-version"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "api-version", true, apiVersion, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.HEAD).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response networkResponse = this.httpPipeline.send(httpRequest)) { + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), null); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/specialheaders/conditionalrequest/implementation/ConditionalRequestClientImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/specialheaders/conditionalrequest/implementation/ConditionalRequestClientImpl.java index 1dee1017503..81ad76dbceb 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/specialheaders/conditionalrequest/implementation/ConditionalRequestClientImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/specialheaders/conditionalrequest/implementation/ConditionalRequestClientImpl.java @@ -1,20 +1,21 @@ package specialheaders.conditionalrequest.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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 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.DateTimeRfc1123; -import java.lang.reflect.InvocationTargetException; +import io.clientcore.core.utils.GeneratedCodeUtils; import java.time.OffsetDateTime; /** @@ -83,55 +84,120 @@ public ConditionalRequestClientImpl(HttpPipeline httpPipeline, Instrumentation i } /** - * The interface defining all the services for ConditionalRequestClient to be used by the proxy service to perform - * REST calls. + * The HTTP operations for ConditionalRequestClient. */ - @ServiceInterface(name = "ConditionalRequestClient", host = "{endpoint}") public interface ConditionalRequestClientService { static ConditionalRequestClientService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class - .forName("specialheaders.conditionalrequest.implementation.ConditionalRequestClientServiceImpl"); - return (ConditionalRequestClientService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new ConditionalRequestClientServiceImpl(pipeline); + } + + Response postIfMatch(String endpoint, String ifMatch, RequestContext requestContext); + + Response postIfNoneMatch(String endpoint, String ifNoneMatch, RequestContext requestContext); + + Response headIfModifiedSince(String endpoint, DateTimeRfc1123 ifModifiedSince, + RequestContext requestContext); + + Response postIfUnmodifiedSince(String endpoint, DateTimeRfc1123 ifUnmodifiedSince, + RequestContext requestContext); + } + + private static final class ConditionalRequestClientServiceImpl implements ConditionalRequestClientService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ConditionalRequestClientService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ConditionalRequestClientServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response postIfMatch(String endpoint, String ifMatch, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/special-headers/conditional-request/if-match"); + if (ifMatch != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("If-Match"), ifMatch); } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 postIfNoneMatch(String endpoint, String ifNoneMatch, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/special-headers/conditional-request/if-none-match"); + if (ifNoneMatch != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("If-None-Match"), ifNoneMatch); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/special-headers/conditional-request/if-match", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response postIfMatch(@HostParam("endpoint") String endpoint, @HeaderParam("If-Match") String ifMatch, - RequestContext requestContext); + @Override + public Response headIfModifiedSince(String endpoint, DateTimeRfc1123 ifModifiedSince, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.HEAD) + .setUri(endpoint + "/special-headers/conditional-request/if-modified-since"); + if (ifModifiedSince != null) { + httpRequest.getHeaders() + .add(HttpHeaderName.fromString("If-Modified-Since"), String.valueOf(ifModifiedSince)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/special-headers/conditional-request/if-none-match", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response postIfNoneMatch(@HostParam("endpoint") String endpoint, - @HeaderParam("If-None-Match") String ifNoneMatch, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.HEAD, - path = "/special-headers/conditional-request/if-modified-since", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response headIfModifiedSince(@HostParam("endpoint") String endpoint, - @HeaderParam("If-Modified-Since") DateTimeRfc1123 ifModifiedSince, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/special-headers/conditional-request/if-unmodified-since", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response postIfUnmodifiedSince(@HostParam("endpoint") String endpoint, - @HeaderParam("If-Unmodified-Since") DateTimeRfc1123 ifUnmodifiedSince, RequestContext requestContext); + @Override + public Response postIfUnmodifiedSince(String endpoint, DateTimeRfc1123 ifUnmodifiedSince, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/special-headers/conditional-request/if-unmodified-since"); + if (ifUnmodifiedSince != null) { + httpRequest.getHeaders() + .add(HttpHeaderName.fromString("If-Unmodified-Since"), String.valueOf(ifUnmodifiedSince)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/specialheaders/repeatability/implementation/RepeatabilityClientImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/specialheaders/repeatability/implementation/RepeatabilityClientImpl.java index d795c6afa71..60209704035 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/specialheaders/repeatability/implementation/RepeatabilityClientImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/specialheaders/repeatability/implementation/RepeatabilityClientImpl.java @@ -1,20 +1,21 @@ package specialheaders.repeatability.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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 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.DateTimeRfc1123; -import java.lang.reflect.InvocationTargetException; +import io.clientcore.core.utils.GeneratedCodeUtils; import java.time.OffsetDateTime; import java.util.UUID; @@ -84,32 +85,55 @@ public RepeatabilityClientImpl(HttpPipeline httpPipeline, Instrumentation instru } /** - * The interface defining all the services for RepeatabilityClient to be used by the proxy service to perform REST - * calls. + * The HTTP operations for RepeatabilityClient. */ - @ServiceInterface(name = "RepeatabilityClient", host = "{endpoint}") public interface RepeatabilityClientService { static RepeatabilityClientService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz - = Class.forName("specialheaders.repeatability.implementation.RepeatabilityClientServiceImpl"); - return (RepeatabilityClientService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new RepeatabilityClientServiceImpl(pipeline); + } + + Response immediateSuccess(String endpoint, String repeatabilityRequestId, String repeatabilityFirstSent, + RequestContext requestContext); + } + + private static final class RepeatabilityClientServiceImpl implements RepeatabilityClientService { + private static final ClientLogger HTTP_LOG = new ClientLogger(RepeatabilityClientService.class); + + private final HttpPipeline httpPipeline; + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private RepeatabilityClientServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/special-headers/repeatability/immediateSuccess", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response immediateSuccess(@HostParam("endpoint") String endpoint, - @HeaderParam("repeatability-request-id") String repeatabilityRequestId, - @HeaderParam("repeatability-first-sent") String repeatabilityFirstSent, RequestContext requestContext); + @Override + public Response immediateSuccess(String endpoint, String repeatabilityRequestId, + String repeatabilityFirstSent, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/special-headers/repeatability/immediateSuccess"); + if (repeatabilityRequestId != null) { + httpRequest.getHeaders() + .add(HttpHeaderName.fromString("repeatability-request-id"), repeatabilityRequestId); + } + if (repeatabilityFirstSent != null) { + httpRequest.getHeaders() + .add(HttpHeaderName.fromString("repeatability-first-sent"), repeatabilityFirstSent); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/specialwords/implementation/ExtensibleStringsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/specialwords/implementation/ExtensibleStringsImpl.java index 5443917a20c..522a93dce40 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/specialwords/implementation/ExtensibleStringsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/specialwords/implementation/ExtensibleStringsImpl.java @@ -1,20 +1,23 @@ package specialwords.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import specialwords.extensiblestrings.ExtensibleString; /** @@ -48,31 +51,71 @@ public final class ExtensibleStringsImpl { } /** - * The interface defining all the services for SpecialWordsClientExtensibleStrings to be used by the proxy service - * to perform REST calls. + * The HTTP operations for SpecialWordsClientExtensibleStrings. */ - @ServiceInterface(name = "SpecialWordsClientExtensibleStrings", host = "{endpoint}") public interface ExtensibleStringsService { static ExtensibleStringsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("specialwords.implementation.ExtensibleStringsServiceImpl"); - return (ExtensibleStringsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new ExtensibleStringsServiceImpl(pipeline); + } + + Response putExtensibleStringValue(String endpoint, String contentType, String accept, + ExtensibleString body, RequestContext requestContext); + } + + private static final class ExtensibleStringsServiceImpl implements ExtensibleStringsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ExtensibleStringsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + private ExtensibleStringsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/special-words/extensible-strings/string", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response putExtensibleStringValue(@HostParam("endpoint") String endpoint, - @HeaderParam("content-type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") ExtensibleString body, RequestContext requestContext); + @Override + public Response putExtensibleStringValue(String endpoint, String contentType, String accept, + ExtensibleString body, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/special-words/extensible-strings/string"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, ExtensibleString.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/specialwords/implementation/ModelPropertiesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/specialwords/implementation/ModelPropertiesImpl.java index 8032f14c309..a9125bcce2e 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/specialwords/implementation/ModelPropertiesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/specialwords/implementation/ModelPropertiesImpl.java @@ -1,20 +1,22 @@ package specialwords.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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 specialwords.modelproperties.DictMethods; import specialwords.modelproperties.ModelWithList; import specialwords.modelproperties.SameAsModel; @@ -50,48 +52,109 @@ public final class ModelPropertiesImpl { } /** - * The interface defining all the services for SpecialWordsClientModelProperties to be used by the proxy service to - * perform REST calls. + * The HTTP operations for SpecialWordsClientModelProperties. */ - @ServiceInterface(name = "SpecialWordsClientModelProperties", host = "{endpoint}") public interface ModelPropertiesService { static ModelPropertiesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("specialwords.implementation.ModelPropertiesServiceImpl"); - return (ModelPropertiesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new ModelPropertiesServiceImpl(pipeline); } - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/special-words/model-properties/same-as-model", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response sameAsModel(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") SameAsModel body, + Response sameAsModel(String endpoint, String contentType, SameAsModel body, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/special-words/model-properties/dict-methods", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response dictMethods(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") DictMethods body, + Response dictMethods(String endpoint, String contentType, DictMethods body, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/special-words/model-properties/list", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withList(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") ModelWithList body, RequestContext requestContext); + Response withList(String endpoint, String contentType, ModelWithList body, RequestContext requestContext); + } + + private static final class ModelPropertiesServiceImpl implements ModelPropertiesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ModelPropertiesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ModelPropertiesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response sameAsModel(String endpoint, String contentType, SameAsModel body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/special-words/model-properties/same-as-model"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 dictMethods(String endpoint, String contentType, DictMethods body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/special-words/model-properties/dict-methods"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withList(String endpoint, String contentType, ModelWithList body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/special-words/model-properties/list"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/specialwords/implementation/ModelsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/specialwords/implementation/ModelsImpl.java index 3ef38084e01..706342ba64e 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/specialwords/implementation/ModelsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/specialwords/implementation/ModelsImpl.java @@ -1,20 +1,22 @@ package specialwords.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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 specialwords.models.And; import specialwords.models.As; import specialwords.models.Assert; @@ -80,301 +82,901 @@ public final class ModelsImpl { } /** - * The interface defining all the services for SpecialWordsClientModels to be used by the proxy service to perform - * REST calls. + * The HTTP operations for SpecialWordsClientModels. */ - @ServiceInterface(name = "SpecialWordsClientModels", host = "{endpoint}") public interface ModelsService { static ModelsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("specialwords.implementation.ModelsServiceImpl"); - return (ModelsService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - - } - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/special-words/models/and", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withAnd(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") And body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/special-words/models/as", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withAs(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") As body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/special-words/models/assert", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withAssert(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") Assert body, - RequestContext requestContext); + return new ModelsServiceImpl(pipeline); + } - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/special-words/models/async", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withAsync(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") Async body, - RequestContext requestContext); + Response withAnd(String endpoint, String contentType, And body, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/special-words/models/await", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withAwait(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") Await body, - RequestContext requestContext); + Response withAs(String endpoint, String contentType, As body, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/special-words/models/break", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withBreak(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") Break body, - RequestContext requestContext); + Response withAssert(String endpoint, String contentType, Assert body, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/special-words/models/class", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withClass(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") ClassModel body, - RequestContext requestContext); + Response withAsync(String endpoint, String contentType, Async body, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/special-words/models/constructor", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withConstructor(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") Constructor body, - RequestContext requestContext); + Response withAwait(String endpoint, String contentType, Await body, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/special-words/models/continue", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withContinue(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") Continue body, - RequestContext requestContext); + Response withBreak(String endpoint, String contentType, Break body, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/special-words/models/def", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withDef(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") Def body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/special-words/models/del", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withDel(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") Del body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/special-words/models/elif", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withElif(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") Elif body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/special-words/models/else", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withElse(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") Else body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/special-words/models/except", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withExcept(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") Except body, - RequestContext requestContext); + Response withClass(String endpoint, String contentType, ClassModel body, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/special-words/models/exec", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withExec(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") Exec body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/special-words/models/finally", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withFinally(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") Finally body, + Response withConstructor(String endpoint, String contentType, Constructor body, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/special-words/models/for", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withFor(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") For body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/special-words/models/from", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withFrom(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") From body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/special-words/models/global", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withGlobal(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") Global body, - RequestContext requestContext); + Response withContinue(String endpoint, String contentType, Continue body, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/special-words/models/if", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withIf(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") If body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/special-words/models/import", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withImport(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") Import body, - RequestContext requestContext); + Response withDef(String endpoint, String contentType, Def body, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/special-words/models/in", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withIn(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") In body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/special-words/models/is", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withIs(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") Is body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/special-words/models/lambda", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withLambda(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") Lambda body, - RequestContext requestContext); + Response withDel(String endpoint, String contentType, Del body, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/special-words/models/not", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withNot(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") Not body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/special-words/models/or", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withOr(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") Or body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/special-words/models/pass", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withPass(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") Pass body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/special-words/models/raise", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withRaise(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") Raise body, - RequestContext requestContext); + Response withElif(String endpoint, String contentType, Elif body, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/special-words/models/return", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withReturn(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") Return body, - RequestContext requestContext); + Response withElse(String endpoint, String contentType, Else body, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/special-words/models/try", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withTry(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") Try body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/special-words/models/while", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withWhile(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") While body, - RequestContext requestContext); + Response withExcept(String endpoint, String contentType, Except body, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/special-words/models/with", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withWith(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") With body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/special-words/models/yield", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withYield(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") Yield body, - RequestContext requestContext); + Response withExec(String endpoint, String contentType, Exec body, RequestContext requestContext); + + Response withFinally(String endpoint, String contentType, Finally body, RequestContext requestContext); + + Response withFor(String endpoint, String contentType, For body, RequestContext requestContext); + + Response withFrom(String endpoint, String contentType, From body, RequestContext requestContext); + + Response withGlobal(String endpoint, String contentType, Global body, RequestContext requestContext); + + Response withIf(String endpoint, String contentType, If body, RequestContext requestContext); + + Response withImport(String endpoint, String contentType, Import body, RequestContext requestContext); + + Response withIn(String endpoint, String contentType, In body, RequestContext requestContext); + + Response withIs(String endpoint, String contentType, Is body, RequestContext requestContext); + + Response withLambda(String endpoint, String contentType, Lambda body, RequestContext requestContext); + + Response withNot(String endpoint, String contentType, Not body, RequestContext requestContext); + + Response withOr(String endpoint, String contentType, Or body, RequestContext requestContext); + + Response withPass(String endpoint, String contentType, Pass body, RequestContext requestContext); + + Response withRaise(String endpoint, String contentType, Raise body, RequestContext requestContext); + + Response withReturn(String endpoint, String contentType, Return body, RequestContext requestContext); + + Response withTry(String endpoint, String contentType, Try body, RequestContext requestContext); + + Response withWhile(String endpoint, String contentType, While body, RequestContext requestContext); + + Response withWith(String endpoint, String contentType, With body, RequestContext requestContext); + + Response withYield(String endpoint, String contentType, Yield body, RequestContext requestContext); + } + + private static final class ModelsServiceImpl implements ModelsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ModelsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ModelsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response withAnd(String endpoint, String contentType, And body, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/special-words/models/and"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withAs(String endpoint, String contentType, As body, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/special-words/models/as"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withAssert(String endpoint, String contentType, Assert body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/special-words/models/assert"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withAsync(String endpoint, String contentType, Async body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/special-words/models/async"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withAwait(String endpoint, String contentType, Await body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/special-words/models/await"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withBreak(String endpoint, String contentType, Break body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/special-words/models/break"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withClass(String endpoint, String contentType, ClassModel body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/special-words/models/class"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withConstructor(String endpoint, String contentType, Constructor body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/special-words/models/constructor"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withContinue(String endpoint, String contentType, Continue body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/special-words/models/continue"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withDef(String endpoint, String contentType, Def body, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/special-words/models/def"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withDel(String endpoint, String contentType, Del body, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/special-words/models/del"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withElif(String endpoint, String contentType, Elif body, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/special-words/models/elif"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withElse(String endpoint, String contentType, Else body, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/special-words/models/else"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withExcept(String endpoint, String contentType, Except body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/special-words/models/except"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withExec(String endpoint, String contentType, Exec body, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/special-words/models/exec"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withFinally(String endpoint, String contentType, Finally body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/special-words/models/finally"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withFor(String endpoint, String contentType, For body, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/special-words/models/for"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withFrom(String endpoint, String contentType, From body, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/special-words/models/from"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withGlobal(String endpoint, String contentType, Global body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/special-words/models/global"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withIf(String endpoint, String contentType, If body, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/special-words/models/if"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withImport(String endpoint, String contentType, Import body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/special-words/models/import"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withIn(String endpoint, String contentType, In body, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/special-words/models/in"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withIs(String endpoint, String contentType, Is body, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/special-words/models/is"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withLambda(String endpoint, String contentType, Lambda body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/special-words/models/lambda"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withNot(String endpoint, String contentType, Not body, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/special-words/models/not"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withOr(String endpoint, String contentType, Or body, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/special-words/models/or"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withPass(String endpoint, String contentType, Pass body, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/special-words/models/pass"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withRaise(String endpoint, String contentType, Raise body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/special-words/models/raise"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withReturn(String endpoint, String contentType, Return body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/special-words/models/return"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withTry(String endpoint, String contentType, Try body, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/special-words/models/try"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withWhile(String endpoint, String contentType, While body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/special-words/models/while"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withWith(String endpoint, String contentType, With body, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/special-words/models/with"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withYield(String endpoint, String contentType, Yield body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/special-words/models/yield"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/specialwords/implementation/OperationsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/specialwords/implementation/OperationsImpl.java index e7007f7cf17..30f3e429746 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/specialwords/implementation/OperationsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/specialwords/implementation/OperationsImpl.java @@ -1,18 +1,19 @@ package specialwords.implementation; 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.GeneratedCodeUtils; /** * An instance of this class provides access to all the operations defined in Operations. @@ -45,252 +46,653 @@ public final class OperationsImpl { } /** - * The interface defining all the services for SpecialWordsClientOperations to be used by the proxy service to - * perform REST calls. + * The HTTP operations for SpecialWordsClientOperations. */ - @ServiceInterface(name = "SpecialWordsClientOperations", host = "{endpoint}") public interface OperationsService { static OperationsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("specialwords.implementation.OperationsServiceImpl"); - return (OperationsService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - - } - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/operations/and", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response and(@HostParam("endpoint") String endpoint, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/operations/as", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response as(@HostParam("endpoint") String endpoint, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/operations/assert", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response assertMethod(@HostParam("endpoint") String endpoint, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/operations/async", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response async(@HostParam("endpoint") String endpoint, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/operations/await", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response await(@HostParam("endpoint") String endpoint, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/operations/break", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response breakMethod(@HostParam("endpoint") String endpoint, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/operations/class", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response classMethod(@HostParam("endpoint") String endpoint, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/operations/constructor", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response constructor(@HostParam("endpoint") String endpoint, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/operations/continue", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response continueMethod(@HostParam("endpoint") String endpoint, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/operations/def", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response def(@HostParam("endpoint") String endpoint, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/operations/del", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response del(@HostParam("endpoint") String endpoint, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/operations/elif", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response elif(@HostParam("endpoint") String endpoint, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/operations/else", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response elseMethod(@HostParam("endpoint") String endpoint, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/operations/except", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response except(@HostParam("endpoint") String endpoint, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/operations/exec", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response exec(@HostParam("endpoint") String endpoint, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/operations/finally", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response finallyMethod(@HostParam("endpoint") String endpoint, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/operations/for", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response forMethod(@HostParam("endpoint") String endpoint, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/operations/from", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response from(@HostParam("endpoint") String endpoint, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/operations/global", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response global(@HostParam("endpoint") String endpoint, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/operations/if", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response ifMethod(@HostParam("endpoint") String endpoint, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/operations/import", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response importMethod(@HostParam("endpoint") String endpoint, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/operations/in", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response in(@HostParam("endpoint") String endpoint, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/operations/is", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response is(@HostParam("endpoint") String endpoint, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/operations/lambda", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response lambda(@HostParam("endpoint") String endpoint, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/operations/not", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response not(@HostParam("endpoint") String endpoint, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/operations/or", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response or(@HostParam("endpoint") String endpoint, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/operations/pass", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response pass(@HostParam("endpoint") String endpoint, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/operations/raise", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response raise(@HostParam("endpoint") String endpoint, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/operations/return", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response returnMethod(@HostParam("endpoint") String endpoint, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/operations/try", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response tryMethod(@HostParam("endpoint") String endpoint, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/operations/while", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response whileMethod(@HostParam("endpoint") String endpoint, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/operations/with", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response with(@HostParam("endpoint") String endpoint, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/operations/yield", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response yield(@HostParam("endpoint") String endpoint, RequestContext requestContext); + return new OperationsServiceImpl(pipeline); + } + + Response and(String endpoint, RequestContext requestContext); + + Response as(String endpoint, RequestContext requestContext); + + Response assertMethod(String endpoint, RequestContext requestContext); + + Response async(String endpoint, RequestContext requestContext); + + Response await(String endpoint, RequestContext requestContext); + + Response breakMethod(String endpoint, RequestContext requestContext); + + Response classMethod(String endpoint, RequestContext requestContext); + + Response constructor(String endpoint, RequestContext requestContext); + + Response continueMethod(String endpoint, RequestContext requestContext); + + Response def(String endpoint, RequestContext requestContext); + + Response del(String endpoint, RequestContext requestContext); + + Response elif(String endpoint, RequestContext requestContext); + + Response elseMethod(String endpoint, RequestContext requestContext); + + Response except(String endpoint, RequestContext requestContext); + + Response exec(String endpoint, RequestContext requestContext); + + Response finallyMethod(String endpoint, RequestContext requestContext); + + Response forMethod(String endpoint, RequestContext requestContext); + + Response from(String endpoint, RequestContext requestContext); + + Response global(String endpoint, RequestContext requestContext); + + Response ifMethod(String endpoint, RequestContext requestContext); + + Response importMethod(String endpoint, RequestContext requestContext); + + Response in(String endpoint, RequestContext requestContext); + + Response is(String endpoint, RequestContext requestContext); + + Response lambda(String endpoint, RequestContext requestContext); + + Response not(String endpoint, RequestContext requestContext); + + Response or(String endpoint, RequestContext requestContext); + + Response pass(String endpoint, RequestContext requestContext); + + Response raise(String endpoint, RequestContext requestContext); + + Response returnMethod(String endpoint, RequestContext requestContext); + + Response tryMethod(String endpoint, RequestContext requestContext); + + Response whileMethod(String endpoint, RequestContext requestContext); + + Response with(String endpoint, RequestContext requestContext); + + Response yield(String endpoint, RequestContext requestContext); + } + + private static final class OperationsServiceImpl implements OperationsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(OperationsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private OperationsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response and(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/special-words/operations/and"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 as(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/special-words/operations/as"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 assertMethod(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/special-words/operations/assert"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 async(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/special-words/operations/async"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 await(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/special-words/operations/await"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 breakMethod(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/special-words/operations/break"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 classMethod(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/special-words/operations/class"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 constructor(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/special-words/operations/constructor"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 continueMethod(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/special-words/operations/continue"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 def(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/special-words/operations/def"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 del(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/special-words/operations/del"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 elif(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/special-words/operations/elif"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 elseMethod(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/special-words/operations/else"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 except(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/special-words/operations/except"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 exec(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/special-words/operations/exec"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 finallyMethod(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/special-words/operations/finally"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 forMethod(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/special-words/operations/for"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 from(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/special-words/operations/from"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 global(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/special-words/operations/global"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 ifMethod(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/special-words/operations/if"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 importMethod(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/special-words/operations/import"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 in(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/special-words/operations/in"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 is(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/special-words/operations/is"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 lambda(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/special-words/operations/lambda"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 not(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/special-words/operations/not"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 or(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/special-words/operations/or"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 pass(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/special-words/operations/pass"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 raise(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/special-words/operations/raise"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 returnMethod(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/special-words/operations/return"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 tryMethod(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/special-words/operations/try"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 whileMethod(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/special-words/operations/while"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 with(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/special-words/operations/with"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 yield(String endpoint, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/special-words/operations/yield"); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/specialwords/implementation/ParametersImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/specialwords/implementation/ParametersImpl.java index 2379b167d42..d58279e01a4 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/specialwords/implementation/ParametersImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/specialwords/implementation/ParametersImpl.java @@ -1,19 +1,20 @@ package specialwords.implementation; 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.QueryParam; -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.GeneratedCodeUtils; +import io.clientcore.core.utils.UriBuilder; /** * An instance of this class provides access to all the operations defined in Parameters. @@ -46,293 +47,707 @@ public final class ParametersImpl { } /** - * The interface defining all the services for SpecialWordsClientParameters to be used by the proxy service to - * perform REST calls. + * The HTTP operations for SpecialWordsClientParameters. */ - @ServiceInterface(name = "SpecialWordsClientParameters", host = "{endpoint}") public interface ParametersService { static ParametersService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("specialwords.implementation.ParametersServiceImpl"); - return (ParametersService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - - } - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/parameters/and", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withAnd(@HostParam("endpoint") String endpoint, @QueryParam("and") String and, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/parameters/as", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withAs(@HostParam("endpoint") String endpoint, @QueryParam("as") String as, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/parameters/assert", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withAssert(@HostParam("endpoint") String endpoint, @QueryParam("assert") String assertParameter, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/parameters/async", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withAsync(@HostParam("endpoint") String endpoint, @QueryParam("async") String async, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/parameters/await", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withAwait(@HostParam("endpoint") String endpoint, @QueryParam("await") String await, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/parameters/break", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withBreak(@HostParam("endpoint") String endpoint, @QueryParam("break") String breakParameter, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/parameters/class", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withClass(@HostParam("endpoint") String endpoint, @QueryParam("class") String classParameter, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/parameters/constructor", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withConstructor(@HostParam("endpoint") String endpoint, - @QueryParam("constructor") String constructor, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/parameters/continue", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withContinue(@HostParam("endpoint") String endpoint, - @QueryParam("continue") String continueParameter, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/parameters/def", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withDef(@HostParam("endpoint") String endpoint, @QueryParam("def") String def, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/parameters/del", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withDel(@HostParam("endpoint") String endpoint, @QueryParam("del") String del, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/parameters/elif", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withElif(@HostParam("endpoint") String endpoint, @QueryParam("elif") String elif, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/parameters/else", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withElse(@HostParam("endpoint") String endpoint, @QueryParam("else") String elseParameter, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/parameters/except", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withExcept(@HostParam("endpoint") String endpoint, @QueryParam("except") String except, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/parameters/exec", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withExec(@HostParam("endpoint") String endpoint, @QueryParam("exec") String exec, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/parameters/finally", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withFinally(@HostParam("endpoint") String endpoint, - @QueryParam("finally") String finallyParameter, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/parameters/for", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withFor(@HostParam("endpoint") String endpoint, @QueryParam("for") String forParameter, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/parameters/from", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withFrom(@HostParam("endpoint") String endpoint, @QueryParam("from") String from, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/parameters/global", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withGlobal(@HostParam("endpoint") String endpoint, @QueryParam("global") String global, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/parameters/if", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withIf(@HostParam("endpoint") String endpoint, @QueryParam("if") String ifParameter, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/parameters/import", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withImport(@HostParam("endpoint") String endpoint, @QueryParam("import") String importParameter, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/parameters/in", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withIn(@HostParam("endpoint") String endpoint, @QueryParam("in") String in, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/parameters/is", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withIs(@HostParam("endpoint") String endpoint, @QueryParam("is") String is, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/parameters/lambda", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withLambda(@HostParam("endpoint") String endpoint, @QueryParam("lambda") String lambda, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/parameters/not", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withNot(@HostParam("endpoint") String endpoint, @QueryParam("not") String not, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/parameters/or", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withOr(@HostParam("endpoint") String endpoint, @QueryParam("or") String or, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/parameters/pass", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withPass(@HostParam("endpoint") String endpoint, @QueryParam("pass") String pass, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/parameters/raise", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withRaise(@HostParam("endpoint") String endpoint, @QueryParam("raise") String raise, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/parameters/return", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withReturn(@HostParam("endpoint") String endpoint, @QueryParam("return") String returnParameter, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/parameters/try", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withTry(@HostParam("endpoint") String endpoint, @QueryParam("try") String tryParameter, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/parameters/while", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withWhile(@HostParam("endpoint") String endpoint, @QueryParam("while") String whileParameter, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/parameters/with", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withWith(@HostParam("endpoint") String endpoint, @QueryParam("with") String with, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/parameters/yield", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withYield(@HostParam("endpoint") String endpoint, @QueryParam("yield") String yield, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/special-words/parameters/cancellationToken", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withCancellationToken(@HostParam("endpoint") String endpoint, - @QueryParam("cancellationToken") String cancellationToken, RequestContext requestContext); + return new ParametersServiceImpl(pipeline); + } + + Response withAnd(String endpoint, String and, RequestContext requestContext); + + Response withAs(String endpoint, String as, RequestContext requestContext); + + Response withAssert(String endpoint, String assertParameter, RequestContext requestContext); + + Response withAsync(String endpoint, String async, RequestContext requestContext); + + Response withAwait(String endpoint, String await, RequestContext requestContext); + + Response withBreak(String endpoint, String breakParameter, RequestContext requestContext); + + Response withClass(String endpoint, String classParameter, RequestContext requestContext); + + Response withConstructor(String endpoint, String constructor, RequestContext requestContext); + + Response withContinue(String endpoint, String continueParameter, RequestContext requestContext); + + Response withDef(String endpoint, String def, RequestContext requestContext); + + Response withDel(String endpoint, String del, RequestContext requestContext); + + Response withElif(String endpoint, String elif, RequestContext requestContext); + + Response withElse(String endpoint, String elseParameter, RequestContext requestContext); + + Response withExcept(String endpoint, String except, RequestContext requestContext); + + Response withExec(String endpoint, String exec, RequestContext requestContext); + + Response withFinally(String endpoint, String finallyParameter, RequestContext requestContext); + + Response withFor(String endpoint, String forParameter, RequestContext requestContext); + + Response withFrom(String endpoint, String from, RequestContext requestContext); + + Response withGlobal(String endpoint, String global, RequestContext requestContext); + + Response withIf(String endpoint, String ifParameter, RequestContext requestContext); + + Response withImport(String endpoint, String importParameter, RequestContext requestContext); + + Response withIn(String endpoint, String in, RequestContext requestContext); + + Response withIs(String endpoint, String is, RequestContext requestContext); + + Response withLambda(String endpoint, String lambda, RequestContext requestContext); + + Response withNot(String endpoint, String not, RequestContext requestContext); + + Response withOr(String endpoint, String or, RequestContext requestContext); + + Response withPass(String endpoint, String pass, RequestContext requestContext); + + Response withRaise(String endpoint, String raise, RequestContext requestContext); + + Response withReturn(String endpoint, String returnParameter, RequestContext requestContext); + + Response withTry(String endpoint, String tryParameter, RequestContext requestContext); + + Response withWhile(String endpoint, String whileParameter, RequestContext requestContext); + + Response withWith(String endpoint, String with, RequestContext requestContext); + + Response withYield(String endpoint, String yield, RequestContext requestContext); + + Response withCancellationToken(String endpoint, String cancellationToken, RequestContext requestContext); + } + + private static final class ParametersServiceImpl implements ParametersService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ParametersService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ParametersServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response withAnd(String endpoint, String and, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/special-words/parameters/and"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "and", true, and, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withAs(String endpoint, String as, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/special-words/parameters/as"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "as", true, as, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withAssert(String endpoint, String assertParameter, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/special-words/parameters/assert"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "assert", true, assertParameter, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withAsync(String endpoint, String async, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/special-words/parameters/async"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "async", true, async, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withAwait(String endpoint, String await, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/special-words/parameters/await"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "await", true, await, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withBreak(String endpoint, String breakParameter, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/special-words/parameters/break"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "break", true, breakParameter, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withClass(String endpoint, String classParameter, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/special-words/parameters/class"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "class", true, classParameter, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withConstructor(String endpoint, String constructor, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/special-words/parameters/constructor"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "constructor", true, constructor, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withContinue(String endpoint, String continueParameter, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/special-words/parameters/continue"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "continue", true, continueParameter, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withDef(String endpoint, String def, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/special-words/parameters/def"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "def", true, def, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withDel(String endpoint, String del, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/special-words/parameters/del"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "del", true, del, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withElif(String endpoint, String elif, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/special-words/parameters/elif"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "elif", true, elif, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withElse(String endpoint, String elseParameter, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/special-words/parameters/else"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "else", true, elseParameter, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withExcept(String endpoint, String except, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/special-words/parameters/except"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "except", true, except, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withExec(String endpoint, String exec, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/special-words/parameters/exec"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "exec", true, exec, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withFinally(String endpoint, String finallyParameter, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/special-words/parameters/finally"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "finally", true, finallyParameter, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withFor(String endpoint, String forParameter, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/special-words/parameters/for"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "for", true, forParameter, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withFrom(String endpoint, String from, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/special-words/parameters/from"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "from", true, from, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withGlobal(String endpoint, String global, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/special-words/parameters/global"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "global", true, global, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withIf(String endpoint, String ifParameter, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/special-words/parameters/if"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "if", true, ifParameter, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withImport(String endpoint, String importParameter, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/special-words/parameters/import"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "import", true, importParameter, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withIn(String endpoint, String in, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/special-words/parameters/in"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "in", true, in, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withIs(String endpoint, String is, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/special-words/parameters/is"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "is", true, is, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withLambda(String endpoint, String lambda, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/special-words/parameters/lambda"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "lambda", true, lambda, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withNot(String endpoint, String not, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/special-words/parameters/not"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "not", true, not, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withOr(String endpoint, String or, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/special-words/parameters/or"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "or", true, or, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withPass(String endpoint, String pass, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/special-words/parameters/pass"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "pass", true, pass, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withRaise(String endpoint, String raise, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/special-words/parameters/raise"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "raise", true, raise, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withReturn(String endpoint, String returnParameter, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/special-words/parameters/return"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "return", true, returnParameter, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withTry(String endpoint, String tryParameter, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/special-words/parameters/try"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "try", true, tryParameter, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withWhile(String endpoint, String whileParameter, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/special-words/parameters/while"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "while", true, whileParameter, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withWith(String endpoint, String with, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/special-words/parameters/with"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "with", true, with, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withYield(String endpoint, String yield, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/special-words/parameters/yield"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "yield", true, yield, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 withCancellationToken(String endpoint, String cancellationToken, + RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/special-words/parameters/cancellationToken"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "cancellationToken", true, cancellationToken, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/specialwords/implementation/ReservedOperationBodyParamsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/specialwords/implementation/ReservedOperationBodyParamsImpl.java index 6499bb60dbd..45a34adeed0 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/specialwords/implementation/ReservedOperationBodyParamsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/specialwords/implementation/ReservedOperationBodyParamsImpl.java @@ -1,20 +1,22 @@ package specialwords.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.List; import specialwords.reservedoperationbodyparams.implementation.WithItemsRequest; @@ -49,31 +51,54 @@ public final class ReservedOperationBodyParamsImpl { } /** - * The interface defining all the services for SpecialWordsClientReservedOperationBodyParams to be used by the proxy - * service to perform REST calls. + * The HTTP operations for SpecialWordsClientReservedOperationBodyParams. */ - @ServiceInterface(name = "SpecialWordsClientReservedOperationBodyParams", host = "{endpoint}") public interface ReservedOperationBodyParamsService { static ReservedOperationBodyParamsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("specialwords.implementation.ReservedOperationBodyParamsServiceImpl"); - return (ReservedOperationBodyParamsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new ReservedOperationBodyParamsServiceImpl(pipeline); + } + + Response withItems(String endpoint, String contentType, WithItemsRequest withItemsRequest, + RequestContext requestContext); + } + + private static final class ReservedOperationBodyParamsServiceImpl implements ReservedOperationBodyParamsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ReservedOperationBodyParamsService.class); + + private final HttpPipeline httpPipeline; + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ReservedOperationBodyParamsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/special-words/operations/body-param-reserved", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response withItems(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") WithItemsRequest withItemsRequest, RequestContext requestContext); + @Override + public Response withItems(String endpoint, String contentType, WithItemsRequest withItemsRequest, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/special-words/operations/body-param-reserved"); + if (withItemsRequest != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(withItemsRequest, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/streaming/jsonl/implementation/BasicsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/streaming/jsonl/implementation/BasicsImpl.java index 436530bac14..cd41efbf7e8 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/streaming/jsonl/implementation/BasicsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/streaming/jsonl/implementation/BasicsImpl.java @@ -1,21 +1,20 @@ package streaming.jsonl.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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 io.clientcore.core.instrumentation.logging.ClientLogger; import io.clientcore.core.models.binarydata.BinaryData; -import java.lang.reflect.InvocationTargetException; +import io.clientcore.core.serialization.json.JsonSerializer; +import io.clientcore.core.serialization.xml.XmlSerializer; +import io.clientcore.core.utils.GeneratedCodeUtils; /** * An instance of this class provides access to all the operations defined in Basics. @@ -48,38 +47,75 @@ public final class BasicsImpl { } /** - * The interface defining all the services for JsonlClientBasics to be used by the proxy service to perform REST - * calls. + * The HTTP operations for JsonlClientBasics. */ - @ServiceInterface(name = "JsonlClientBasics", host = "{endpoint}") public interface BasicsService { static BasicsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("streaming.jsonl.implementation.BasicsServiceImpl"); - return (BasicsService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new BasicsServiceImpl(pipeline); } - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/streaming/jsonl/basic/send", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response send(@HostParam("endpoint") String endpoint, @HeaderParam("content-type") String contentType, - @BodyParam("application/jsonl") BinaryData body, @HeaderParam("Content-Length") long contentLength, + Response send(String endpoint, String contentType, BinaryData body, long contentLength, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/streaming/jsonl/basic/receive", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response receive(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); + Response receive(String endpoint, String accept, RequestContext requestContext); + } + + private static final class BasicsServiceImpl implements BasicsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(BasicsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private BasicsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response send(String endpoint, String contentType, BinaryData body, long contentLength, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/streaming/jsonl/basic/send"); + httpRequest.getHeaders().add(HttpHeaderName.fromString("Content-Length"), String.valueOf(contentLength)); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + if (body.getLength() != null) { + httpRequest.setBody(body); + } + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 receive(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/streaming/jsonl/basic/receive"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return networkResponse; + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/streaming/sse/implementation/NamedsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/streaming/sse/implementation/NamedsImpl.java index a024bc46ba0..38257819078 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/streaming/sse/implementation/NamedsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/streaming/sse/implementation/NamedsImpl.java @@ -1,20 +1,20 @@ package streaming.sse.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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 io.clientcore.core.instrumentation.logging.ClientLogger; import io.clientcore.core.models.binarydata.BinaryData; -import java.lang.reflect.InvocationTargetException; +import io.clientcore.core.serialization.json.JsonSerializer; +import io.clientcore.core.serialization.xml.XmlSerializer; +import io.clientcore.core.utils.GeneratedCodeUtils; /** * An instance of this class provides access to all the operations defined in Nameds. @@ -47,29 +47,47 @@ public final class NamedsImpl { } /** - * The interface defining all the services for SseClientNameds to be used by the proxy service to perform REST - * calls. + * The HTTP operations for SseClientNameds. */ - @ServiceInterface(name = "SseClientNameds", host = "{endpoint}") public interface NamedsService { static NamedsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("streaming.sse.implementation.NamedsServiceImpl"); - return (NamedsService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new NamedsServiceImpl(pipeline); + } + + Response receive(String endpoint, String accept, RequestContext requestContext); + } + + private static final class NamedsServiceImpl implements NamedsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(NamedsService.class); + + private final HttpPipeline httpPipeline; + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private NamedsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/streaming/sse/named/receive", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response receive(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); + @Override + public Response receive(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/streaming/sse/named/receive"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return networkResponse; + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/streaming/sse/implementation/ProtocolDatasImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/streaming/sse/implementation/ProtocolDatasImpl.java index b6434c17f4b..83584f451f1 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/streaming/sse/implementation/ProtocolDatasImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/streaming/sse/implementation/ProtocolDatasImpl.java @@ -1,20 +1,20 @@ package streaming.sse.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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 io.clientcore.core.instrumentation.logging.ClientLogger; import io.clientcore.core.models.binarydata.BinaryData; -import java.lang.reflect.InvocationTargetException; +import io.clientcore.core.serialization.json.JsonSerializer; +import io.clientcore.core.serialization.xml.XmlSerializer; +import io.clientcore.core.utils.GeneratedCodeUtils; /** * An instance of this class provides access to all the operations defined in ProtocolDatas. @@ -47,38 +47,68 @@ public final class ProtocolDatasImpl { } /** - * The interface defining all the services for SseClientProtocolDatas to be used by the proxy service to perform - * REST calls. + * The HTTP operations for SseClientProtocolDatas. */ - @ServiceInterface(name = "SseClientProtocolDatas", host = "{endpoint}") public interface ProtocolDatasService { static ProtocolDatasService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("streaming.sse.implementation.ProtocolDatasServiceImpl"); - return (ProtocolDatasService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new ProtocolDatasServiceImpl(pipeline); + } + + Response withEnvelope(String endpoint, String accept, RequestContext requestContext); + + Response withoutEnvelope(String endpoint, String accept, RequestContext requestContext); + } + + private static final class ProtocolDatasServiceImpl implements ProtocolDatasService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ProtocolDatasService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ProtocolDatasServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response withEnvelope(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/streaming/sse/protocol/data/with-envelope"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return networkResponse; } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/streaming/sse/protocol/data/with-envelope", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response withEnvelope(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/streaming/sse/protocol/data/without-envelope", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response withoutEnvelope(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); + @Override + public Response withoutEnvelope(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/streaming/sse/protocol/data/without-envelope"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return networkResponse; + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/streaming/sse/implementation/ProtocolsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/streaming/sse/implementation/ProtocolsImpl.java index 2e338fd1c65..9532e706849 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/streaming/sse/implementation/ProtocolsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/streaming/sse/implementation/ProtocolsImpl.java @@ -1,20 +1,20 @@ package streaming.sse.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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 io.clientcore.core.instrumentation.logging.ClientLogger; import io.clientcore.core.models.binarydata.BinaryData; -import java.lang.reflect.InvocationTargetException; +import io.clientcore.core.serialization.json.JsonSerializer; +import io.clientcore.core.serialization.xml.XmlSerializer; +import io.clientcore.core.utils.GeneratedCodeUtils; /** * An instance of this class provides access to all the operations defined in Protocols. @@ -47,61 +47,131 @@ public final class ProtocolsImpl { } /** - * The interface defining all the services for SseClientProtocols to be used by the proxy service to perform REST - * calls. + * The HTTP operations for SseClientProtocols. */ - @ServiceInterface(name = "SseClientProtocols", host = "{endpoint}") public interface ProtocolsService { static ProtocolsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("streaming.sse.implementation.ProtocolsServiceImpl"); - return (ProtocolsService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new ProtocolsServiceImpl(pipeline); + } + + Response id(String endpoint, String accept, RequestContext requestContext); + + Response invalidId(String endpoint, String accept, RequestContext requestContext); + + Response retry(String endpoint, String accept, RequestContext requestContext); + + Response invalidRetry(String endpoint, String accept, RequestContext requestContext); + + Response reconnect(String endpoint, String accept, RequestContext requestContext); + } + + private static final class ProtocolsServiceImpl implements ProtocolsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ProtocolsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ProtocolsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response id(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/streaming/sse/protocol/id"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return networkResponse; + } + + @Override + public Response invalidId(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/streaming/sse/protocol/invalid-id"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return networkResponse; + } + + @Override + public Response retry(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/streaming/sse/protocol/retry"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return networkResponse; + } + @Override + public Response invalidRetry(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/streaming/sse/protocol/invalid-retry"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return networkResponse; } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/streaming/sse/protocol/id", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response id(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/streaming/sse/protocol/invalid-id", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response invalidId(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/streaming/sse/protocol/retry", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response retry(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/streaming/sse/protocol/invalid-retry", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response invalidRetry(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/streaming/sse/protocol/reconnect", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response reconnect(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); + @Override + public Response reconnect(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/streaming/sse/protocol/reconnect"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return networkResponse; + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/streaming/sse/implementation/RetrievesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/streaming/sse/implementation/RetrievesImpl.java index 6abd01cbcff..7ee03d025ab 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/streaming/sse/implementation/RetrievesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/streaming/sse/implementation/RetrievesImpl.java @@ -1,21 +1,22 @@ package streaming.sse.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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 io.clientcore.core.instrumentation.logging.ClientLogger; import io.clientcore.core.models.binarydata.BinaryData; -import java.lang.reflect.InvocationTargetException; +import io.clientcore.core.serialization.SerializationFormat; +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 streaming.sse.retrieve.RetrievalRequest; /** @@ -49,30 +50,56 @@ public final class RetrievesImpl { } /** - * The interface defining all the services for SseClientRetrieves to be used by the proxy service to perform REST - * calls. + * The HTTP operations for SseClientRetrieves. */ - @ServiceInterface(name = "SseClientRetrieves", host = "{endpoint}") public interface RetrievesService { static RetrievesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("streaming.sse.implementation.RetrievesServiceImpl"); - return (RetrievesService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new RetrievesServiceImpl(pipeline); + } + + Response stream(String endpoint, String contentType, String accept, RetrievalRequest request, + RequestContext requestContext); + } + + private static final class RetrievesServiceImpl implements RetrievesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(RetrievesService.class); + + private final HttpPipeline httpPipeline; + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private RetrievesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/streaming/sse/retrieve/stream", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response stream(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") RetrievalRequest request, RequestContext requestContext); + @Override + public Response stream(String endpoint, String contentType, String accept, RetrievalRequest request, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/streaming/sse/retrieve/stream"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (request != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(request, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return networkResponse; + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/streaming/sse/implementation/UnnamedsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/streaming/sse/implementation/UnnamedsImpl.java index 6a4294b5ea4..72acddffbd8 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/streaming/sse/implementation/UnnamedsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/streaming/sse/implementation/UnnamedsImpl.java @@ -1,20 +1,20 @@ package streaming.sse.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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 io.clientcore.core.instrumentation.logging.ClientLogger; import io.clientcore.core.models.binarydata.BinaryData; -import java.lang.reflect.InvocationTargetException; +import io.clientcore.core.serialization.json.JsonSerializer; +import io.clientcore.core.serialization.xml.XmlSerializer; +import io.clientcore.core.utils.GeneratedCodeUtils; /** * An instance of this class provides access to all the operations defined in Unnameds. @@ -47,29 +47,47 @@ public final class UnnamedsImpl { } /** - * The interface defining all the services for SseClientUnnameds to be used by the proxy service to perform REST - * calls. + * The HTTP operations for SseClientUnnameds. */ - @ServiceInterface(name = "SseClientUnnameds", host = "{endpoint}") public interface UnnamedsService { static UnnamedsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("streaming.sse.implementation.UnnamedsServiceImpl"); - return (UnnamedsService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new UnnamedsServiceImpl(pipeline); + } + + Response receive(String endpoint, String accept, RequestContext requestContext); + } + + private static final class UnnamedsServiceImpl implements UnnamedsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(UnnamedsService.class); + + private final HttpPipeline httpPipeline; + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private UnnamedsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/streaming/sse/unnamed/receive", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response receive(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); + @Override + public Response receive(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/streaming/sse/unnamed/receive"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return networkResponse; + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/BooleanValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/BooleanValuesImpl.java index cbf48c3ebf4..880942e5c4a 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/BooleanValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/BooleanValuesImpl.java @@ -1,20 +1,23 @@ package type.array.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import java.util.List; /** @@ -48,32 +51,90 @@ public final class BooleanValuesImpl { } /** - * The interface defining all the services for ArrayClientBooleanValues to be used by the proxy service to perform - * REST calls. + * The HTTP operations for ArrayClientBooleanValues. */ - @ServiceInterface(name = "ArrayClientBooleanValues", host = "{endpoint}") public interface BooleanValuesService { static BooleanValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.array.implementation.BooleanValuesServiceImpl"); - return (BooleanValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new BooleanValuesServiceImpl(pipeline); + } + + Response> get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, List body, RequestContext requestContext); + } + private static final class BooleanValuesServiceImpl implements BooleanValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(BooleanValuesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private BooleanValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation(method = HttpMethod.GET, path = "/type/array/boolean", expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response> get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); + @Override + public Response> get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/array/boolean"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, + CoreUtils.createParameterizedType(List.class, Boolean.class)), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } - @HttpRequestInformation(method = HttpMethod.PUT, path = "/type/array/boolean", expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") List body, RequestContext requestContext); + @Override + public Response put(String endpoint, String contentType, List body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/array/boolean"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/DatetimeValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/DatetimeValuesImpl.java index ef8c232546f..6cc1f7957a4 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/DatetimeValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/DatetimeValuesImpl.java @@ -1,20 +1,23 @@ package type.array.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import java.time.OffsetDateTime; import java.util.List; @@ -49,36 +52,91 @@ public final class DatetimeValuesImpl { } /** - * The interface defining all the services for ArrayClientDatetimeValues to be used by the proxy service to perform - * REST calls. + * The HTTP operations for ArrayClientDatetimeValues. */ - @ServiceInterface(name = "ArrayClientDatetimeValues", host = "{endpoint}") public interface DatetimeValuesService { static DatetimeValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.array.implementation.DatetimeValuesServiceImpl"); - return (DatetimeValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new DatetimeValuesServiceImpl(pipeline); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/array/datetime", - expectedStatusCodes = { 200 }, - returnValueWireType = OffsetDateTime.class) - @UnexpectedResponseExceptionDetail - Response> get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, + Response> get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, List body, RequestContext requestContext); + } + + private static final class DatetimeValuesServiceImpl implements DatetimeValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(DatetimeValuesService.class); + + private final HttpPipeline httpPipeline; - @HttpRequestInformation(method = HttpMethod.PUT, path = "/type/array/datetime", expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") List body, RequestContext requestContext); + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private DatetimeValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response> get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/array/datetime"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, + CoreUtils.createParameterizedType(List.class, OffsetDateTime.class)), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response put(String endpoint, String contentType, List body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/array/datetime"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/DurationValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/DurationValuesImpl.java index 800edce1e30..981244c9a1d 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/DurationValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/DurationValuesImpl.java @@ -1,20 +1,23 @@ package type.array.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import java.time.Duration; import java.util.List; @@ -49,32 +52,90 @@ public final class DurationValuesImpl { } /** - * The interface defining all the services for ArrayClientDurationValues to be used by the proxy service to perform - * REST calls. + * The HTTP operations for ArrayClientDurationValues. */ - @ServiceInterface(name = "ArrayClientDurationValues", host = "{endpoint}") public interface DurationValuesService { static DurationValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.array.implementation.DurationValuesServiceImpl"); - return (DurationValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new DurationValuesServiceImpl(pipeline); + } + + Response> get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, List body, RequestContext requestContext); + } + private static final class DurationValuesServiceImpl implements DurationValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(DurationValuesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private DurationValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation(method = HttpMethod.GET, path = "/type/array/duration", expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response> get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); + @Override + public Response> get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/array/duration"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, + CoreUtils.createParameterizedType(List.class, Duration.class)), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } - @HttpRequestInformation(method = HttpMethod.PUT, path = "/type/array/duration", expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") List body, RequestContext requestContext); + @Override + public Response put(String endpoint, String contentType, List body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/array/duration"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/Float32ValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/Float32ValuesImpl.java index b4a9e7c8f78..8bf199b6548 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/Float32ValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/Float32ValuesImpl.java @@ -1,20 +1,23 @@ package type.array.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import java.util.List; /** @@ -48,32 +51,90 @@ public final class Float32ValuesImpl { } /** - * The interface defining all the services for ArrayClientFloat32Values to be used by the proxy service to perform - * REST calls. + * The HTTP operations for ArrayClientFloat32Values. */ - @ServiceInterface(name = "ArrayClientFloat32Values", host = "{endpoint}") public interface Float32ValuesService { static Float32ValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.array.implementation.Float32ValuesServiceImpl"); - return (Float32ValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new Float32ValuesServiceImpl(pipeline); + } + + Response> get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, List body, RequestContext requestContext); + } + private static final class Float32ValuesServiceImpl implements Float32ValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(Float32ValuesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private Float32ValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation(method = HttpMethod.GET, path = "/type/array/float32", expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response> get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); + @Override + public Response> get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/array/float32"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, + CoreUtils.createParameterizedType(List.class, Double.class)), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } - @HttpRequestInformation(method = HttpMethod.PUT, path = "/type/array/float32", expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") List body, RequestContext requestContext); + @Override + public Response put(String endpoint, String contentType, List body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/array/float32"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/Int32ValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/Int32ValuesImpl.java index 8b6f36b9559..464987b6d4b 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/Int32ValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/Int32ValuesImpl.java @@ -1,20 +1,23 @@ package type.array.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import java.util.List; /** @@ -48,32 +51,90 @@ public final class Int32ValuesImpl { } /** - * The interface defining all the services for ArrayClientInt32Values to be used by the proxy service to perform - * REST calls. + * The HTTP operations for ArrayClientInt32Values. */ - @ServiceInterface(name = "ArrayClientInt32Values", host = "{endpoint}") public interface Int32ValuesService { static Int32ValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.array.implementation.Int32ValuesServiceImpl"); - return (Int32ValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new Int32ValuesServiceImpl(pipeline); + } + + Response> get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, List body, RequestContext requestContext); + } + private static final class Int32ValuesServiceImpl implements Int32ValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(Int32ValuesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private Int32ValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation(method = HttpMethod.GET, path = "/type/array/int32", expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response> get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); + @Override + public Response> get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/array/int32"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, + CoreUtils.createParameterizedType(List.class, Integer.class)), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } - @HttpRequestInformation(method = HttpMethod.PUT, path = "/type/array/int32", expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") List body, RequestContext requestContext); + @Override + public Response put(String endpoint, String contentType, List body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/array/int32"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/Int64ValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/Int64ValuesImpl.java index 031c95c83b8..a6a6452e5f2 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/Int64ValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/Int64ValuesImpl.java @@ -1,20 +1,23 @@ package type.array.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import java.util.List; /** @@ -48,32 +51,89 @@ public final class Int64ValuesImpl { } /** - * The interface defining all the services for ArrayClientInt64Values to be used by the proxy service to perform - * REST calls. + * The HTTP operations for ArrayClientInt64Values. */ - @ServiceInterface(name = "ArrayClientInt64Values", host = "{endpoint}") public interface Int64ValuesService { static Int64ValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.array.implementation.Int64ValuesServiceImpl"); - return (Int64ValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new Int64ValuesServiceImpl(pipeline); + } + + Response> get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, List body, RequestContext requestContext); + } + private static final class Int64ValuesServiceImpl implements Int64ValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(Int64ValuesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private Int64ValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation(method = HttpMethod.GET, path = "/type/array/int64", expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response> get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); + @Override + public Response> get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/array/int64"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, + CoreUtils.createParameterizedType(List.class, Long.class)), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } - @HttpRequestInformation(method = HttpMethod.PUT, path = "/type/array/int64", expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") List body, RequestContext requestContext); + @Override + public Response put(String endpoint, String contentType, List body, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/array/int64"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/ModelValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/ModelValuesImpl.java index f24873964e9..b1da1ac3b55 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/ModelValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/ModelValuesImpl.java @@ -1,20 +1,23 @@ package type.array.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import java.util.List; import type.array.InnerModel; @@ -49,32 +52,90 @@ public final class ModelValuesImpl { } /** - * The interface defining all the services for ArrayClientModelValues to be used by the proxy service to perform - * REST calls. + * The HTTP operations for ArrayClientModelValues. */ - @ServiceInterface(name = "ArrayClientModelValues", host = "{endpoint}") public interface ModelValuesService { static ModelValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.array.implementation.ModelValuesServiceImpl"); - return (ModelValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new ModelValuesServiceImpl(pipeline); + } + + Response> get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, List body, RequestContext requestContext); + } + private static final class ModelValuesServiceImpl implements ModelValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ModelValuesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ModelValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation(method = HttpMethod.GET, path = "/type/array/model", expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response> get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); + @Override + public Response> get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/array/model"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, + CoreUtils.createParameterizedType(List.class, InnerModel.class)), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } - @HttpRequestInformation(method = HttpMethod.PUT, path = "/type/array/model", expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") List body, RequestContext requestContext); + @Override + public Response put(String endpoint, String contentType, List body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/array/model"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/NullableBooleanValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/NullableBooleanValuesImpl.java index c2b0875cd00..d09305d1e3b 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/NullableBooleanValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/NullableBooleanValuesImpl.java @@ -1,20 +1,23 @@ package type.array.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import java.util.List; /** @@ -48,38 +51,90 @@ public final class NullableBooleanValuesImpl { } /** - * The interface defining all the services for ArrayClientNullableBooleanValues to be used by the proxy service to - * perform REST calls. + * The HTTP operations for ArrayClientNullableBooleanValues. */ - @ServiceInterface(name = "ArrayClientNullableBooleanValues", host = "{endpoint}") public interface NullableBooleanValuesService { static NullableBooleanValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.array.implementation.NullableBooleanValuesServiceImpl"); - return (NullableBooleanValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new NullableBooleanValuesServiceImpl(pipeline); + } + + Response> get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, List body, RequestContext requestContext); + } + + private static final class NullableBooleanValuesServiceImpl implements NullableBooleanValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(NullableBooleanValuesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private NullableBooleanValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response> get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/array/nullable-boolean"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, + CoreUtils.createParameterizedType(List.class, Boolean.class)), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, List body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/array/nullable-boolean"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/array/nullable-boolean", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response> get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/array/nullable-boolean", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") List body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/NullableFloatValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/NullableFloatValuesImpl.java index 773511cc584..5de8c55a12e 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/NullableFloatValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/NullableFloatValuesImpl.java @@ -1,20 +1,23 @@ package type.array.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import java.util.List; /** @@ -48,38 +51,90 @@ public final class NullableFloatValuesImpl { } /** - * The interface defining all the services for ArrayClientNullableFloatValues to be used by the proxy service to - * perform REST calls. + * The HTTP operations for ArrayClientNullableFloatValues. */ - @ServiceInterface(name = "ArrayClientNullableFloatValues", host = "{endpoint}") public interface NullableFloatValuesService { static NullableFloatValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.array.implementation.NullableFloatValuesServiceImpl"); - return (NullableFloatValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new NullableFloatValuesServiceImpl(pipeline); + } + + Response> get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, List body, RequestContext requestContext); + } + + private static final class NullableFloatValuesServiceImpl implements NullableFloatValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(NullableFloatValuesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private NullableFloatValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response> get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/array/nullable-float"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, + CoreUtils.createParameterizedType(List.class, Double.class)), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, List body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/array/nullable-float"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/array/nullable-float", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response> get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/array/nullable-float", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") List body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/NullableInt32ValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/NullableInt32ValuesImpl.java index 6d1f91703a9..caa02f86b04 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/NullableInt32ValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/NullableInt32ValuesImpl.java @@ -1,20 +1,23 @@ package type.array.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import java.util.List; /** @@ -48,38 +51,90 @@ public final class NullableInt32ValuesImpl { } /** - * The interface defining all the services for ArrayClientNullableInt32Values to be used by the proxy service to - * perform REST calls. + * The HTTP operations for ArrayClientNullableInt32Values. */ - @ServiceInterface(name = "ArrayClientNullableInt32Values", host = "{endpoint}") public interface NullableInt32ValuesService { static NullableInt32ValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.array.implementation.NullableInt32ValuesServiceImpl"); - return (NullableInt32ValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new NullableInt32ValuesServiceImpl(pipeline); + } + + Response> get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, List body, RequestContext requestContext); + } + + private static final class NullableInt32ValuesServiceImpl implements NullableInt32ValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(NullableInt32ValuesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private NullableInt32ValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response> get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/array/nullable-int32"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, + CoreUtils.createParameterizedType(List.class, Integer.class)), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, List body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/array/nullable-int32"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/array/nullable-int32", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response> get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/array/nullable-int32", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") List body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/NullableModelValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/NullableModelValuesImpl.java index ed2fdd47eb9..978b8554a10 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/NullableModelValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/NullableModelValuesImpl.java @@ -1,20 +1,23 @@ package type.array.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import java.util.List; import type.array.InnerModel; @@ -49,38 +52,90 @@ public final class NullableModelValuesImpl { } /** - * The interface defining all the services for ArrayClientNullableModelValues to be used by the proxy service to - * perform REST calls. + * The HTTP operations for ArrayClientNullableModelValues. */ - @ServiceInterface(name = "ArrayClientNullableModelValues", host = "{endpoint}") public interface NullableModelValuesService { static NullableModelValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.array.implementation.NullableModelValuesServiceImpl"); - return (NullableModelValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new NullableModelValuesServiceImpl(pipeline); + } + + Response> get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, List body, RequestContext requestContext); + } + + private static final class NullableModelValuesServiceImpl implements NullableModelValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(NullableModelValuesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private NullableModelValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response> get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/array/nullable-model"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, + CoreUtils.createParameterizedType(List.class, InnerModel.class)), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, List body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/array/nullable-model"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/array/nullable-model", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response> get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/array/nullable-model", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") List body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/NullableStringValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/NullableStringValuesImpl.java index b14d9896efc..1f1a40bf402 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/NullableStringValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/NullableStringValuesImpl.java @@ -1,20 +1,23 @@ package type.array.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import java.util.List; /** @@ -48,38 +51,90 @@ public final class NullableStringValuesImpl { } /** - * The interface defining all the services for ArrayClientNullableStringValues to be used by the proxy service to - * perform REST calls. + * The HTTP operations for ArrayClientNullableStringValues. */ - @ServiceInterface(name = "ArrayClientNullableStringValues", host = "{endpoint}") public interface NullableStringValuesService { static NullableStringValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.array.implementation.NullableStringValuesServiceImpl"); - return (NullableStringValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new NullableStringValuesServiceImpl(pipeline); + } + + Response> get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, List body, RequestContext requestContext); + } + + private static final class NullableStringValuesServiceImpl implements NullableStringValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(NullableStringValuesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private NullableStringValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response> get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/array/nullable-string"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, + CoreUtils.createParameterizedType(List.class, String.class)), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, List body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/array/nullable-string"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/array/nullable-string", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response> get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/array/nullable-string", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") List body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/StringValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/StringValuesImpl.java index c03c7d23420..e9437c9c06f 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/StringValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/StringValuesImpl.java @@ -1,20 +1,23 @@ package type.array.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import java.util.List; /** @@ -48,32 +51,90 @@ public final class StringValuesImpl { } /** - * The interface defining all the services for ArrayClientStringValues to be used by the proxy service to perform - * REST calls. + * The HTTP operations for ArrayClientStringValues. */ - @ServiceInterface(name = "ArrayClientStringValues", host = "{endpoint}") public interface StringValuesService { static StringValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.array.implementation.StringValuesServiceImpl"); - return (StringValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new StringValuesServiceImpl(pipeline); + } + + Response> get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, List body, RequestContext requestContext); + } + private static final class StringValuesServiceImpl implements StringValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(StringValuesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private StringValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation(method = HttpMethod.GET, path = "/type/array/string", expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response> get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); + @Override + public Response> get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/array/string"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, + CoreUtils.createParameterizedType(List.class, String.class)), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } - @HttpRequestInformation(method = HttpMethod.PUT, path = "/type/array/string", expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") List body, RequestContext requestContext); + @Override + public Response put(String endpoint, String contentType, List body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/array/string"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/UnknownValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/UnknownValuesImpl.java index 593dd375e72..db13ded4f72 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/UnknownValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/array/implementation/UnknownValuesImpl.java @@ -1,21 +1,23 @@ package type.array.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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 io.clientcore.core.instrumentation.logging.ClientLogger; import io.clientcore.core.models.binarydata.BinaryData; -import java.lang.reflect.InvocationTargetException; +import io.clientcore.core.serialization.SerializationFormat; +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.lang.reflect.ParameterizedType; import java.util.List; /** @@ -49,32 +51,91 @@ public final class UnknownValuesImpl { } /** - * The interface defining all the services for ArrayClientUnknownValues to be used by the proxy service to perform - * REST calls. + * The HTTP operations for ArrayClientUnknownValues. */ - @ServiceInterface(name = "ArrayClientUnknownValues", host = "{endpoint}") public interface UnknownValuesService { static UnknownValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.array.implementation.UnknownValuesServiceImpl"); - return (UnknownValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new UnknownValuesServiceImpl(pipeline); + } + + Response> get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, List body, RequestContext requestContext); + } + private static final class UnknownValuesServiceImpl implements UnknownValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(UnknownValuesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private UnknownValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation(method = HttpMethod.GET, path = "/type/array/unknown", expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response> get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); + @Override + public Response> get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/array/unknown"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response networkResponse = this.httpPipeline.send(httpRequest)) { + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, + CoreUtils.createParameterizedType(List.class, BinaryData.class)), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + } - @HttpRequestInformation(method = HttpMethod.PUT, path = "/type/array/unknown", expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") List body, RequestContext requestContext); + @Override + public Response put(String endpoint, String contentType, List body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/array/unknown"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/dictionary/implementation/BooleanValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/dictionary/implementation/BooleanValuesImpl.java index 65b58a60933..567b6b9d4ea 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/dictionary/implementation/BooleanValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/dictionary/implementation/BooleanValuesImpl.java @@ -1,20 +1,23 @@ package type.dictionary.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import java.util.Map; /** @@ -48,38 +51,91 @@ public final class BooleanValuesImpl { } /** - * The interface defining all the services for DictionaryClientBooleanValues to be used by the proxy service to - * perform REST calls. + * The HTTP operations for DictionaryClientBooleanValues. */ - @ServiceInterface(name = "DictionaryClientBooleanValues", host = "{endpoint}") public interface BooleanValuesService { static BooleanValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.dictionary.implementation.BooleanValuesServiceImpl"); - return (BooleanValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new BooleanValuesServiceImpl(pipeline); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/dictionary/boolean", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response> get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, + Response> get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, Map body, RequestContext requestContext); + } + + private static final class BooleanValuesServiceImpl implements BooleanValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(BooleanValuesService.class); + + private final HttpPipeline httpPipeline; - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/dictionary/boolean", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") Map body, RequestContext requestContext); + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private BooleanValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response> get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/dictionary/boolean"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, + CoreUtils.createParameterizedType(Map.class, String.class, Boolean.class)), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response put(String endpoint, String contentType, Map body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/dictionary/boolean"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/dictionary/implementation/DatetimeValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/dictionary/implementation/DatetimeValuesImpl.java index 83c7b6030d1..cec64b5fd83 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/dictionary/implementation/DatetimeValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/dictionary/implementation/DatetimeValuesImpl.java @@ -1,20 +1,23 @@ package type.dictionary.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import java.time.OffsetDateTime; import java.util.Map; @@ -49,39 +52,92 @@ public final class DatetimeValuesImpl { } /** - * The interface defining all the services for DictionaryClientDatetimeValues to be used by the proxy service to - * perform REST calls. + * The HTTP operations for DictionaryClientDatetimeValues. */ - @ServiceInterface(name = "DictionaryClientDatetimeValues", host = "{endpoint}") public interface DatetimeValuesService { static DatetimeValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.dictionary.implementation.DatetimeValuesServiceImpl"); - return (DatetimeValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new DatetimeValuesServiceImpl(pipeline); + } + + Response> get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, Map body, + RequestContext requestContext); + } + + private static final class DatetimeValuesServiceImpl implements DatetimeValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(DatetimeValuesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private DatetimeValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response> get(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/dictionary/datetime"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, + CoreUtils.createParameterizedType(Map.class, String.class, OffsetDateTime.class)), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, Map body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/dictionary/datetime"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/dictionary/datetime", - expectedStatusCodes = { 200 }, - returnValueWireType = OffsetDateTime.class) - @UnexpectedResponseExceptionDetail - Response> get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/dictionary/datetime", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") Map body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/dictionary/implementation/DurationValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/dictionary/implementation/DurationValuesImpl.java index cd05e592c61..995f06c9cdc 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/dictionary/implementation/DurationValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/dictionary/implementation/DurationValuesImpl.java @@ -1,20 +1,23 @@ package type.dictionary.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import java.time.Duration; import java.util.Map; @@ -49,38 +52,91 @@ public final class DurationValuesImpl { } /** - * The interface defining all the services for DictionaryClientDurationValues to be used by the proxy service to - * perform REST calls. + * The HTTP operations for DictionaryClientDurationValues. */ - @ServiceInterface(name = "DictionaryClientDurationValues", host = "{endpoint}") public interface DurationValuesService { static DurationValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.dictionary.implementation.DurationValuesServiceImpl"); - return (DurationValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new DurationValuesServiceImpl(pipeline); + } + + Response> get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, Map body, + RequestContext requestContext); + } + + private static final class DurationValuesServiceImpl implements DurationValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(DurationValuesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private DurationValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response> get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/dictionary/duration"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, + CoreUtils.createParameterizedType(Map.class, String.class, Duration.class)), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, Map body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/dictionary/duration"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/dictionary/duration", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response> get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/dictionary/duration", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") Map body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/dictionary/implementation/Float32ValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/dictionary/implementation/Float32ValuesImpl.java index 22793163892..e9b2142886d 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/dictionary/implementation/Float32ValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/dictionary/implementation/Float32ValuesImpl.java @@ -1,20 +1,23 @@ package type.dictionary.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import java.util.Map; /** @@ -48,38 +51,91 @@ public final class Float32ValuesImpl { } /** - * The interface defining all the services for DictionaryClientFloat32Values to be used by the proxy service to - * perform REST calls. + * The HTTP operations for DictionaryClientFloat32Values. */ - @ServiceInterface(name = "DictionaryClientFloat32Values", host = "{endpoint}") public interface Float32ValuesService { static Float32ValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.dictionary.implementation.Float32ValuesServiceImpl"); - return (Float32ValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new Float32ValuesServiceImpl(pipeline); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/dictionary/float32", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response> get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, + Response> get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, Map body, RequestContext requestContext); + } + + private static final class Float32ValuesServiceImpl implements Float32ValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(Float32ValuesService.class); + + private final HttpPipeline httpPipeline; - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/dictionary/float32", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") Map body, RequestContext requestContext); + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private Float32ValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response> get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/dictionary/float32"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, + CoreUtils.createParameterizedType(Map.class, String.class, Double.class)), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response put(String endpoint, String contentType, Map body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/dictionary/float32"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/dictionary/implementation/Int32ValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/dictionary/implementation/Int32ValuesImpl.java index e01933fb9fe..07c629b85e6 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/dictionary/implementation/Int32ValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/dictionary/implementation/Int32ValuesImpl.java @@ -1,20 +1,23 @@ package type.dictionary.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import java.util.Map; /** @@ -48,32 +51,91 @@ public final class Int32ValuesImpl { } /** - * The interface defining all the services for DictionaryClientInt32Values to be used by the proxy service to - * perform REST calls. + * The HTTP operations for DictionaryClientInt32Values. */ - @ServiceInterface(name = "DictionaryClientInt32Values", host = "{endpoint}") public interface Int32ValuesService { static Int32ValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.dictionary.implementation.Int32ValuesServiceImpl"); - return (Int32ValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new Int32ValuesServiceImpl(pipeline); } - @HttpRequestInformation(method = HttpMethod.GET, path = "/type/dictionary/int32", expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response> get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, + Response> get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, Map body, RequestContext requestContext); + } + + private static final class Int32ValuesServiceImpl implements Int32ValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(Int32ValuesService.class); + + private final HttpPipeline httpPipeline; - @HttpRequestInformation(method = HttpMethod.PUT, path = "/type/dictionary/int32", expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") Map body, RequestContext requestContext); + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private Int32ValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response> get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/dictionary/int32"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, + CoreUtils.createParameterizedType(Map.class, String.class, Integer.class)), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response put(String endpoint, String contentType, Map body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/dictionary/int32"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/dictionary/implementation/Int64ValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/dictionary/implementation/Int64ValuesImpl.java index 3284671ef3b..725a2089148 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/dictionary/implementation/Int64ValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/dictionary/implementation/Int64ValuesImpl.java @@ -1,20 +1,23 @@ package type.dictionary.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import java.util.Map; /** @@ -48,32 +51,90 @@ public final class Int64ValuesImpl { } /** - * The interface defining all the services for DictionaryClientInt64Values to be used by the proxy service to - * perform REST calls. + * The HTTP operations for DictionaryClientInt64Values. */ - @ServiceInterface(name = "DictionaryClientInt64Values", host = "{endpoint}") public interface Int64ValuesService { static Int64ValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.dictionary.implementation.Int64ValuesServiceImpl"); - return (Int64ValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new Int64ValuesServiceImpl(pipeline); + } + + Response> get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, Map body, RequestContext requestContext); + } + private static final class Int64ValuesServiceImpl implements Int64ValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(Int64ValuesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private Int64ValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation(method = HttpMethod.GET, path = "/type/dictionary/int64", expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response> get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); + @Override + public Response> get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/dictionary/int64"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, + CoreUtils.createParameterizedType(Map.class, String.class, Long.class)), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } - @HttpRequestInformation(method = HttpMethod.PUT, path = "/type/dictionary/int64", expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") Map body, RequestContext requestContext); + @Override + public Response put(String endpoint, String contentType, Map body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/dictionary/int64"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/dictionary/implementation/ModelValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/dictionary/implementation/ModelValuesImpl.java index a9eac71fb4b..23dcb76d0e6 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/dictionary/implementation/ModelValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/dictionary/implementation/ModelValuesImpl.java @@ -1,20 +1,23 @@ package type.dictionary.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import java.util.Map; import type.dictionary.InnerModel; @@ -49,32 +52,91 @@ public final class ModelValuesImpl { } /** - * The interface defining all the services for DictionaryClientModelValues to be used by the proxy service to - * perform REST calls. + * The HTTP operations for DictionaryClientModelValues. */ - @ServiceInterface(name = "DictionaryClientModelValues", host = "{endpoint}") public interface ModelValuesService { static ModelValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.dictionary.implementation.ModelValuesServiceImpl"); - return (ModelValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new ModelValuesServiceImpl(pipeline); + } + + Response> get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, Map body, + RequestContext requestContext); + } + private static final class ModelValuesServiceImpl implements ModelValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ModelValuesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ModelValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation(method = HttpMethod.GET, path = "/type/dictionary/model", expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response> get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); + @Override + public Response> get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/dictionary/model"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, + CoreUtils.createParameterizedType(Map.class, String.class, InnerModel.class)), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } - @HttpRequestInformation(method = HttpMethod.PUT, path = "/type/dictionary/model", expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") Map body, RequestContext requestContext); + @Override + public Response put(String endpoint, String contentType, Map body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/dictionary/model"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/dictionary/implementation/NullableFloatValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/dictionary/implementation/NullableFloatValuesImpl.java index f5000779cf2..2b27f84684a 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/dictionary/implementation/NullableFloatValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/dictionary/implementation/NullableFloatValuesImpl.java @@ -1,20 +1,23 @@ package type.dictionary.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import java.util.Map; /** @@ -48,39 +51,91 @@ public final class NullableFloatValuesImpl { } /** - * The interface defining all the services for DictionaryClientNullableFloatValues to be used by the proxy service - * to perform REST calls. + * The HTTP operations for DictionaryClientNullableFloatValues. */ - @ServiceInterface(name = "DictionaryClientNullableFloatValues", host = "{endpoint}") public interface NullableFloatValuesService { static NullableFloatValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.dictionary.implementation.NullableFloatValuesServiceImpl"); - return (NullableFloatValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new NullableFloatValuesServiceImpl(pipeline); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/dictionary/nullable-float", - expectedStatusCodes = { 200 }, - returnValueWireType = Double.class) - @UnexpectedResponseExceptionDetail - Response> get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, + Response> get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, Map body, RequestContext requestContext); + } + + private static final class NullableFloatValuesServiceImpl implements NullableFloatValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(NullableFloatValuesService.class); + + private final HttpPipeline httpPipeline; - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/dictionary/nullable-float", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") Map body, RequestContext requestContext); + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private NullableFloatValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response> get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/dictionary/nullable-float"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, + CoreUtils.createParameterizedType(Map.class, String.class, Double.class)), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response put(String endpoint, String contentType, Map body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/dictionary/nullable-float"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/dictionary/implementation/RecursiveModelValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/dictionary/implementation/RecursiveModelValuesImpl.java index bbfba057d43..b37eef70119 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/dictionary/implementation/RecursiveModelValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/dictionary/implementation/RecursiveModelValuesImpl.java @@ -1,20 +1,23 @@ package type.dictionary.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import java.util.Map; import type.dictionary.InnerModel; @@ -49,38 +52,91 @@ public final class RecursiveModelValuesImpl { } /** - * The interface defining all the services for DictionaryClientRecursiveModelValues to be used by the proxy service - * to perform REST calls. + * The HTTP operations for DictionaryClientRecursiveModelValues. */ - @ServiceInterface(name = "DictionaryClientRecursiveModelValues", host = "{endpoint}") public interface RecursiveModelValuesService { static RecursiveModelValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.dictionary.implementation.RecursiveModelValuesServiceImpl"); - return (RecursiveModelValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new RecursiveModelValuesServiceImpl(pipeline); + } + + Response> get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, Map body, + RequestContext requestContext); + } + + private static final class RecursiveModelValuesServiceImpl implements RecursiveModelValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(RecursiveModelValuesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private RecursiveModelValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response> get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/dictionary/model/recursive"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, + CoreUtils.createParameterizedType(Map.class, String.class, InnerModel.class)), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, Map body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/dictionary/model/recursive"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/dictionary/model/recursive", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response> get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/dictionary/model/recursive", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") Map body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/dictionary/implementation/StringValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/dictionary/implementation/StringValuesImpl.java index 4a325decf70..c9a130a48d3 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/dictionary/implementation/StringValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/dictionary/implementation/StringValuesImpl.java @@ -1,20 +1,23 @@ package type.dictionary.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import java.util.Map; /** @@ -48,38 +51,91 @@ public final class StringValuesImpl { } /** - * The interface defining all the services for DictionaryClientStringValues to be used by the proxy service to - * perform REST calls. + * The HTTP operations for DictionaryClientStringValues. */ - @ServiceInterface(name = "DictionaryClientStringValues", host = "{endpoint}") public interface StringValuesService { static StringValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.dictionary.implementation.StringValuesServiceImpl"); - return (StringValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new StringValuesServiceImpl(pipeline); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/dictionary/string", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response> get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, + Response> get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, Map body, RequestContext requestContext); + } + + private static final class StringValuesServiceImpl implements StringValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(StringValuesService.class); + + private final HttpPipeline httpPipeline; - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/dictionary/string", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") Map body, RequestContext requestContext); + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private StringValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response> get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/dictionary/string"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, + CoreUtils.createParameterizedType(Map.class, String.class, String.class)), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response put(String endpoint, String contentType, Map body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/dictionary/string"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/dictionary/implementation/UnknownValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/dictionary/implementation/UnknownValuesImpl.java index d71e13b2985..ac2527834ed 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/dictionary/implementation/UnknownValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/dictionary/implementation/UnknownValuesImpl.java @@ -1,21 +1,23 @@ package type.dictionary.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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 io.clientcore.core.instrumentation.logging.ClientLogger; import io.clientcore.core.models.binarydata.BinaryData; -import java.lang.reflect.InvocationTargetException; +import io.clientcore.core.serialization.SerializationFormat; +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.lang.reflect.ParameterizedType; import java.util.Map; /** @@ -49,38 +51,91 @@ public final class UnknownValuesImpl { } /** - * The interface defining all the services for DictionaryClientUnknownValues to be used by the proxy service to - * perform REST calls. + * The HTTP operations for DictionaryClientUnknownValues. */ - @ServiceInterface(name = "DictionaryClientUnknownValues", host = "{endpoint}") public interface UnknownValuesService { static UnknownValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.dictionary.implementation.UnknownValuesServiceImpl"); - return (UnknownValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new UnknownValuesServiceImpl(pipeline); + } + + Response> get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, Map body, + RequestContext requestContext); + } + + private static final class UnknownValuesServiceImpl implements UnknownValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(UnknownValuesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private UnknownValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response> get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/dictionary/unknown"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, + CoreUtils.createParameterizedType(Map.class, String.class, BinaryData.class)), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, Map body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/dictionary/unknown"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/dictionary/unknown", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response> get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/dictionary/unknown", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") Map body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/enumnamespace/extensible/implementation/StringOperationsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/enumnamespace/extensible/implementation/StringOperationsImpl.java index 3c61370fad2..873e9badf85 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/enumnamespace/extensible/implementation/StringOperationsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/enumnamespace/extensible/implementation/StringOperationsImpl.java @@ -1,20 +1,23 @@ package type.enumnamespace.extensible.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.enumnamespace.extensible.DaysOfWeekExtensibleEnum; /** @@ -48,57 +51,145 @@ public final class StringOperationsImpl { } /** - * The interface defining all the services for ExtensibleClientStringOperations to be used by the proxy service to - * perform REST calls. + * The HTTP operations for ExtensibleClientStringOperations. */ - @ServiceInterface(name = "ExtensibleClientStringOperations", host = "{endpoint}") public interface StringOperationsService { static StringOperationsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz - = Class.forName("type.enumnamespace.extensible.implementation.StringOperationsServiceImpl"); - return (StringOperationsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new StringOperationsServiceImpl(pipeline); + } + + Response getKnownValue(String endpoint, String accept, RequestContext requestContext); + + Response getUnknownValue(String endpoint, String accept, + RequestContext requestContext); + + Response putKnownValue(String endpoint, String contentType, DaysOfWeekExtensibleEnum body, + RequestContext requestContext); + + Response putUnknownValue(String endpoint, String contentType, DaysOfWeekExtensibleEnum body, + RequestContext requestContext); + } + + private static final class StringOperationsServiceImpl implements StringOperationsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(StringOperationsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private StringOperationsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response getKnownValue(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/enum/extensible/string/known-value"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, DaysOfWeekExtensibleEnum.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response getUnknownValue(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/enum/extensible/string/unknown-value"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, DaysOfWeekExtensibleEnum.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response putKnownValue(String endpoint, String contentType, DaysOfWeekExtensibleEnum body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/enum/extensible/string/known-value"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 putUnknownValue(String endpoint, String contentType, DaysOfWeekExtensibleEnum body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/enum/extensible/string/unknown-value"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/enum/extensible/string/known-value", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getKnownValue(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/enum/extensible/string/unknown-value", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getUnknownValue(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/enum/extensible/string/known-value", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putKnownValue(@HostParam("endpoint") String endpoint, - @HeaderParam("content-type") String contentType, - @BodyParam("application/json") DaysOfWeekExtensibleEnum body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/enum/extensible/string/unknown-value", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putUnknownValue(@HostParam("endpoint") String endpoint, - @HeaderParam("content-type") String contentType, - @BodyParam("application/json") DaysOfWeekExtensibleEnum body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/enumnamespace/fixed/implementation/StringOperationsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/enumnamespace/fixed/implementation/StringOperationsImpl.java index 4c0dc731ffc..57ad9e98168 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/enumnamespace/fixed/implementation/StringOperationsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/enumnamespace/fixed/implementation/StringOperationsImpl.java @@ -1,20 +1,23 @@ package type.enumnamespace.fixed.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.enumnamespace.fixed.DaysOfWeekEnum; /** @@ -48,50 +51,120 @@ public final class StringOperationsImpl { } /** - * The interface defining all the services for FixedClientStringOperations to be used by the proxy service to - * perform REST calls. + * The HTTP operations for FixedClientStringOperations. */ - @ServiceInterface(name = "FixedClientStringOperations", host = "{endpoint}") public interface StringOperationsService { static StringOperationsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.enumnamespace.fixed.implementation.StringOperationsServiceImpl"); - return (StringOperationsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new StringOperationsServiceImpl(pipeline); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/enum/fixed/string/known-value", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getKnownValue(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/enum/fixed/string/known-value", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putKnownValue(@HostParam("endpoint") String endpoint, - @HeaderParam("content-type") String contentType, @BodyParam("application/json") DaysOfWeekEnum body, + Response getKnownValue(String endpoint, String accept, RequestContext requestContext); + + Response putKnownValue(String endpoint, String contentType, DaysOfWeekEnum body, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/enum/fixed/string/unknown-value", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putUnknownValue(@HostParam("endpoint") String endpoint, - @HeaderParam("content-type") String contentType, @BodyParam("application/json") DaysOfWeekEnum body, + Response putUnknownValue(String endpoint, String contentType, DaysOfWeekEnum body, RequestContext requestContext); } + private static final class StringOperationsServiceImpl implements StringOperationsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(StringOperationsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private StringOperationsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response getKnownValue(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/enum/fixed/string/known-value"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, DaysOfWeekEnum.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response putKnownValue(String endpoint, String contentType, DaysOfWeekEnum body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/enum/fixed/string/known-value"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 putUnknownValue(String endpoint, String contentType, DaysOfWeekEnum body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/enum/fixed/string/unknown-value"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } + } + /** * getKnownValue. * diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/file/implementation/BodiesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/file/implementation/BodiesImpl.java index 4b2c758155a..d1fa96e4611 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/file/implementation/BodiesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/file/implementation/BodiesImpl.java @@ -1,21 +1,22 @@ package type.file.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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 io.clientcore.core.instrumentation.logging.ClientLogger; import io.clientcore.core.models.binarydata.BinaryData; -import java.lang.reflect.InvocationTargetException; +import io.clientcore.core.serialization.SerializationFormat; +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 type.file.body.UploadFileMultipleContentTypesContentType; /** @@ -49,90 +50,233 @@ public final class BodiesImpl { } /** - * The interface defining all the services for FileClientBodies to be used by the proxy service to perform REST - * calls. + * The HTTP operations for FileClientBodies. */ - @ServiceInterface(name = "FileClientBodies", host = "{endpoint}") public interface BodiesService { static BodiesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.file.implementation.BodiesServiceImpl"); - return (BodiesService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new BodiesServiceImpl(pipeline); + } + + Response uploadFileSpecificContentType(String endpoint, String contentType, BinaryData file, + long contentLength, RequestContext requestContext); + + Response uploadFileJsonContentType(String endpoint, String contentType, BinaryData file, + long contentLength, RequestContext requestContext); + + Response downloadFileJsonContentType(String endpoint, String accept, RequestContext requestContext); + + Response downloadFileSpecificContentType(String endpoint, String accept, + RequestContext requestContext); + + Response uploadFileMultipleContentTypes(String endpoint, + UploadFileMultipleContentTypesContentType contentType, BinaryData file, long contentLength, + RequestContext requestContext); + + Response downloadFileMultipleContentTypes(String endpoint, String accept, + RequestContext requestContext); + + Response uploadFileDefaultContentType(String endpoint, String contentType, BinaryData file, + long contentLength, RequestContext requestContext); + + Response downloadFileDefaultContentType(String endpoint, String accept, + RequestContext requestContext); + } + + private static final class BodiesServiceImpl implements BodiesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(BodiesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private BodiesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response uploadFileSpecificContentType(String endpoint, String contentType, BinaryData file, + long contentLength, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/type/file/body/request/specific-content-type"); + httpRequest.getHeaders().add(HttpHeaderName.fromString("Content-Length"), String.valueOf(contentLength)); + if (file != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + if (file.getLength() != null) { + httpRequest.setBody(file); + } + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 uploadFileJsonContentType(String endpoint, String contentType, BinaryData file, + long contentLength, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/type/file/body/request/json-content-type"); + httpRequest.getHeaders().add(HttpHeaderName.fromString("Content-Length"), String.valueOf(contentLength)); + if (file != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(file, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 downloadFileJsonContentType(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/file/body/response/json-content-type"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return networkResponse; } - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/type/file/body/request/specific-content-type", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response uploadFileSpecificContentType(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("image/png") BinaryData file, - @HeaderParam("Content-Length") long contentLength, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/type/file/body/request/json-content-type", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response uploadFileJsonContentType(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") BinaryData file, - @HeaderParam("Content-Length") long contentLength, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/file/body/response/json-content-type", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response downloadFileJsonContentType(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/file/body/response/specific-content-type", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response downloadFileSpecificContentType(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/type/file/body/request/multiple-content-types", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response uploadFileMultipleContentTypes(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") UploadFileMultipleContentTypesContentType contentType, - @BodyParam("application/json") BinaryData file, @HeaderParam("Content-Length") long contentLength, - RequestContext requestContext); + @Override + public Response downloadFileSpecificContentType(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/file/body/response/specific-content-type"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return networkResponse; + } + + @Override + public Response uploadFileMultipleContentTypes(String endpoint, + UploadFileMultipleContentTypesContentType contentType, BinaryData file, long contentLength, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/type/file/body/request/multiple-content-types"); + httpRequest.getHeaders().add(HttpHeaderName.fromString("Content-Length"), String.valueOf(contentLength)); + if (file != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, String.valueOf(contentType)); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(file, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 downloadFileMultipleContentTypes(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/file/body/response/multiple-content-types"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return networkResponse; + } + + @Override + public Response uploadFileDefaultContentType(String endpoint, String contentType, BinaryData file, + long contentLength, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/type/file/body/request/default-content-type"); + httpRequest.getHeaders().add(HttpHeaderName.fromString("Content-Length"), String.valueOf(contentLength)); + if (file != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + if (file.getLength() != null) { + httpRequest.setBody(file); + } + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/file/body/response/multiple-content-types", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response downloadFileMultipleContentTypes(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/type/file/body/request/default-content-type", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response uploadFileDefaultContentType(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("*/*") BinaryData file, - @HeaderParam("Content-Length") long contentLength, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/file/body/response/default-content-type", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response downloadFileDefaultContentType(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); + @Override + public Response downloadFileDefaultContentType(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/file/body/response/default-content-type"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return networkResponse; + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/model/empty/implementation/EmptyClientImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/model/empty/implementation/EmptyClientImpl.java index 24ad1051552..eb7749133e2 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/model/empty/implementation/EmptyClientImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/model/empty/implementation/EmptyClientImpl.java @@ -1,20 +1,23 @@ package type.model.empty.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.model.empty.EmptyInput; import type.model.empty.EmptyInputOutput; import type.model.empty.EmptyOutput; @@ -85,46 +88,122 @@ public EmptyClientImpl(HttpPipeline httpPipeline, Instrumentation instrumentatio } /** - * The interface defining all the services for EmptyClient to be used by the proxy service to perform REST calls. + * The HTTP operations for EmptyClient. */ - @ServiceInterface(name = "EmptyClient", host = "{endpoint}") public interface EmptyClientService { static EmptyClientService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.model.empty.implementation.EmptyClientServiceImpl"); - return (EmptyClientService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new EmptyClientServiceImpl(pipeline); + } + + Response putEmpty(String endpoint, String contentType, EmptyInput input, RequestContext requestContext); + + Response getEmpty(String endpoint, String accept, RequestContext requestContext); + + Response postRoundTripEmpty(String endpoint, String contentType, String accept, + EmptyInputOutput body, RequestContext requestContext); + } + + private static final class EmptyClientServiceImpl implements EmptyClientService { + private static final ClientLogger HTTP_LOG = new ClientLogger(EmptyClientService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private EmptyClientServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response putEmpty(String endpoint, String contentType, EmptyInput input, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/model/empty/alone"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 getEmpty(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/model/empty/alone"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, EmptyOutput.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response postRoundTripEmpty(String endpoint, String contentType, String accept, + EmptyInputOutput body, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/type/model/empty/round-trip"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, EmptyInputOutput.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); } - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/model/empty/alone", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putEmpty(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") EmptyInput input, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/model/empty/alone", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getEmpty(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/type/model/empty/round-trip", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response postRoundTripEmpty(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") EmptyInputOutput body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/model/inheritance/enumdiscriminator/implementation/EnumDiscriminatorClientImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/model/inheritance/enumdiscriminator/implementation/EnumDiscriminatorClientImpl.java index c0045b59a88..27c45a486df 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/model/inheritance/enumdiscriminator/implementation/EnumDiscriminatorClientImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/model/inheritance/enumdiscriminator/implementation/EnumDiscriminatorClientImpl.java @@ -1,20 +1,23 @@ package type.model.inheritance.enumdiscriminator.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.model.inheritance.enumdiscriminator.Dog; import type.model.inheritance.enumdiscriminator.Snake; @@ -84,89 +87,244 @@ public EnumDiscriminatorClientImpl(HttpPipeline httpPipeline, Instrumentation in } /** - * The interface defining all the services for EnumDiscriminatorClient to be used by the proxy service to perform - * REST calls. + * The HTTP operations for EnumDiscriminatorClient. */ - @ServiceInterface(name = "EnumDiscriminatorClient", host = "{endpoint}") public interface EnumDiscriminatorClientService { static EnumDiscriminatorClientService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName( - "type.model.inheritance.enumdiscriminator.implementation.EnumDiscriminatorClientServiceImpl"); - return (EnumDiscriminatorClientService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new EnumDiscriminatorClientServiceImpl(pipeline); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/model/inheritance/enum-discriminator/extensible-enum", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getExtensibleModel(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, + Response getExtensibleModel(String endpoint, String accept, RequestContext requestContext); + + Response putExtensibleModel(String endpoint, String contentType, Dog input, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/model/inheritance/enum-discriminator/extensible-enum", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putExtensibleModel(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") Dog input, + Response getExtensibleModelMissingDiscriminator(String endpoint, String accept, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/model/inheritance/enum-discriminator/extensible-enum/missingdiscriminator", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getExtensibleModelMissingDiscriminator(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/model/inheritance/enum-discriminator/extensible-enum/wrongdiscriminator", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getExtensibleModelWrongDiscriminator(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/model/inheritance/enum-discriminator/fixed-enum", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getFixedModel(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, + Response getExtensibleModelWrongDiscriminator(String endpoint, String accept, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/model/inheritance/enum-discriminator/fixed-enum", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putFixedModel(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") Snake input, + Response getFixedModel(String endpoint, String accept, RequestContext requestContext); + + Response putFixedModel(String endpoint, String contentType, Snake input, RequestContext requestContext); + + Response getFixedModelMissingDiscriminator(String endpoint, String accept, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/model/inheritance/enum-discriminator/fixed-enum/missingdiscriminator", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getFixedModelMissingDiscriminator(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/model/inheritance/enum-discriminator/fixed-enum/wrongdiscriminator", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getFixedModelWrongDiscriminator(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); + Response getFixedModelWrongDiscriminator(String endpoint, String accept, RequestContext requestContext); + } + + private static final class EnumDiscriminatorClientServiceImpl implements EnumDiscriminatorClientService { + private static final ClientLogger HTTP_LOG = new ClientLogger(EnumDiscriminatorClientService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private EnumDiscriminatorClientServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response getExtensibleModel(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/model/inheritance/enum-discriminator/extensible-enum"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, Dog.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response putExtensibleModel(String endpoint, String contentType, Dog input, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/model/inheritance/enum-discriminator/extensible-enum"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 getExtensibleModelMissingDiscriminator(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/model/inheritance/enum-discriminator/extensible-enum/missingdiscriminator"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, Dog.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response getExtensibleModelWrongDiscriminator(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/model/inheritance/enum-discriminator/extensible-enum/wrongdiscriminator"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, Dog.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response getFixedModel(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/model/inheritance/enum-discriminator/fixed-enum"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, Snake.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response putFixedModel(String endpoint, String contentType, Snake input, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/model/inheritance/enum-discriminator/fixed-enum"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 getFixedModelMissingDiscriminator(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/model/inheritance/enum-discriminator/fixed-enum/missingdiscriminator"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, Snake.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response getFixedModelWrongDiscriminator(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/model/inheritance/enum-discriminator/fixed-enum/wrongdiscriminator"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, Snake.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/model/inheritance/nesteddiscriminator/implementation/NestedDiscriminatorClientImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/model/inheritance/nesteddiscriminator/implementation/NestedDiscriminatorClientImpl.java index ce7e1b97c03..f4e89c4094b 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/model/inheritance/nesteddiscriminator/implementation/NestedDiscriminatorClientImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/model/inheritance/nesteddiscriminator/implementation/NestedDiscriminatorClientImpl.java @@ -1,20 +1,23 @@ package type.model.inheritance.nesteddiscriminator.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.model.inheritance.nesteddiscriminator.Fish; /** @@ -83,72 +86,188 @@ public NestedDiscriminatorClientImpl(HttpPipeline httpPipeline, Instrumentation } /** - * The interface defining all the services for NestedDiscriminatorClient to be used by the proxy service to perform - * REST calls. + * The HTTP operations for NestedDiscriminatorClient. */ - @ServiceInterface(name = "NestedDiscriminatorClient", host = "{endpoint}") public interface NestedDiscriminatorClientService { static NestedDiscriminatorClientService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName( - "type.model.inheritance.nesteddiscriminator.implementation.NestedDiscriminatorClientServiceImpl"); - return (NestedDiscriminatorClientService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new NestedDiscriminatorClientServiceImpl(pipeline); + } + + Response getModel(String endpoint, String accept, RequestContext requestContext); + + Response putModel(String endpoint, String contentType, Fish input, RequestContext requestContext); + + Response getRecursiveModel(String endpoint, String accept, RequestContext requestContext); + + Response putRecursiveModel(String endpoint, String contentType, Fish input, + RequestContext requestContext); + + Response getMissingDiscriminator(String endpoint, String accept, RequestContext requestContext); + + Response getWrongDiscriminator(String endpoint, String accept, RequestContext requestContext); + } + + private static final class NestedDiscriminatorClientServiceImpl implements NestedDiscriminatorClientService { + private static final ClientLogger HTTP_LOG = new ClientLogger(NestedDiscriminatorClientService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private NestedDiscriminatorClientServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response getModel(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/model/inheritance/nested-discriminator/model"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, Fish.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response putModel(String endpoint, String contentType, Fish input, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/model/inheritance/nested-discriminator/model"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/model/inheritance/nested-discriminator/model", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getModel(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); + @Override + public Response getRecursiveModel(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/model/inheritance/nested-discriminator/recursivemodel"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, Fish.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/model/inheritance/nested-discriminator/model", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putModel(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") Fish input, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/model/inheritance/nested-discriminator/recursivemodel", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getRecursiveModel(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); + @Override + public Response putRecursiveModel(String endpoint, String contentType, Fish input, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/model/inheritance/nested-discriminator/recursivemodel"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/model/inheritance/nested-discriminator/recursivemodel", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putRecursiveModel(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") Fish input, - RequestContext requestContext); + @Override + public Response getMissingDiscriminator(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/model/inheritance/nested-discriminator/missingdiscriminator"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, Fish.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/model/inheritance/nested-discriminator/missingdiscriminator", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getMissingDiscriminator(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/model/inheritance/nested-discriminator/wrongdiscriminator", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getWrongDiscriminator(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); + @Override + public Response getWrongDiscriminator(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/model/inheritance/nested-discriminator/wrongdiscriminator"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, Fish.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/model/inheritance/notdiscriminated/implementation/NotDiscriminatedClientImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/model/inheritance/notdiscriminated/implementation/NotDiscriminatedClientImpl.java index 3627913b4f3..38c5205664c 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/model/inheritance/notdiscriminated/implementation/NotDiscriminatedClientImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/model/inheritance/notdiscriminated/implementation/NotDiscriminatedClientImpl.java @@ -1,20 +1,23 @@ package type.model.inheritance.notdiscriminated.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.model.inheritance.notdiscriminated.Siamese; /** @@ -83,49 +86,122 @@ public NotDiscriminatedClientImpl(HttpPipeline httpPipeline, Instrumentation ins } /** - * The interface defining all the services for NotDiscriminatedClient to be used by the proxy service to perform - * REST calls. + * The HTTP operations for NotDiscriminatedClient. */ - @ServiceInterface(name = "NotDiscriminatedClient", host = "{endpoint}") public interface NotDiscriminatedClientService { static NotDiscriminatedClientService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName( - "type.model.inheritance.notdiscriminated.implementation.NotDiscriminatedClientServiceImpl"); - return (NotDiscriminatedClientService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new NotDiscriminatedClientServiceImpl(pipeline); } - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/type/model/inheritance/not-discriminated/valid", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response postValid(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") Siamese input, - RequestContext requestContext); + Response postValid(String endpoint, String contentType, Siamese input, RequestContext requestContext); + + Response getValid(String endpoint, String accept, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/model/inheritance/not-discriminated/valid", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getValid(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, + Response putValid(String endpoint, String contentType, String accept, Siamese input, RequestContext requestContext); + } + + private static final class NotDiscriminatedClientServiceImpl implements NotDiscriminatedClientService { + private static final ClientLogger HTTP_LOG = new ClientLogger(NotDiscriminatedClientService.class); + + private final HttpPipeline httpPipeline; - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/model/inheritance/not-discriminated/valid", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response putValid(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") Siamese input, RequestContext requestContext); + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private NotDiscriminatedClientServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response postValid(String endpoint, String contentType, Siamese input, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/type/model/inheritance/not-discriminated/valid"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 getValid(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/model/inheritance/not-discriminated/valid"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, Siamese.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response putValid(String endpoint, String contentType, String accept, Siamese input, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/model/inheritance/not-discriminated/valid"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, Siamese.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/model/inheritance/recursive/implementation/RecursiveClientImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/model/inheritance/recursive/implementation/RecursiveClientImpl.java index fd84ebd7087..12cc360dde1 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/model/inheritance/recursive/implementation/RecursiveClientImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/model/inheritance/recursive/implementation/RecursiveClientImpl.java @@ -1,20 +1,23 @@ package type.model.inheritance.recursive.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.model.inheritance.recursive.Extension; /** @@ -83,39 +86,88 @@ public RecursiveClientImpl(HttpPipeline httpPipeline, Instrumentation instrument } /** - * The interface defining all the services for RecursiveClient to be used by the proxy service to perform REST - * calls. + * The HTTP operations for RecursiveClient. */ - @ServiceInterface(name = "RecursiveClient", host = "{endpoint}") public interface RecursiveClientService { static RecursiveClientService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz - = Class.forName("type.model.inheritance.recursive.implementation.RecursiveClientServiceImpl"); - return (RecursiveClientService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new RecursiveClientServiceImpl(pipeline); + } + + Response put(String endpoint, String contentType, Extension input, RequestContext requestContext); + + Response get(String endpoint, String accept, RequestContext requestContext); + } + + private static final class RecursiveClientServiceImpl implements RecursiveClientService { + private static final ClientLogger HTTP_LOG = new ClientLogger(RecursiveClientService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private RecursiveClientServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response put(String endpoint, String contentType, Extension input, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/model/inheritance/recursive"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/model/inheritance/recursive"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, Extension.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); } - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/model/inheritance/recursive", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") Extension input, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/model/inheritance/recursive", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/model/inheritance/singlediscriminator/implementation/SingleDiscriminatorClientImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/model/inheritance/singlediscriminator/implementation/SingleDiscriminatorClientImpl.java index e83deee5158..f8abbeee543 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/model/inheritance/singlediscriminator/implementation/SingleDiscriminatorClientImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/model/inheritance/singlediscriminator/implementation/SingleDiscriminatorClientImpl.java @@ -1,20 +1,23 @@ package type.model.inheritance.singlediscriminator.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.model.inheritance.singlediscriminator.Bird; import type.model.inheritance.singlediscriminator.Dinosaur; import type.model.inheritance.singlediscriminator.Fish; @@ -85,99 +88,266 @@ public SingleDiscriminatorClientImpl(HttpPipeline httpPipeline, Instrumentation } /** - * The interface defining all the services for SingleDiscriminatorClient to be used by the proxy service to perform - * REST calls. + * The HTTP operations for SingleDiscriminatorClient. */ - @ServiceInterface(name = "SingleDiscriminatorClient", host = "{endpoint}") public interface SingleDiscriminatorClientService { static SingleDiscriminatorClientService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName( - "type.model.inheritance.singlediscriminator.implementation.SingleDiscriminatorClientServiceImpl"); - return (SingleDiscriminatorClientService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new SingleDiscriminatorClientServiceImpl(pipeline); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/model/inheritance/single-discriminator/model", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getModel(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); + Response getModel(String endpoint, String accept, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/model/inheritance/single-discriminator/model", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putModel(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") Bird input, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/model/inheritance/single-discriminator/recursivemodel", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getRecursiveModel(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); + Response putModel(String endpoint, String contentType, Bird input, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/model/inheritance/single-discriminator/recursivemodel", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putRecursiveModel(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") Bird input, - RequestContext requestContext); + Response getRecursiveModel(String endpoint, String accept, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/model/inheritance/single-discriminator/missingdiscriminator", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getMissingDiscriminator(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/model/inheritance/single-discriminator/wrongdiscriminator", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getWrongDiscriminator(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/model/inheritance/single-discriminator/legacy-model", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getLegacyModel(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, + Response putRecursiveModel(String endpoint, String contentType, Bird input, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/model/inheritance/single-discriminator/no-subtypes/model", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getNoSubtypesModel(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); + Response getMissingDiscriminator(String endpoint, String accept, RequestContext requestContext); + + Response getWrongDiscriminator(String endpoint, String accept, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/model/inheritance/single-discriminator/no-subtypes/model", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putNoSubtypesModel(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") Fish input, + Response getLegacyModel(String endpoint, String accept, RequestContext requestContext); + + Response getNoSubtypesModel(String endpoint, String accept, RequestContext requestContext); + + Response putNoSubtypesModel(String endpoint, String contentType, Fish input, RequestContext requestContext); } + private static final class SingleDiscriminatorClientServiceImpl implements SingleDiscriminatorClientService { + private static final ClientLogger HTTP_LOG = new ClientLogger(SingleDiscriminatorClientService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private SingleDiscriminatorClientServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response getModel(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/model/inheritance/single-discriminator/model"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, Bird.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response putModel(String endpoint, String contentType, Bird input, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/model/inheritance/single-discriminator/model"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 getRecursiveModel(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/model/inheritance/single-discriminator/recursivemodel"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, Bird.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response putRecursiveModel(String endpoint, String contentType, Bird input, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/model/inheritance/single-discriminator/recursivemodel"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 getMissingDiscriminator(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/model/inheritance/single-discriminator/missingdiscriminator"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, Bird.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response getWrongDiscriminator(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/model/inheritance/single-discriminator/wrongdiscriminator"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, Bird.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response getLegacyModel(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/model/inheritance/single-discriminator/legacy-model"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, Dinosaur.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response getNoSubtypesModel(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/model/inheritance/single-discriminator/no-subtypes/model"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, Fish.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response putNoSubtypesModel(String endpoint, String contentType, Fish input, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/model/inheritance/single-discriminator/no-subtypes/model"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } + } + /** * The getModel operation. * diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/model/usage/implementation/UsageClientImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/model/usage/implementation/UsageClientImpl.java index 112d9195ed3..1ef92922216 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/model/usage/implementation/UsageClientImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/model/usage/implementation/UsageClientImpl.java @@ -1,20 +1,23 @@ package type.model.usage.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.model.usage.InputOutputRecord; import type.model.usage.InputRecord; import type.model.usage.OutputRecord; @@ -85,46 +88,122 @@ public UsageClientImpl(HttpPipeline httpPipeline, Instrumentation instrumentatio } /** - * The interface defining all the services for UsageClient to be used by the proxy service to perform REST calls. + * The HTTP operations for UsageClient. */ - @ServiceInterface(name = "UsageClient", host = "{endpoint}") public interface UsageClientService { static UsageClientService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.model.usage.implementation.UsageClientServiceImpl"); - return (UsageClientService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new UsageClientServiceImpl(pipeline); + } + + Response input(String endpoint, String contentType, InputRecord input, RequestContext requestContext); + + Response output(String endpoint, String accept, RequestContext requestContext); + + Response inputAndOutput(String endpoint, String contentType, String accept, + InputOutputRecord body, RequestContext requestContext); + } + + private static final class UsageClientServiceImpl implements UsageClientService { + private static final ClientLogger HTTP_LOG = new ClientLogger(UsageClientService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private UsageClientServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response input(String endpoint, String contentType, InputRecord input, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/type/model/usage/input"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 output(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/model/usage/output"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, OutputRecord.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response inputAndOutput(String endpoint, String contentType, String accept, + InputOutputRecord body, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/type/model/usage/input-output"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, InputOutputRecord.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); } - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/type/model/usage/input", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response input(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") InputRecord input, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/model/usage/output", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response output(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/type/model/usage/input-output", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response inputAndOutput(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") InputOutputRecord body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/model/visibility/implementation/VisibilityClientImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/model/visibility/implementation/VisibilityClientImpl.java index 5d022baf1cc..5fe2bbd8ad5 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/model/visibility/implementation/VisibilityClientImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/model/visibility/implementation/VisibilityClientImpl.java @@ -1,21 +1,24 @@ package type.model.visibility.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -import io.clientcore.core.http.annotations.HostParam; -import io.clientcore.core.http.annotations.HttpRequestInformation; -import io.clientcore.core.http.annotations.QueryParam; -import io.clientcore.core.http.annotations.UnexpectedResponseExceptionDetail; +import io.clientcore.core.http.models.HttpHeaderName; 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.SerializationFormat; +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 io.clientcore.core.utils.UriBuilder; +import java.lang.reflect.ParameterizedType; import type.model.visibility.ReadOnlyModel; import type.model.visibility.VisibilityModel; @@ -85,79 +88,246 @@ public VisibilityClientImpl(HttpPipeline httpPipeline, Instrumentation instrumen } /** - * The interface defining all the services for VisibilityClient to be used by the proxy service to perform REST - * calls. + * The HTTP operations for VisibilityClient. */ - @ServiceInterface(name = "VisibilityClient", host = "{endpoint}") public interface VisibilityClientService { static VisibilityClientService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.model.visibility.implementation.VisibilityClientServiceImpl"); - return (VisibilityClientService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new VisibilityClientServiceImpl(pipeline); } - @HttpRequestInformation(method = HttpMethod.GET, path = "/type/model/visibility", expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getModel(@HostParam("endpoint") String endpoint, - @QueryParam("queryProp") int queryProp, @HeaderParam("Content-Type") String contentType, - @HeaderParam("Accept") String accept, @BodyParam("application/json") VisibilityModel input, + Response getModel(String endpoint, int queryProp, String contentType, String accept, + VisibilityModel input, RequestContext requestContext); + + Response headModel(String endpoint, int queryProp, String contentType, VisibilityModel input, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.HEAD, - path = "/type/model/visibility", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response headModel(@HostParam("endpoint") String endpoint, @QueryParam("queryProp") int queryProp, - @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") VisibilityModel input, + Response putModel(String endpoint, String contentType, VisibilityModel input, RequestContext requestContext); - @HttpRequestInformation(method = HttpMethod.PUT, path = "/type/model/visibility", expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putModel(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") VisibilityModel input, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PATCH, - path = "/type/model/visibility", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response patchModel(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") VisibilityModel input, + Response patchModel(String endpoint, String contentType, VisibilityModel input, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/type/model/visibility", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response postModel(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") VisibilityModel input, + Response postModel(String endpoint, String contentType, VisibilityModel input, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.DELETE, - path = "/type/model/visibility", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response deleteModel(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") VisibilityModel input, + Response deleteModel(String endpoint, String contentType, VisibilityModel input, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/model/visibility/readonlyroundtrip", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response putReadOnlyModel(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") ReadOnlyModel input, RequestContext requestContext); + Response putReadOnlyModel(String endpoint, String contentType, String accept, + ReadOnlyModel input, RequestContext requestContext); + } + + private static final class VisibilityClientServiceImpl implements VisibilityClientService { + private static final ClientLogger HTTP_LOG = new ClientLogger(VisibilityClientService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private VisibilityClientServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response getModel(String endpoint, int queryProp, String contentType, String accept, + VisibilityModel input, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/type/model/visibility"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "queryProp", true, queryProp, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, VisibilityModel.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response headModel(String endpoint, int queryProp, String contentType, VisibilityModel input, + RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/type/model/visibility"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "queryProp", true, queryProp, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.HEAD).setUri(uriBuilder.toString()); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response networkResponse = this.httpPipeline.send(httpRequest)) { + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + 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 putModel(String endpoint, String contentType, VisibilityModel input, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/model/visibility"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 patchModel(String endpoint, String contentType, VisibilityModel input, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PATCH).setUri(endpoint + "/type/model/visibility"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 postModel(String endpoint, String contentType, VisibilityModel input, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/type/model/visibility"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 deleteModel(String endpoint, String contentType, VisibilityModel input, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.DELETE).setUri(endpoint + "/type/model/visibility"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 putReadOnlyModel(String endpoint, String contentType, String accept, + ReadOnlyModel input, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/model/visibility/readonlyroundtrip"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, ReadOnlyModel.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/ExtendsDifferentSpreadFloatsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/ExtendsDifferentSpreadFloatsImpl.java index 6f8aadef714..8eea5633f9f 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/ExtendsDifferentSpreadFloatsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/ExtendsDifferentSpreadFloatsImpl.java @@ -1,20 +1,23 @@ package type.property.additionalproperties.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.additionalproperties.DifferentSpreadFloatDerived; /** @@ -48,39 +51,91 @@ public final class ExtendsDifferentSpreadFloatsImpl { } /** - * The interface defining all the services for AdditionalPropertiesClientExtendsDifferentSpreadFloats to be used by - * the proxy service to perform REST calls. + * The HTTP operations for AdditionalPropertiesClientExtendsDifferentSpreadFloats. */ - @ServiceInterface(name = "AdditionalPropertiesClientExtendsDifferentSpreadFloats", host = "{endpoint}") public interface ExtendsDifferentSpreadFloatsService { static ExtendsDifferentSpreadFloatsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName( - "type.property.additionalproperties.implementation.ExtendsDifferentSpreadFloatsServiceImpl"); - return (ExtendsDifferentSpreadFloatsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new ExtendsDifferentSpreadFloatsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, DifferentSpreadFloatDerived body, + RequestContext requestContext); + } + + private static final class ExtendsDifferentSpreadFloatsServiceImpl implements ExtendsDifferentSpreadFloatsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ExtendsDifferentSpreadFloatsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ExtendsDifferentSpreadFloatsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/additionalProperties/extendsDifferentSpreadFloat"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, DifferentSpreadFloatDerived.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, DifferentSpreadFloatDerived body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/additionalProperties/extendsDifferentSpreadFloat"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/additionalProperties/extendsDifferentSpreadFloat", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/additionalProperties/extendsDifferentSpreadFloat", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") DifferentSpreadFloatDerived body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/ExtendsDifferentSpreadModelArraysImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/ExtendsDifferentSpreadModelArraysImpl.java index 11bd008cac9..594729d716f 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/ExtendsDifferentSpreadModelArraysImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/ExtendsDifferentSpreadModelArraysImpl.java @@ -1,20 +1,23 @@ package type.property.additionalproperties.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.additionalproperties.DifferentSpreadModelArrayDerived; /** @@ -48,39 +51,92 @@ public final class ExtendsDifferentSpreadModelArraysImpl { } /** - * The interface defining all the services for AdditionalPropertiesClientExtendsDifferentSpreadModelArrays to be - * used by the proxy service to perform REST calls. + * The HTTP operations for AdditionalPropertiesClientExtendsDifferentSpreadModelArrays. */ - @ServiceInterface(name = "AdditionalPropertiesClientExtendsDifferentSpreadModelArrays", host = "{endpoint}") public interface ExtendsDifferentSpreadModelArraysService { static ExtendsDifferentSpreadModelArraysService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName( - "type.property.additionalproperties.implementation.ExtendsDifferentSpreadModelArraysServiceImpl"); - return (ExtendsDifferentSpreadModelArraysService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new ExtendsDifferentSpreadModelArraysServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, DifferentSpreadModelArrayDerived body, + RequestContext requestContext); + } + + private static final class ExtendsDifferentSpreadModelArraysServiceImpl + implements ExtendsDifferentSpreadModelArraysService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ExtendsDifferentSpreadModelArraysService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ExtendsDifferentSpreadModelArraysServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/additionalProperties/extendsDifferentSpreadModelArray"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, DifferentSpreadModelArrayDerived.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, DifferentSpreadModelArrayDerived body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/additionalProperties/extendsDifferentSpreadModelArray"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/additionalProperties/extendsDifferentSpreadModelArray", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/additionalProperties/extendsDifferentSpreadModelArray", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") DifferentSpreadModelArrayDerived body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/ExtendsDifferentSpreadModelsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/ExtendsDifferentSpreadModelsImpl.java index 4381840c74b..4095854783e 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/ExtendsDifferentSpreadModelsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/ExtendsDifferentSpreadModelsImpl.java @@ -1,20 +1,23 @@ package type.property.additionalproperties.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.additionalproperties.DifferentSpreadModelDerived; /** @@ -48,39 +51,91 @@ public final class ExtendsDifferentSpreadModelsImpl { } /** - * The interface defining all the services for AdditionalPropertiesClientExtendsDifferentSpreadModels to be used by - * the proxy service to perform REST calls. + * The HTTP operations for AdditionalPropertiesClientExtendsDifferentSpreadModels. */ - @ServiceInterface(name = "AdditionalPropertiesClientExtendsDifferentSpreadModels", host = "{endpoint}") public interface ExtendsDifferentSpreadModelsService { static ExtendsDifferentSpreadModelsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName( - "type.property.additionalproperties.implementation.ExtendsDifferentSpreadModelsServiceImpl"); - return (ExtendsDifferentSpreadModelsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new ExtendsDifferentSpreadModelsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, DifferentSpreadModelDerived body, + RequestContext requestContext); + } + + private static final class ExtendsDifferentSpreadModelsServiceImpl implements ExtendsDifferentSpreadModelsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ExtendsDifferentSpreadModelsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ExtendsDifferentSpreadModelsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/additionalProperties/extendsDifferentSpreadModel"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, DifferentSpreadModelDerived.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, DifferentSpreadModelDerived body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/additionalProperties/extendsDifferentSpreadModel"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/additionalProperties/extendsDifferentSpreadModel", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/additionalProperties/extendsDifferentSpreadModel", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") DifferentSpreadModelDerived body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/ExtendsDifferentSpreadStringsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/ExtendsDifferentSpreadStringsImpl.java index c7968c6d6e8..9e3e4d18dcd 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/ExtendsDifferentSpreadStringsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/ExtendsDifferentSpreadStringsImpl.java @@ -1,20 +1,23 @@ package type.property.additionalproperties.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.additionalproperties.DifferentSpreadStringDerived; /** @@ -48,39 +51,92 @@ public final class ExtendsDifferentSpreadStringsImpl { } /** - * The interface defining all the services for AdditionalPropertiesClientExtendsDifferentSpreadStrings to be used by - * the proxy service to perform REST calls. + * The HTTP operations for AdditionalPropertiesClientExtendsDifferentSpreadStrings. */ - @ServiceInterface(name = "AdditionalPropertiesClientExtendsDifferentSpreadStrings", host = "{endpoint}") public interface ExtendsDifferentSpreadStringsService { static ExtendsDifferentSpreadStringsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName( - "type.property.additionalproperties.implementation.ExtendsDifferentSpreadStringsServiceImpl"); - return (ExtendsDifferentSpreadStringsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new ExtendsDifferentSpreadStringsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, DifferentSpreadStringDerived body, + RequestContext requestContext); + } + + private static final class ExtendsDifferentSpreadStringsServiceImpl + implements ExtendsDifferentSpreadStringsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ExtendsDifferentSpreadStringsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ExtendsDifferentSpreadStringsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/additionalProperties/extendsDifferentSpreadString"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, DifferentSpreadStringDerived.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, DifferentSpreadStringDerived body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/additionalProperties/extendsDifferentSpreadString"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/additionalProperties/extendsDifferentSpreadString", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/additionalProperties/extendsDifferentSpreadString", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") DifferentSpreadStringDerived body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/ExtendsFloatsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/ExtendsFloatsImpl.java index c7409cdaea0..b8446a91288 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/ExtendsFloatsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/ExtendsFloatsImpl.java @@ -1,20 +1,23 @@ package type.property.additionalproperties.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.additionalproperties.ExtendsFloatAdditionalProperties; /** @@ -48,39 +51,91 @@ public final class ExtendsFloatsImpl { } /** - * The interface defining all the services for AdditionalPropertiesClientExtendsFloats to be used by the proxy - * service to perform REST calls. + * The HTTP operations for AdditionalPropertiesClientExtendsFloats. */ - @ServiceInterface(name = "AdditionalPropertiesClientExtendsFloats", host = "{endpoint}") public interface ExtendsFloatsService { static ExtendsFloatsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz - = Class.forName("type.property.additionalproperties.implementation.ExtendsFloatsServiceImpl"); - return (ExtendsFloatsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new ExtendsFloatsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, ExtendsFloatAdditionalProperties body, + RequestContext requestContext); + } + + private static final class ExtendsFloatsServiceImpl implements ExtendsFloatsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ExtendsFloatsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ExtendsFloatsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/additionalProperties/extendsRecordFloat"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, ExtendsFloatAdditionalProperties.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, ExtendsFloatAdditionalProperties body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/additionalProperties/extendsRecordFloat"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/additionalProperties/extendsRecordFloat", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/additionalProperties/extendsRecordFloat", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") ExtendsFloatAdditionalProperties body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/ExtendsModelArraysImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/ExtendsModelArraysImpl.java index 7a594877a77..dabc0b2b087 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/ExtendsModelArraysImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/ExtendsModelArraysImpl.java @@ -1,20 +1,23 @@ package type.property.additionalproperties.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.additionalproperties.ExtendsModelArrayAdditionalProperties; /** @@ -48,39 +51,92 @@ public final class ExtendsModelArraysImpl { } /** - * The interface defining all the services for AdditionalPropertiesClientExtendsModelArrays to be used by the proxy - * service to perform REST calls. + * The HTTP operations for AdditionalPropertiesClientExtendsModelArrays. */ - @ServiceInterface(name = "AdditionalPropertiesClientExtendsModelArrays", host = "{endpoint}") public interface ExtendsModelArraysService { static ExtendsModelArraysService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz - = Class.forName("type.property.additionalproperties.implementation.ExtendsModelArraysServiceImpl"); - return (ExtendsModelArraysService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new ExtendsModelArraysServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, + RequestContext requestContext); + + Response put(String endpoint, String contentType, ExtendsModelArrayAdditionalProperties body, + RequestContext requestContext); + } + + private static final class ExtendsModelArraysServiceImpl implements ExtendsModelArraysService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ExtendsModelArraysService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ExtendsModelArraysServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/additionalProperties/extendsRecordModelArray"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, ExtendsModelArrayAdditionalProperties.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, ExtendsModelArrayAdditionalProperties body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/additionalProperties/extendsRecordModelArray"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/additionalProperties/extendsRecordModelArray", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/additionalProperties/extendsRecordModelArray", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") ExtendsModelArrayAdditionalProperties body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/ExtendsModelsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/ExtendsModelsImpl.java index 0cc4ae03b33..d1ba6608822 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/ExtendsModelsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/ExtendsModelsImpl.java @@ -1,20 +1,23 @@ package type.property.additionalproperties.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.additionalproperties.ExtendsModelAdditionalProperties; /** @@ -48,39 +51,91 @@ public final class ExtendsModelsImpl { } /** - * The interface defining all the services for AdditionalPropertiesClientExtendsModels to be used by the proxy - * service to perform REST calls. + * The HTTP operations for AdditionalPropertiesClientExtendsModels. */ - @ServiceInterface(name = "AdditionalPropertiesClientExtendsModels", host = "{endpoint}") public interface ExtendsModelsService { static ExtendsModelsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz - = Class.forName("type.property.additionalproperties.implementation.ExtendsModelsServiceImpl"); - return (ExtendsModelsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new ExtendsModelsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, ExtendsModelAdditionalProperties body, + RequestContext requestContext); + } + + private static final class ExtendsModelsServiceImpl implements ExtendsModelsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ExtendsModelsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ExtendsModelsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/additionalProperties/extendsRecordModel"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, ExtendsModelAdditionalProperties.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, ExtendsModelAdditionalProperties body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/additionalProperties/extendsRecordModel"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/additionalProperties/extendsRecordModel", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/additionalProperties/extendsRecordModel", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") ExtendsModelAdditionalProperties body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/ExtendsStringsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/ExtendsStringsImpl.java index 207bc26b055..0b509e61da0 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/ExtendsStringsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/ExtendsStringsImpl.java @@ -1,20 +1,23 @@ package type.property.additionalproperties.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.additionalproperties.ExtendsStringAdditionalProperties; /** @@ -48,39 +51,91 @@ public final class ExtendsStringsImpl { } /** - * The interface defining all the services for AdditionalPropertiesClientExtendsStrings to be used by the proxy - * service to perform REST calls. + * The HTTP operations for AdditionalPropertiesClientExtendsStrings. */ - @ServiceInterface(name = "AdditionalPropertiesClientExtendsStrings", host = "{endpoint}") public interface ExtendsStringsService { static ExtendsStringsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz - = Class.forName("type.property.additionalproperties.implementation.ExtendsStringsServiceImpl"); - return (ExtendsStringsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new ExtendsStringsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, ExtendsStringAdditionalProperties body, + RequestContext requestContext); + } + + private static final class ExtendsStringsServiceImpl implements ExtendsStringsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ExtendsStringsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ExtendsStringsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/additionalProperties/extendsRecordString"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, ExtendsStringAdditionalProperties.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, ExtendsStringAdditionalProperties body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/additionalProperties/extendsRecordString"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/additionalProperties/extendsRecordString", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/additionalProperties/extendsRecordString", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") ExtendsStringAdditionalProperties body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/ExtendsUnknownDerivedsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/ExtendsUnknownDerivedsImpl.java index 8b951499fc4..5b958641601 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/ExtendsUnknownDerivedsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/ExtendsUnknownDerivedsImpl.java @@ -1,20 +1,23 @@ package type.property.additionalproperties.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.additionalproperties.ExtendsUnknownAdditionalPropertiesDerived; /** @@ -48,40 +51,92 @@ public final class ExtendsUnknownDerivedsImpl { } /** - * The interface defining all the services for AdditionalPropertiesClientExtendsUnknownDeriveds to be used by the - * proxy service to perform REST calls. + * The HTTP operations for AdditionalPropertiesClientExtendsUnknownDeriveds. */ - @ServiceInterface(name = "AdditionalPropertiesClientExtendsUnknownDeriveds", host = "{endpoint}") public interface ExtendsUnknownDerivedsService { static ExtendsUnknownDerivedsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class - .forName("type.property.additionalproperties.implementation.ExtendsUnknownDerivedsServiceImpl"); - return (ExtendsUnknownDerivedsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new ExtendsUnknownDerivedsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, + RequestContext requestContext); + + Response put(String endpoint, String contentType, ExtendsUnknownAdditionalPropertiesDerived body, + RequestContext requestContext); + } + + private static final class ExtendsUnknownDerivedsServiceImpl implements ExtendsUnknownDerivedsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ExtendsUnknownDerivedsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ExtendsUnknownDerivedsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/additionalProperties/extendsRecordUnknownDerived"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, ExtendsUnknownAdditionalPropertiesDerived.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, ExtendsUnknownAdditionalPropertiesDerived body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/additionalProperties/extendsRecordUnknownDerived"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/additionalProperties/extendsRecordUnknownDerived", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/additionalProperties/extendsRecordUnknownDerived", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") ExtendsUnknownAdditionalPropertiesDerived body, - RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/ExtendsUnknownDiscriminatedsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/ExtendsUnknownDiscriminatedsImpl.java index e01b30af6ca..91f3e68e0e1 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/ExtendsUnknownDiscriminatedsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/ExtendsUnknownDiscriminatedsImpl.java @@ -1,20 +1,23 @@ package type.property.additionalproperties.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.additionalproperties.ExtendsUnknownAdditionalPropertiesDiscriminated; /** @@ -48,40 +51,93 @@ public final class ExtendsUnknownDiscriminatedsImpl { } /** - * The interface defining all the services for AdditionalPropertiesClientExtendsUnknownDiscriminateds to be used by - * the proxy service to perform REST calls. + * The HTTP operations for AdditionalPropertiesClientExtendsUnknownDiscriminateds. */ - @ServiceInterface(name = "AdditionalPropertiesClientExtendsUnknownDiscriminateds", host = "{endpoint}") public interface ExtendsUnknownDiscriminatedsService { static ExtendsUnknownDiscriminatedsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName( - "type.property.additionalproperties.implementation.ExtendsUnknownDiscriminatedsServiceImpl"); - return (ExtendsUnknownDiscriminatedsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new ExtendsUnknownDiscriminatedsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, + RequestContext requestContext); + + Response put(String endpoint, String contentType, ExtendsUnknownAdditionalPropertiesDiscriminated body, + RequestContext requestContext); + } + + private static final class ExtendsUnknownDiscriminatedsServiceImpl implements ExtendsUnknownDiscriminatedsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ExtendsUnknownDiscriminatedsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ExtendsUnknownDiscriminatedsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/additionalProperties/extendsUnknownDiscriminated"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, + ExtendsUnknownAdditionalPropertiesDiscriminated.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, + ExtendsUnknownAdditionalPropertiesDiscriminated body, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/additionalProperties/extendsUnknownDiscriminated"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/additionalProperties/extendsUnknownDiscriminated", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/additionalProperties/extendsUnknownDiscriminated", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") ExtendsUnknownAdditionalPropertiesDiscriminated body, - RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/ExtendsUnknownsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/ExtendsUnknownsImpl.java index 4c79c4f57c8..473f783a620 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/ExtendsUnknownsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/ExtendsUnknownsImpl.java @@ -1,20 +1,23 @@ package type.property.additionalproperties.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.additionalproperties.ExtendsUnknownAdditionalProperties; /** @@ -48,39 +51,91 @@ public final class ExtendsUnknownsImpl { } /** - * The interface defining all the services for AdditionalPropertiesClientExtendsUnknowns to be used by the proxy - * service to perform REST calls. + * The HTTP operations for AdditionalPropertiesClientExtendsUnknowns. */ - @ServiceInterface(name = "AdditionalPropertiesClientExtendsUnknowns", host = "{endpoint}") public interface ExtendsUnknownsService { static ExtendsUnknownsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz - = Class.forName("type.property.additionalproperties.implementation.ExtendsUnknownsServiceImpl"); - return (ExtendsUnknownsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new ExtendsUnknownsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, ExtendsUnknownAdditionalProperties body, + RequestContext requestContext); + } + + private static final class ExtendsUnknownsServiceImpl implements ExtendsUnknownsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ExtendsUnknownsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ExtendsUnknownsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/additionalProperties/extendsRecordUnknown"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, ExtendsUnknownAdditionalProperties.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, ExtendsUnknownAdditionalProperties body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/additionalProperties/extendsRecordUnknown"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/additionalProperties/extendsRecordUnknown", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/additionalProperties/extendsRecordUnknown", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") ExtendsUnknownAdditionalProperties body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/IsFloatsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/IsFloatsImpl.java index f66210cff38..8f37aeec9da 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/IsFloatsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/IsFloatsImpl.java @@ -1,20 +1,23 @@ package type.property.additionalproperties.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.additionalproperties.IsFloatAdditionalProperties; /** @@ -48,37 +51,91 @@ public final class IsFloatsImpl { } /** - * The interface defining all the services for AdditionalPropertiesClientIsFloats to be used by the proxy service to - * perform REST calls. + * The HTTP operations for AdditionalPropertiesClientIsFloats. */ - @ServiceInterface(name = "AdditionalPropertiesClientIsFloats", host = "{endpoint}") public interface IsFloatsService { static IsFloatsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.additionalproperties.implementation.IsFloatsServiceImpl"); - return (IsFloatsService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new IsFloatsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, IsFloatAdditionalProperties body, + RequestContext requestContext); + } + + private static final class IsFloatsServiceImpl implements IsFloatsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(IsFloatsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private IsFloatsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/additionalProperties/isRecordFloat"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, IsFloatAdditionalProperties.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, IsFloatAdditionalProperties body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/additionalProperties/isRecordFloat"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/additionalProperties/isRecordFloat", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/additionalProperties/isRecordFloat", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") IsFloatAdditionalProperties body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/IsModelArraysImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/IsModelArraysImpl.java index 6d2ffe82b00..37bf4344c03 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/IsModelArraysImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/IsModelArraysImpl.java @@ -1,20 +1,23 @@ package type.property.additionalproperties.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.additionalproperties.IsModelArrayAdditionalProperties; /** @@ -48,39 +51,91 @@ public final class IsModelArraysImpl { } /** - * The interface defining all the services for AdditionalPropertiesClientIsModelArrays to be used by the proxy - * service to perform REST calls. + * The HTTP operations for AdditionalPropertiesClientIsModelArrays. */ - @ServiceInterface(name = "AdditionalPropertiesClientIsModelArrays", host = "{endpoint}") public interface IsModelArraysService { static IsModelArraysService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz - = Class.forName("type.property.additionalproperties.implementation.IsModelArraysServiceImpl"); - return (IsModelArraysService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new IsModelArraysServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, IsModelArrayAdditionalProperties body, + RequestContext requestContext); + } + + private static final class IsModelArraysServiceImpl implements IsModelArraysService { + private static final ClientLogger HTTP_LOG = new ClientLogger(IsModelArraysService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private IsModelArraysServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/additionalProperties/isRecordModelArray"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, IsModelArrayAdditionalProperties.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, IsModelArrayAdditionalProperties body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/additionalProperties/isRecordModelArray"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/additionalProperties/isRecordModelArray", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/additionalProperties/isRecordModelArray", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") IsModelArrayAdditionalProperties body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/IsModelsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/IsModelsImpl.java index 6274e5754d0..efd1978ad0b 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/IsModelsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/IsModelsImpl.java @@ -1,20 +1,23 @@ package type.property.additionalproperties.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.additionalproperties.IsModelAdditionalProperties; /** @@ -48,37 +51,91 @@ public final class IsModelsImpl { } /** - * The interface defining all the services for AdditionalPropertiesClientIsModels to be used by the proxy service to - * perform REST calls. + * The HTTP operations for AdditionalPropertiesClientIsModels. */ - @ServiceInterface(name = "AdditionalPropertiesClientIsModels", host = "{endpoint}") public interface IsModelsService { static IsModelsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.additionalproperties.implementation.IsModelsServiceImpl"); - return (IsModelsService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new IsModelsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, IsModelAdditionalProperties body, + RequestContext requestContext); + } + + private static final class IsModelsServiceImpl implements IsModelsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(IsModelsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private IsModelsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/additionalProperties/isRecordModel"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, IsModelAdditionalProperties.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, IsModelAdditionalProperties body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/additionalProperties/isRecordModel"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/additionalProperties/isRecordModel", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/additionalProperties/isRecordModel", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") IsModelAdditionalProperties body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/IsStringsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/IsStringsImpl.java index cadd904d08a..7288c0e8627 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/IsStringsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/IsStringsImpl.java @@ -1,20 +1,23 @@ package type.property.additionalproperties.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.additionalproperties.IsStringAdditionalProperties; /** @@ -48,38 +51,91 @@ public final class IsStringsImpl { } /** - * The interface defining all the services for AdditionalPropertiesClientIsStrings to be used by the proxy service - * to perform REST calls. + * The HTTP operations for AdditionalPropertiesClientIsStrings. */ - @ServiceInterface(name = "AdditionalPropertiesClientIsStrings", host = "{endpoint}") public interface IsStringsService { static IsStringsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz - = Class.forName("type.property.additionalproperties.implementation.IsStringsServiceImpl"); - return (IsStringsService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new IsStringsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, IsStringAdditionalProperties body, + RequestContext requestContext); + } + + private static final class IsStringsServiceImpl implements IsStringsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(IsStringsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private IsStringsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/additionalProperties/isRecordstring"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, IsStringAdditionalProperties.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, IsStringAdditionalProperties body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/additionalProperties/isRecordstring"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/additionalProperties/isRecordstring", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/additionalProperties/isRecordstring", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") IsStringAdditionalProperties body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/IsUnknownDerivedsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/IsUnknownDerivedsImpl.java index b4452abfad5..fb5a5dff0f4 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/IsUnknownDerivedsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/IsUnknownDerivedsImpl.java @@ -1,20 +1,23 @@ package type.property.additionalproperties.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.additionalproperties.IsUnknownAdditionalPropertiesDerived; /** @@ -48,39 +51,92 @@ public final class IsUnknownDerivedsImpl { } /** - * The interface defining all the services for AdditionalPropertiesClientIsUnknownDeriveds to be used by the proxy - * service to perform REST calls. + * The HTTP operations for AdditionalPropertiesClientIsUnknownDeriveds. */ - @ServiceInterface(name = "AdditionalPropertiesClientIsUnknownDeriveds", host = "{endpoint}") public interface IsUnknownDerivedsService { static IsUnknownDerivedsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz - = Class.forName("type.property.additionalproperties.implementation.IsUnknownDerivedsServiceImpl"); - return (IsUnknownDerivedsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new IsUnknownDerivedsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, + RequestContext requestContext); + + Response put(String endpoint, String contentType, IsUnknownAdditionalPropertiesDerived body, + RequestContext requestContext); + } + + private static final class IsUnknownDerivedsServiceImpl implements IsUnknownDerivedsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(IsUnknownDerivedsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private IsUnknownDerivedsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/additionalProperties/isRecordUnknownDerived"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, IsUnknownAdditionalPropertiesDerived.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, IsUnknownAdditionalPropertiesDerived body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/additionalProperties/isRecordUnknownDerived"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/additionalProperties/isRecordUnknownDerived", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/additionalProperties/isRecordUnknownDerived", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") IsUnknownAdditionalPropertiesDerived body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/IsUnknownDiscriminatedsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/IsUnknownDiscriminatedsImpl.java index d6860077c3a..97bc4270d36 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/IsUnknownDiscriminatedsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/IsUnknownDiscriminatedsImpl.java @@ -1,20 +1,23 @@ package type.property.additionalproperties.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.additionalproperties.IsUnknownAdditionalPropertiesDiscriminated; /** @@ -48,40 +51,92 @@ public final class IsUnknownDiscriminatedsImpl { } /** - * The interface defining all the services for AdditionalPropertiesClientIsUnknownDiscriminateds to be used by the - * proxy service to perform REST calls. + * The HTTP operations for AdditionalPropertiesClientIsUnknownDiscriminateds. */ - @ServiceInterface(name = "AdditionalPropertiesClientIsUnknownDiscriminateds", host = "{endpoint}") public interface IsUnknownDiscriminatedsService { static IsUnknownDiscriminatedsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class - .forName("type.property.additionalproperties.implementation.IsUnknownDiscriminatedsServiceImpl"); - return (IsUnknownDiscriminatedsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new IsUnknownDiscriminatedsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, + RequestContext requestContext); + + Response put(String endpoint, String contentType, IsUnknownAdditionalPropertiesDiscriminated body, + RequestContext requestContext); + } + + private static final class IsUnknownDiscriminatedsServiceImpl implements IsUnknownDiscriminatedsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(IsUnknownDiscriminatedsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private IsUnknownDiscriminatedsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/additionalProperties/isUnknownDiscriminated"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, IsUnknownAdditionalPropertiesDiscriminated.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, IsUnknownAdditionalPropertiesDiscriminated body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/additionalProperties/isUnknownDiscriminated"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/additionalProperties/isUnknownDiscriminated", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/additionalProperties/isUnknownDiscriminated", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") IsUnknownAdditionalPropertiesDiscriminated body, - RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/IsUnknownsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/IsUnknownsImpl.java index e08298ebd85..c071355992f 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/IsUnknownsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/IsUnknownsImpl.java @@ -1,20 +1,23 @@ package type.property.additionalproperties.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.additionalproperties.IsUnknownAdditionalProperties; /** @@ -48,38 +51,91 @@ public final class IsUnknownsImpl { } /** - * The interface defining all the services for AdditionalPropertiesClientIsUnknowns to be used by the proxy service - * to perform REST calls. + * The HTTP operations for AdditionalPropertiesClientIsUnknowns. */ - @ServiceInterface(name = "AdditionalPropertiesClientIsUnknowns", host = "{endpoint}") public interface IsUnknownsService { static IsUnknownsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz - = Class.forName("type.property.additionalproperties.implementation.IsUnknownsServiceImpl"); - return (IsUnknownsService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new IsUnknownsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, IsUnknownAdditionalProperties body, + RequestContext requestContext); + } + + private static final class IsUnknownsServiceImpl implements IsUnknownsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(IsUnknownsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private IsUnknownsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/additionalProperties/isRecordUnknown"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, IsUnknownAdditionalProperties.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, IsUnknownAdditionalProperties body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/additionalProperties/isRecordUnknown"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/additionalProperties/isRecordUnknown", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/additionalProperties/isRecordUnknown", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") IsUnknownAdditionalProperties body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/MultipleSpreadsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/MultipleSpreadsImpl.java index 3d8da444e07..1762a95dff7 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/MultipleSpreadsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/MultipleSpreadsImpl.java @@ -1,20 +1,23 @@ package type.property.additionalproperties.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.additionalproperties.MultipleSpreadRecord; /** @@ -48,39 +51,90 @@ public final class MultipleSpreadsImpl { } /** - * The interface defining all the services for AdditionalPropertiesClientMultipleSpreads to be used by the proxy - * service to perform REST calls. + * The HTTP operations for AdditionalPropertiesClientMultipleSpreads. */ - @ServiceInterface(name = "AdditionalPropertiesClientMultipleSpreads", host = "{endpoint}") public interface MultipleSpreadsService { static MultipleSpreadsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz - = Class.forName("type.property.additionalproperties.implementation.MultipleSpreadsServiceImpl"); - return (MultipleSpreadsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new MultipleSpreadsServiceImpl(pipeline); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/additionalProperties/multipleSpreadRecord", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, MultipleSpreadRecord body, RequestContext requestContext); + } + + private static final class MultipleSpreadsServiceImpl implements MultipleSpreadsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(MultipleSpreadsService.class); + + private final HttpPipeline httpPipeline; - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/additionalProperties/multipleSpreadRecord", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") MultipleSpreadRecord body, RequestContext requestContext); + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private MultipleSpreadsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/additionalProperties/multipleSpreadRecord"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, MultipleSpreadRecord.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response put(String endpoint, String contentType, MultipleSpreadRecord body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/additionalProperties/multipleSpreadRecord"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadDifferentFloatsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadDifferentFloatsImpl.java index 8f36eca394e..f345115aa4b 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadDifferentFloatsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadDifferentFloatsImpl.java @@ -1,20 +1,23 @@ package type.property.additionalproperties.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.additionalproperties.DifferentSpreadFloatRecord; /** @@ -48,39 +51,90 @@ public final class SpreadDifferentFloatsImpl { } /** - * The interface defining all the services for AdditionalPropertiesClientSpreadDifferentFloats to be used by the - * proxy service to perform REST calls. + * The HTTP operations for AdditionalPropertiesClientSpreadDifferentFloats. */ - @ServiceInterface(name = "AdditionalPropertiesClientSpreadDifferentFloats", host = "{endpoint}") public interface SpreadDifferentFloatsService { static SpreadDifferentFloatsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class - .forName("type.property.additionalproperties.implementation.SpreadDifferentFloatsServiceImpl"); - return (SpreadDifferentFloatsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new SpreadDifferentFloatsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, DifferentSpreadFloatRecord body, + RequestContext requestContext); + } + + private static final class SpreadDifferentFloatsServiceImpl implements SpreadDifferentFloatsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(SpreadDifferentFloatsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private SpreadDifferentFloatsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/additionalProperties/spreadDifferentRecordFloat"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, DifferentSpreadFloatRecord.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, DifferentSpreadFloatRecord body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/additionalProperties/spreadDifferentRecordFloat"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/additionalProperties/spreadDifferentRecordFloat", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/additionalProperties/spreadDifferentRecordFloat", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") DifferentSpreadFloatRecord body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadDifferentModelArraysImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadDifferentModelArraysImpl.java index 2a106f4f089..2bd398c21b8 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadDifferentModelArraysImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadDifferentModelArraysImpl.java @@ -1,20 +1,23 @@ package type.property.additionalproperties.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.additionalproperties.DifferentSpreadModelArrayRecord; /** @@ -48,39 +51,91 @@ public final class SpreadDifferentModelArraysImpl { } /** - * The interface defining all the services for AdditionalPropertiesClientSpreadDifferentModelArrays to be used by - * the proxy service to perform REST calls. + * The HTTP operations for AdditionalPropertiesClientSpreadDifferentModelArrays. */ - @ServiceInterface(name = "AdditionalPropertiesClientSpreadDifferentModelArrays", host = "{endpoint}") public interface SpreadDifferentModelArraysService { static SpreadDifferentModelArraysService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class - .forName("type.property.additionalproperties.implementation.SpreadDifferentModelArraysServiceImpl"); - return (SpreadDifferentModelArraysService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new SpreadDifferentModelArraysServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, DifferentSpreadModelArrayRecord body, + RequestContext requestContext); + } + + private static final class SpreadDifferentModelArraysServiceImpl implements SpreadDifferentModelArraysService { + private static final ClientLogger HTTP_LOG = new ClientLogger(SpreadDifferentModelArraysService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private SpreadDifferentModelArraysServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/additionalProperties/spreadDifferentRecordModelArray"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, DifferentSpreadModelArrayRecord.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, DifferentSpreadModelArrayRecord body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/additionalProperties/spreadDifferentRecordModelArray"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/additionalProperties/spreadDifferentRecordModelArray", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/additionalProperties/spreadDifferentRecordModelArray", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") DifferentSpreadModelArrayRecord body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadDifferentModelsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadDifferentModelsImpl.java index 04815821306..12ae2fb60cb 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadDifferentModelsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadDifferentModelsImpl.java @@ -1,20 +1,23 @@ package type.property.additionalproperties.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.additionalproperties.DifferentSpreadModelRecord; /** @@ -48,39 +51,90 @@ public final class SpreadDifferentModelsImpl { } /** - * The interface defining all the services for AdditionalPropertiesClientSpreadDifferentModels to be used by the - * proxy service to perform REST calls. + * The HTTP operations for AdditionalPropertiesClientSpreadDifferentModels. */ - @ServiceInterface(name = "AdditionalPropertiesClientSpreadDifferentModels", host = "{endpoint}") public interface SpreadDifferentModelsService { static SpreadDifferentModelsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class - .forName("type.property.additionalproperties.implementation.SpreadDifferentModelsServiceImpl"); - return (SpreadDifferentModelsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new SpreadDifferentModelsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, DifferentSpreadModelRecord body, + RequestContext requestContext); + } + + private static final class SpreadDifferentModelsServiceImpl implements SpreadDifferentModelsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(SpreadDifferentModelsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private SpreadDifferentModelsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/additionalProperties/spreadDifferentRecordModel"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, DifferentSpreadModelRecord.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, DifferentSpreadModelRecord body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/additionalProperties/spreadDifferentRecordModel"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/additionalProperties/spreadDifferentRecordModel", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/additionalProperties/spreadDifferentRecordModel", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") DifferentSpreadModelRecord body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadDifferentStringsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadDifferentStringsImpl.java index f8279955559..2adfa51c360 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadDifferentStringsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadDifferentStringsImpl.java @@ -1,20 +1,23 @@ package type.property.additionalproperties.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.additionalproperties.DifferentSpreadStringRecord; /** @@ -48,39 +51,91 @@ public final class SpreadDifferentStringsImpl { } /** - * The interface defining all the services for AdditionalPropertiesClientSpreadDifferentStrings to be used by the - * proxy service to perform REST calls. + * The HTTP operations for AdditionalPropertiesClientSpreadDifferentStrings. */ - @ServiceInterface(name = "AdditionalPropertiesClientSpreadDifferentStrings", host = "{endpoint}") public interface SpreadDifferentStringsService { static SpreadDifferentStringsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class - .forName("type.property.additionalproperties.implementation.SpreadDifferentStringsServiceImpl"); - return (SpreadDifferentStringsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new SpreadDifferentStringsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, DifferentSpreadStringRecord body, + RequestContext requestContext); + } + + private static final class SpreadDifferentStringsServiceImpl implements SpreadDifferentStringsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(SpreadDifferentStringsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private SpreadDifferentStringsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/additionalProperties/spreadDifferentRecordString"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, DifferentSpreadStringRecord.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, DifferentSpreadStringRecord body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/additionalProperties/spreadDifferentRecordString"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/additionalProperties/spreadDifferentRecordString", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/additionalProperties/spreadDifferentRecordString", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") DifferentSpreadStringRecord body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadFloatsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadFloatsImpl.java index 05073524ad3..ba4dab9928e 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadFloatsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadFloatsImpl.java @@ -1,20 +1,23 @@ package type.property.additionalproperties.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.additionalproperties.SpreadFloatRecord; /** @@ -48,39 +51,89 @@ public final class SpreadFloatsImpl { } /** - * The interface defining all the services for AdditionalPropertiesClientSpreadFloats to be used by the proxy - * service to perform REST calls. + * The HTTP operations for AdditionalPropertiesClientSpreadFloats. */ - @ServiceInterface(name = "AdditionalPropertiesClientSpreadFloats", host = "{endpoint}") public interface SpreadFloatsService { static SpreadFloatsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz - = Class.forName("type.property.additionalproperties.implementation.SpreadFloatsServiceImpl"); - return (SpreadFloatsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new SpreadFloatsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, SpreadFloatRecord body, RequestContext requestContext); + } + + private static final class SpreadFloatsServiceImpl implements SpreadFloatsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(SpreadFloatsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private SpreadFloatsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/additionalProperties/spreadRecordFloat"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, SpreadFloatRecord.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, SpreadFloatRecord body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/additionalProperties/spreadRecordFloat"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/additionalProperties/spreadRecordFloat", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/additionalProperties/spreadRecordFloat", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") SpreadFloatRecord body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadModelArraysImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadModelArraysImpl.java index d8477bfb895..e78a1e39a64 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadModelArraysImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadModelArraysImpl.java @@ -1,20 +1,23 @@ package type.property.additionalproperties.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.additionalproperties.SpreadModelArrayRecord; /** @@ -48,39 +51,90 @@ public final class SpreadModelArraysImpl { } /** - * The interface defining all the services for AdditionalPropertiesClientSpreadModelArrays to be used by the proxy - * service to perform REST calls. + * The HTTP operations for AdditionalPropertiesClientSpreadModelArrays. */ - @ServiceInterface(name = "AdditionalPropertiesClientSpreadModelArrays", host = "{endpoint}") public interface SpreadModelArraysService { static SpreadModelArraysService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz - = Class.forName("type.property.additionalproperties.implementation.SpreadModelArraysServiceImpl"); - return (SpreadModelArraysService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new SpreadModelArraysServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, SpreadModelArrayRecord body, + RequestContext requestContext); + } + + private static final class SpreadModelArraysServiceImpl implements SpreadModelArraysService { + private static final ClientLogger HTTP_LOG = new ClientLogger(SpreadModelArraysService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private SpreadModelArraysServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/additionalProperties/spreadRecordModelArray"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, SpreadModelArrayRecord.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, SpreadModelArrayRecord body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/additionalProperties/spreadRecordModelArray"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/additionalProperties/spreadRecordModelArray", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/additionalProperties/spreadRecordModelArray", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") SpreadModelArrayRecord body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadModelsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadModelsImpl.java index 507d183d4e6..70587ba556b 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadModelsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadModelsImpl.java @@ -1,20 +1,23 @@ package type.property.additionalproperties.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.additionalproperties.SpreadModelRecord; /** @@ -48,39 +51,89 @@ public final class SpreadModelsImpl { } /** - * The interface defining all the services for AdditionalPropertiesClientSpreadModels to be used by the proxy - * service to perform REST calls. + * The HTTP operations for AdditionalPropertiesClientSpreadModels. */ - @ServiceInterface(name = "AdditionalPropertiesClientSpreadModels", host = "{endpoint}") public interface SpreadModelsService { static SpreadModelsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz - = Class.forName("type.property.additionalproperties.implementation.SpreadModelsServiceImpl"); - return (SpreadModelsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new SpreadModelsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, SpreadModelRecord body, RequestContext requestContext); + } + + private static final class SpreadModelsServiceImpl implements SpreadModelsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(SpreadModelsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private SpreadModelsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/additionalProperties/spreadRecordModel"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, SpreadModelRecord.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, SpreadModelRecord body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/additionalProperties/spreadRecordModel"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/additionalProperties/spreadRecordModel", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/additionalProperties/spreadRecordModel", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") SpreadModelRecord body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadRecordNonDiscriminatedUnion2sImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadRecordNonDiscriminatedUnion2sImpl.java index e70497bfe73..6c2108a980e 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadRecordNonDiscriminatedUnion2sImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadRecordNonDiscriminatedUnion2sImpl.java @@ -1,20 +1,23 @@ package type.property.additionalproperties.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.additionalproperties.SpreadRecordForNonDiscriminatedUnion2; /** @@ -48,40 +51,93 @@ public final class SpreadRecordNonDiscriminatedUnion2sImpl { } /** - * The interface defining all the services for AdditionalPropertiesClientSpreadRecordNonDiscriminatedUnion2s to be - * used by the proxy service to perform REST calls. + * The HTTP operations for AdditionalPropertiesClientSpreadRecordNonDiscriminatedUnion2s. */ - @ServiceInterface(name = "AdditionalPropertiesClientSpreadRecordNonDiscriminatedUnion2s", host = "{endpoint}") public interface SpreadRecordNonDiscriminatedUnion2sService { static SpreadRecordNonDiscriminatedUnion2sService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName( - "type.property.additionalproperties.implementation.SpreadRecordNonDiscriminatedUnion2sServiceImpl"); - return (SpreadRecordNonDiscriminatedUnion2sService) clazz - .getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new SpreadRecordNonDiscriminatedUnion2sServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, + RequestContext requestContext); + + Response put(String endpoint, String contentType, SpreadRecordForNonDiscriminatedUnion2 body, + RequestContext requestContext); + } + + private static final class SpreadRecordNonDiscriminatedUnion2sServiceImpl + implements SpreadRecordNonDiscriminatedUnion2sService { + private static final ClientLogger HTTP_LOG = new ClientLogger(SpreadRecordNonDiscriminatedUnion2sService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private SpreadRecordNonDiscriminatedUnion2sServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/additionalProperties/spreadRecordNonDiscriminatedUnion2"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, SpreadRecordForNonDiscriminatedUnion2.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, SpreadRecordForNonDiscriminatedUnion2 body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/additionalProperties/spreadRecordNonDiscriminatedUnion2"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/additionalProperties/spreadRecordNonDiscriminatedUnion2", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/additionalProperties/spreadRecordNonDiscriminatedUnion2", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") SpreadRecordForNonDiscriminatedUnion2 body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadRecordNonDiscriminatedUnion3sImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadRecordNonDiscriminatedUnion3sImpl.java index 3f0bb9248e5..7aa8d8b0d32 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadRecordNonDiscriminatedUnion3sImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadRecordNonDiscriminatedUnion3sImpl.java @@ -1,20 +1,23 @@ package type.property.additionalproperties.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.additionalproperties.SpreadRecordForNonDiscriminatedUnion3; /** @@ -48,40 +51,93 @@ public final class SpreadRecordNonDiscriminatedUnion3sImpl { } /** - * The interface defining all the services for AdditionalPropertiesClientSpreadRecordNonDiscriminatedUnion3s to be - * used by the proxy service to perform REST calls. + * The HTTP operations for AdditionalPropertiesClientSpreadRecordNonDiscriminatedUnion3s. */ - @ServiceInterface(name = "AdditionalPropertiesClientSpreadRecordNonDiscriminatedUnion3s", host = "{endpoint}") public interface SpreadRecordNonDiscriminatedUnion3sService { static SpreadRecordNonDiscriminatedUnion3sService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName( - "type.property.additionalproperties.implementation.SpreadRecordNonDiscriminatedUnion3sServiceImpl"); - return (SpreadRecordNonDiscriminatedUnion3sService) clazz - .getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new SpreadRecordNonDiscriminatedUnion3sServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, + RequestContext requestContext); + + Response put(String endpoint, String contentType, SpreadRecordForNonDiscriminatedUnion3 body, + RequestContext requestContext); + } + + private static final class SpreadRecordNonDiscriminatedUnion3sServiceImpl + implements SpreadRecordNonDiscriminatedUnion3sService { + private static final ClientLogger HTTP_LOG = new ClientLogger(SpreadRecordNonDiscriminatedUnion3sService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private SpreadRecordNonDiscriminatedUnion3sServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/additionalProperties/spreadRecordNonDiscriminatedUnion3"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, SpreadRecordForNonDiscriminatedUnion3.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, SpreadRecordForNonDiscriminatedUnion3 body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/additionalProperties/spreadRecordNonDiscriminatedUnion3"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/additionalProperties/spreadRecordNonDiscriminatedUnion3", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/additionalProperties/spreadRecordNonDiscriminatedUnion3", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") SpreadRecordForNonDiscriminatedUnion3 body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadRecordNonDiscriminatedUnionsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadRecordNonDiscriminatedUnionsImpl.java index 5f3015d163a..22f01d1a67a 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadRecordNonDiscriminatedUnionsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadRecordNonDiscriminatedUnionsImpl.java @@ -1,20 +1,23 @@ package type.property.additionalproperties.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.additionalproperties.SpreadRecordForNonDiscriminatedUnion; /** @@ -48,39 +51,93 @@ public final class SpreadRecordNonDiscriminatedUnionsImpl { } /** - * The interface defining all the services for AdditionalPropertiesClientSpreadRecordNonDiscriminatedUnions to be - * used by the proxy service to perform REST calls. + * The HTTP operations for AdditionalPropertiesClientSpreadRecordNonDiscriminatedUnions. */ - @ServiceInterface(name = "AdditionalPropertiesClientSpreadRecordNonDiscriminatedUnions", host = "{endpoint}") public interface SpreadRecordNonDiscriminatedUnionsService { static SpreadRecordNonDiscriminatedUnionsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName( - "type.property.additionalproperties.implementation.SpreadRecordNonDiscriminatedUnionsServiceImpl"); - return (SpreadRecordNonDiscriminatedUnionsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new SpreadRecordNonDiscriminatedUnionsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, + RequestContext requestContext); + + Response put(String endpoint, String contentType, SpreadRecordForNonDiscriminatedUnion body, + RequestContext requestContext); + } + + private static final class SpreadRecordNonDiscriminatedUnionsServiceImpl + implements SpreadRecordNonDiscriminatedUnionsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(SpreadRecordNonDiscriminatedUnionsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private SpreadRecordNonDiscriminatedUnionsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/additionalProperties/spreadRecordNonDiscriminatedUnion"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, SpreadRecordForNonDiscriminatedUnion.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, SpreadRecordForNonDiscriminatedUnion body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/additionalProperties/spreadRecordNonDiscriminatedUnion"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/additionalProperties/spreadRecordNonDiscriminatedUnion", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/additionalProperties/spreadRecordNonDiscriminatedUnion", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") SpreadRecordForNonDiscriminatedUnion body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadRecordUnionsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadRecordUnionsImpl.java index 6d97cdfed97..7a34dae3583 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadRecordUnionsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadRecordUnionsImpl.java @@ -1,20 +1,23 @@ package type.property.additionalproperties.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.additionalproperties.SpreadRecordForUnion; /** @@ -48,39 +51,90 @@ public final class SpreadRecordUnionsImpl { } /** - * The interface defining all the services for AdditionalPropertiesClientSpreadRecordUnions to be used by the proxy - * service to perform REST calls. + * The HTTP operations for AdditionalPropertiesClientSpreadRecordUnions. */ - @ServiceInterface(name = "AdditionalPropertiesClientSpreadRecordUnions", host = "{endpoint}") public interface SpreadRecordUnionsService { static SpreadRecordUnionsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz - = Class.forName("type.property.additionalproperties.implementation.SpreadRecordUnionsServiceImpl"); - return (SpreadRecordUnionsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new SpreadRecordUnionsServiceImpl(pipeline); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/additionalProperties/spreadRecordUnion", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, SpreadRecordForUnion body, RequestContext requestContext); + } + + private static final class SpreadRecordUnionsServiceImpl implements SpreadRecordUnionsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(SpreadRecordUnionsService.class); + + private final HttpPipeline httpPipeline; - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/additionalProperties/spreadRecordUnion", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") SpreadRecordForUnion body, RequestContext requestContext); + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private SpreadRecordUnionsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/additionalProperties/spreadRecordUnion"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, SpreadRecordForUnion.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response put(String endpoint, String contentType, SpreadRecordForUnion body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/additionalProperties/spreadRecordUnion"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadStringsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadStringsImpl.java index b805273f36b..643cde06a34 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadStringsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/additionalproperties/implementation/SpreadStringsImpl.java @@ -1,20 +1,23 @@ package type.property.additionalproperties.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.additionalproperties.SpreadStringRecord; /** @@ -48,39 +51,89 @@ public final class SpreadStringsImpl { } /** - * The interface defining all the services for AdditionalPropertiesClientSpreadStrings to be used by the proxy - * service to perform REST calls. + * The HTTP operations for AdditionalPropertiesClientSpreadStrings. */ - @ServiceInterface(name = "AdditionalPropertiesClientSpreadStrings", host = "{endpoint}") public interface SpreadStringsService { static SpreadStringsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz - = Class.forName("type.property.additionalproperties.implementation.SpreadStringsServiceImpl"); - return (SpreadStringsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new SpreadStringsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, SpreadStringRecord body, RequestContext requestContext); + } + + private static final class SpreadStringsServiceImpl implements SpreadStringsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(SpreadStringsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private SpreadStringsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/additionalProperties/spreadRecordString"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, SpreadStringRecord.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, SpreadStringRecord body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/additionalProperties/spreadRecordString"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/additionalProperties/spreadRecordString", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/additionalProperties/spreadRecordString", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") SpreadStringRecord body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/nullable/implementation/BytesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/nullable/implementation/BytesImpl.java index 8889a532054..2c9a0bc5ad1 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/nullable/implementation/BytesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/nullable/implementation/BytesImpl.java @@ -1,20 +1,23 @@ package type.property.nullable.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.nullable.BytesProperty; /** @@ -48,55 +51,142 @@ public final class BytesImpl { } /** - * The interface defining all the services for NullableClientBytes to be used by the proxy service to perform REST - * calls. + * The HTTP operations for NullableClientBytes. */ - @ServiceInterface(name = "NullableClientBytes", host = "{endpoint}") public interface BytesService { static BytesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.nullable.implementation.BytesServiceImpl"); - return (BytesService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new BytesServiceImpl(pipeline); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/nullable/bytes/non-null", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getNonNull(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, + Response getNonNull(String endpoint, String accept, RequestContext requestContext); + + Response getNull(String endpoint, String accept, RequestContext requestContext); + + Response patchNonNull(String endpoint, String contentType, BytesProperty body, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/nullable/bytes/null", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getNull(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, + Response patchNull(String endpoint, String contentType, BytesProperty body, RequestContext requestContext); + } + + private static final class BytesServiceImpl implements BytesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(BytesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private BytesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response getNonNull(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/nullable/bytes/non-null"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, BytesProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } - @HttpRequestInformation( - method = HttpMethod.PATCH, - path = "/type/property/nullable/bytes/non-null", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response patchNonNull(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, - @BodyParam("application/merge-patch+json") BytesProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PATCH, - path = "/type/property/nullable/bytes/null", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response patchNull(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, - @BodyParam("application/merge-patch+json") BytesProperty body, RequestContext requestContext); + @Override + public Response getNull(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/property/nullable/bytes/null"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, BytesProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response patchNonNull(String endpoint, String contentType, BytesProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PATCH) + .setUri(endpoint + "/type/property/nullable/bytes/non-null"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 patchNull(String endpoint, String contentType, BytesProperty body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PATCH).setUri(endpoint + "/type/property/nullable/bytes/null"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/nullable/implementation/CollectionsBytesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/nullable/implementation/CollectionsBytesImpl.java index 954538bddaf..6911f3bbdde 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/nullable/implementation/CollectionsBytesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/nullable/implementation/CollectionsBytesImpl.java @@ -1,20 +1,23 @@ package type.property.nullable.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.nullable.CollectionsByteProperty; /** @@ -48,56 +51,144 @@ public final class CollectionsBytesImpl { } /** - * The interface defining all the services for NullableClientCollectionsBytes to be used by the proxy service to - * perform REST calls. + * The HTTP operations for NullableClientCollectionsBytes. */ - @ServiceInterface(name = "NullableClientCollectionsBytes", host = "{endpoint}") public interface CollectionsBytesService { static CollectionsBytesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.nullable.implementation.CollectionsBytesServiceImpl"); - return (CollectionsBytesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new CollectionsBytesServiceImpl(pipeline); + } + + Response getNonNull(String endpoint, String accept, RequestContext requestContext); + + Response getNull(String endpoint, String accept, RequestContext requestContext); + + Response patchNonNull(String endpoint, String contentType, CollectionsByteProperty body, + RequestContext requestContext); + + Response patchNull(String endpoint, String contentType, CollectionsByteProperty body, + RequestContext requestContext); + } + + private static final class CollectionsBytesServiceImpl implements CollectionsBytesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(CollectionsBytesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private CollectionsBytesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response getNonNull(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/nullable/collections/bytes/non-null"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, CollectionsByteProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response getNull(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/nullable/collections/bytes/null"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, CollectionsByteProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response patchNonNull(String endpoint, String contentType, CollectionsByteProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PATCH) + .setUri(endpoint + "/type/property/nullable/collections/bytes/non-null"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 patchNull(String endpoint, String contentType, CollectionsByteProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PATCH) + .setUri(endpoint + "/type/property/nullable/collections/bytes/null"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/nullable/collections/bytes/non-null", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getNonNull(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/nullable/collections/bytes/null", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getNull(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PATCH, - path = "/type/property/nullable/collections/bytes/non-null", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response patchNonNull(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, - @BodyParam("application/merge-patch+json") CollectionsByteProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PATCH, - path = "/type/property/nullable/collections/bytes/null", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response patchNull(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, - @BodyParam("application/merge-patch+json") CollectionsByteProperty body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/nullable/implementation/CollectionsModelsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/nullable/implementation/CollectionsModelsImpl.java index dc067619d5f..959981db623 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/nullable/implementation/CollectionsModelsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/nullable/implementation/CollectionsModelsImpl.java @@ -1,20 +1,23 @@ package type.property.nullable.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.nullable.CollectionsModelProperty; /** @@ -48,56 +51,144 @@ public final class CollectionsModelsImpl { } /** - * The interface defining all the services for NullableClientCollectionsModels to be used by the proxy service to - * perform REST calls. + * The HTTP operations for NullableClientCollectionsModels. */ - @ServiceInterface(name = "NullableClientCollectionsModels", host = "{endpoint}") public interface CollectionsModelsService { static CollectionsModelsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.nullable.implementation.CollectionsModelsServiceImpl"); - return (CollectionsModelsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new CollectionsModelsServiceImpl(pipeline); + } + + Response getNonNull(String endpoint, String accept, RequestContext requestContext); + + Response getNull(String endpoint, String accept, RequestContext requestContext); + + Response patchNonNull(String endpoint, String contentType, CollectionsModelProperty body, + RequestContext requestContext); + + Response patchNull(String endpoint, String contentType, CollectionsModelProperty body, + RequestContext requestContext); + } + + private static final class CollectionsModelsServiceImpl implements CollectionsModelsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(CollectionsModelsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private CollectionsModelsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response getNonNull(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/nullable/collections/model/non-null"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, CollectionsModelProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response getNull(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/nullable/collections/model/null"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, CollectionsModelProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response patchNonNull(String endpoint, String contentType, CollectionsModelProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PATCH) + .setUri(endpoint + "/type/property/nullable/collections/model/non-null"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 patchNull(String endpoint, String contentType, CollectionsModelProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PATCH) + .setUri(endpoint + "/type/property/nullable/collections/model/null"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/nullable/collections/model/non-null", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getNonNull(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/nullable/collections/model/null", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getNull(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PATCH, - path = "/type/property/nullable/collections/model/non-null", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response patchNonNull(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, - @BodyParam("application/merge-patch+json") CollectionsModelProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PATCH, - path = "/type/property/nullable/collections/model/null", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response patchNull(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, - @BodyParam("application/merge-patch+json") CollectionsModelProperty body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/nullable/implementation/CollectionsStringsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/nullable/implementation/CollectionsStringsImpl.java index 34699d37567..9b438cf5176 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/nullable/implementation/CollectionsStringsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/nullable/implementation/CollectionsStringsImpl.java @@ -1,20 +1,23 @@ package type.property.nullable.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.nullable.CollectionsStringProperty; /** @@ -48,56 +51,144 @@ public final class CollectionsStringsImpl { } /** - * The interface defining all the services for NullableClientCollectionsStrings to be used by the proxy service to - * perform REST calls. + * The HTTP operations for NullableClientCollectionsStrings. */ - @ServiceInterface(name = "NullableClientCollectionsStrings", host = "{endpoint}") public interface CollectionsStringsService { static CollectionsStringsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.nullable.implementation.CollectionsStringsServiceImpl"); - return (CollectionsStringsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new CollectionsStringsServiceImpl(pipeline); + } + + Response getNonNull(String endpoint, String accept, RequestContext requestContext); + + Response getNull(String endpoint, String accept, RequestContext requestContext); + + Response patchNonNull(String endpoint, String contentType, CollectionsStringProperty body, + RequestContext requestContext); + + Response patchNull(String endpoint, String contentType, CollectionsStringProperty body, + RequestContext requestContext); + } + + private static final class CollectionsStringsServiceImpl implements CollectionsStringsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(CollectionsStringsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private CollectionsStringsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response getNonNull(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/nullable/collections/string/non-null"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, CollectionsStringProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response getNull(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/nullable/collections/string/null"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, CollectionsStringProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response patchNonNull(String endpoint, String contentType, CollectionsStringProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PATCH) + .setUri(endpoint + "/type/property/nullable/collections/string/non-null"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 patchNull(String endpoint, String contentType, CollectionsStringProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PATCH) + .setUri(endpoint + "/type/property/nullable/collections/string/null"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/nullable/collections/string/non-null", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getNonNull(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/nullable/collections/string/null", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getNull(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PATCH, - path = "/type/property/nullable/collections/string/non-null", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response patchNonNull(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, - @BodyParam("application/merge-patch+json") CollectionsStringProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PATCH, - path = "/type/property/nullable/collections/string/null", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response patchNull(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, - @BodyParam("application/merge-patch+json") CollectionsStringProperty body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/nullable/implementation/DatetimeOperationsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/nullable/implementation/DatetimeOperationsImpl.java index 26010016d0d..c944d86a52a 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/nullable/implementation/DatetimeOperationsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/nullable/implementation/DatetimeOperationsImpl.java @@ -1,20 +1,23 @@ package type.property.nullable.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.nullable.DatetimeProperty; /** @@ -48,56 +51,142 @@ public final class DatetimeOperationsImpl { } /** - * The interface defining all the services for NullableClientDatetimeOperations to be used by the proxy service to - * perform REST calls. + * The HTTP operations for NullableClientDatetimeOperations. */ - @ServiceInterface(name = "NullableClientDatetimeOperations", host = "{endpoint}") public interface DatetimeOperationsService { static DatetimeOperationsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.nullable.implementation.DatetimeOperationsServiceImpl"); - return (DatetimeOperationsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new DatetimeOperationsServiceImpl(pipeline); + } + + Response getNonNull(String endpoint, String accept, RequestContext requestContext); + + Response getNull(String endpoint, String accept, RequestContext requestContext); + + Response patchNonNull(String endpoint, String contentType, DatetimeProperty body, + RequestContext requestContext); + + Response patchNull(String endpoint, String contentType, DatetimeProperty body, + RequestContext requestContext); + } + + private static final class DatetimeOperationsServiceImpl implements DatetimeOperationsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(DatetimeOperationsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private DatetimeOperationsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response getNonNull(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/nullable/datetime/non-null"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, DatetimeProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response getNull(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/nullable/datetime/null"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, DatetimeProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/nullable/datetime/non-null", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getNonNull(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/nullable/datetime/null", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getNull(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); + @Override + public Response patchNonNull(String endpoint, String contentType, DatetimeProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PATCH) + .setUri(endpoint + "/type/property/nullable/datetime/non-null"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } - @HttpRequestInformation( - method = HttpMethod.PATCH, - path = "/type/property/nullable/datetime/non-null", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response patchNonNull(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, - @BodyParam("application/merge-patch+json") DatetimeProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PATCH, - path = "/type/property/nullable/datetime/null", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response patchNull(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, - @BodyParam("application/merge-patch+json") DatetimeProperty body, RequestContext requestContext); + @Override + public Response patchNull(String endpoint, String contentType, DatetimeProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PATCH) + .setUri(endpoint + "/type/property/nullable/datetime/null"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/nullable/implementation/DurationOperationsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/nullable/implementation/DurationOperationsImpl.java index 5376f2b94fc..a92fa78fdce 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/nullable/implementation/DurationOperationsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/nullable/implementation/DurationOperationsImpl.java @@ -1,20 +1,23 @@ package type.property.nullable.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.nullable.DurationProperty; /** @@ -48,56 +51,142 @@ public final class DurationOperationsImpl { } /** - * The interface defining all the services for NullableClientDurationOperations to be used by the proxy service to - * perform REST calls. + * The HTTP operations for NullableClientDurationOperations. */ - @ServiceInterface(name = "NullableClientDurationOperations", host = "{endpoint}") public interface DurationOperationsService { static DurationOperationsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.nullable.implementation.DurationOperationsServiceImpl"); - return (DurationOperationsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new DurationOperationsServiceImpl(pipeline); + } + + Response getNonNull(String endpoint, String accept, RequestContext requestContext); + + Response getNull(String endpoint, String accept, RequestContext requestContext); + + Response patchNonNull(String endpoint, String contentType, DurationProperty body, + RequestContext requestContext); + + Response patchNull(String endpoint, String contentType, DurationProperty body, + RequestContext requestContext); + } + + private static final class DurationOperationsServiceImpl implements DurationOperationsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(DurationOperationsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private DurationOperationsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response getNonNull(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/nullable/duration/non-null"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, DurationProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response getNull(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/nullable/duration/null"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, DurationProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/nullable/duration/non-null", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getNonNull(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/nullable/duration/null", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getNull(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); + @Override + public Response patchNonNull(String endpoint, String contentType, DurationProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PATCH) + .setUri(endpoint + "/type/property/nullable/duration/non-null"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } - @HttpRequestInformation( - method = HttpMethod.PATCH, - path = "/type/property/nullable/duration/non-null", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response patchNonNull(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, - @BodyParam("application/merge-patch+json") DurationProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PATCH, - path = "/type/property/nullable/duration/null", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response patchNull(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, - @BodyParam("application/merge-patch+json") DurationProperty body, RequestContext requestContext); + @Override + public Response patchNull(String endpoint, String contentType, DurationProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PATCH) + .setUri(endpoint + "/type/property/nullable/duration/null"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/nullable/implementation/StringOperationsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/nullable/implementation/StringOperationsImpl.java index a94049cec16..26e4615bb26 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/nullable/implementation/StringOperationsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/nullable/implementation/StringOperationsImpl.java @@ -1,20 +1,23 @@ package type.property.nullable.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.nullable.StringProperty; /** @@ -48,56 +51,142 @@ public final class StringOperationsImpl { } /** - * The interface defining all the services for NullableClientStringOperations to be used by the proxy service to - * perform REST calls. + * The HTTP operations for NullableClientStringOperations. */ - @ServiceInterface(name = "NullableClientStringOperations", host = "{endpoint}") public interface StringOperationsService { static StringOperationsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.nullable.implementation.StringOperationsServiceImpl"); - return (StringOperationsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new StringOperationsServiceImpl(pipeline); + } + + Response getNonNull(String endpoint, String accept, RequestContext requestContext); + + Response getNull(String endpoint, String accept, RequestContext requestContext); + + Response patchNonNull(String endpoint, String contentType, StringProperty body, + RequestContext requestContext); + + Response patchNull(String endpoint, String contentType, StringProperty body, + RequestContext requestContext); + } + + private static final class StringOperationsServiceImpl implements StringOperationsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(StringOperationsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private StringOperationsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response getNonNull(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/nullable/string/non-null"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, StringProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response getNull(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/property/nullable/string/null"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, StringProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/nullable/string/non-null", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getNonNull(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/nullable/string/null", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getNull(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); + @Override + public Response patchNonNull(String endpoint, String contentType, StringProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PATCH) + .setUri(endpoint + "/type/property/nullable/string/non-null"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } - @HttpRequestInformation( - method = HttpMethod.PATCH, - path = "/type/property/nullable/string/non-null", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response patchNonNull(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, - @BodyParam("application/merge-patch+json") StringProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PATCH, - path = "/type/property/nullable/string/null", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response patchNull(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, - @BodyParam("application/merge-patch+json") StringProperty body, RequestContext requestContext); + @Override + public Response patchNull(String endpoint, String contentType, StringProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PATCH) + .setUri(endpoint + "/type/property/nullable/string/null"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/BooleanLiteralsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/BooleanLiteralsImpl.java index a048000b385..bee322c88dd 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/BooleanLiteralsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/BooleanLiteralsImpl.java @@ -1,20 +1,23 @@ package type.property.optional.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.optional.BooleanLiteralProperty; /** @@ -48,55 +51,143 @@ public final class BooleanLiteralsImpl { } /** - * The interface defining all the services for OptionalClientBooleanLiterals to be used by the proxy service to - * perform REST calls. + * The HTTP operations for OptionalClientBooleanLiterals. */ - @ServiceInterface(name = "OptionalClientBooleanLiterals", host = "{endpoint}") public interface BooleanLiteralsService { static BooleanLiteralsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.optional.implementation.BooleanLiteralsServiceImpl"); - return (BooleanLiteralsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new BooleanLiteralsServiceImpl(pipeline); + } + + Response getAll(String endpoint, String accept, RequestContext requestContext); + + Response getDefault(String endpoint, String accept, RequestContext requestContext); + + Response putAll(String endpoint, String contentType, BooleanLiteralProperty body, + RequestContext requestContext); + + Response putDefault(String endpoint, String contentType, BooleanLiteralProperty body, + RequestContext requestContext); + } + + private static final class BooleanLiteralsServiceImpl implements BooleanLiteralsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(BooleanLiteralsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private BooleanLiteralsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response getAll(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/optional/boolean/literal/all"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, BooleanLiteralProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response getDefault(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/optional/boolean/literal/default"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, BooleanLiteralProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/optional/boolean/literal/all", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getAll(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/optional/boolean/literal/default", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getDefault(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/optional/boolean/literal/all", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putAll(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") BooleanLiteralProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/optional/boolean/literal/default", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putDefault(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") BooleanLiteralProperty body, - RequestContext requestContext); + @Override + public Response putAll(String endpoint, String contentType, BooleanLiteralProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/optional/boolean/literal/all"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 putDefault(String endpoint, String contentType, BooleanLiteralProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/optional/boolean/literal/default"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/BytesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/BytesImpl.java index 2ff109a6b4a..a4b0ef90fea 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/BytesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/BytesImpl.java @@ -1,20 +1,23 @@ package type.property.optional.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.optional.BytesProperty; /** @@ -48,54 +51,141 @@ public final class BytesImpl { } /** - * The interface defining all the services for OptionalClientBytes to be used by the proxy service to perform REST - * calls. + * The HTTP operations for OptionalClientBytes. */ - @ServiceInterface(name = "OptionalClientBytes", host = "{endpoint}") public interface BytesService { static BytesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.optional.implementation.BytesServiceImpl"); - return (BytesService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new BytesServiceImpl(pipeline); + } + + Response getAll(String endpoint, String accept, RequestContext requestContext); + + Response getDefault(String endpoint, String accept, RequestContext requestContext); + + Response putAll(String endpoint, String contentType, BytesProperty body, RequestContext requestContext); + + Response putDefault(String endpoint, String contentType, BytesProperty body, + RequestContext requestContext); + } + + private static final class BytesServiceImpl implements BytesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(BytesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private BytesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response getAll(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/property/optional/bytes/all"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, BytesProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response getDefault(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/optional/bytes/default"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, BytesProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/optional/bytes/all", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getAll(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); + @Override + public Response putAll(String endpoint, String contentType, BytesProperty body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/property/optional/bytes/all"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/optional/bytes/default", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getDefault(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); + @Override + public Response putDefault(String endpoint, String contentType, BytesProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/optional/bytes/default"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/optional/bytes/all", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putAll(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") BytesProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/optional/bytes/default", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putDefault(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") BytesProperty body, - RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/CollectionsBytesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/CollectionsBytesImpl.java index 48b4db75673..962420aebde 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/CollectionsBytesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/CollectionsBytesImpl.java @@ -1,20 +1,23 @@ package type.property.optional.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.optional.CollectionsByteProperty; /** @@ -48,55 +51,143 @@ public final class CollectionsBytesImpl { } /** - * The interface defining all the services for OptionalClientCollectionsBytes to be used by the proxy service to - * perform REST calls. + * The HTTP operations for OptionalClientCollectionsBytes. */ - @ServiceInterface(name = "OptionalClientCollectionsBytes", host = "{endpoint}") public interface CollectionsBytesService { static CollectionsBytesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.optional.implementation.CollectionsBytesServiceImpl"); - return (CollectionsBytesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new CollectionsBytesServiceImpl(pipeline); + } + + Response getAll(String endpoint, String accept, RequestContext requestContext); + + Response getDefault(String endpoint, String accept, RequestContext requestContext); + + Response putAll(String endpoint, String contentType, CollectionsByteProperty body, + RequestContext requestContext); + + Response putDefault(String endpoint, String contentType, CollectionsByteProperty body, + RequestContext requestContext); + } + + private static final class CollectionsBytesServiceImpl implements CollectionsBytesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(CollectionsBytesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private CollectionsBytesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response getAll(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/optional/collections/bytes/all"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, CollectionsByteProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response getDefault(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/optional/collections/bytes/default"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, CollectionsByteProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response putAll(String endpoint, String contentType, CollectionsByteProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/optional/collections/bytes/all"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 putDefault(String endpoint, String contentType, CollectionsByteProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/optional/collections/bytes/default"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/optional/collections/bytes/all", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getAll(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/optional/collections/bytes/default", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getDefault(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/optional/collections/bytes/all", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putAll(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") CollectionsByteProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/optional/collections/bytes/default", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putDefault(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") CollectionsByteProperty body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/CollectionsModelsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/CollectionsModelsImpl.java index 9f8c070390f..5ef524c311f 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/CollectionsModelsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/CollectionsModelsImpl.java @@ -1,20 +1,23 @@ package type.property.optional.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.optional.CollectionsModelProperty; /** @@ -48,55 +51,144 @@ public final class CollectionsModelsImpl { } /** - * The interface defining all the services for OptionalClientCollectionsModels to be used by the proxy service to - * perform REST calls. + * The HTTP operations for OptionalClientCollectionsModels. */ - @ServiceInterface(name = "OptionalClientCollectionsModels", host = "{endpoint}") public interface CollectionsModelsService { static CollectionsModelsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.optional.implementation.CollectionsModelsServiceImpl"); - return (CollectionsModelsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new CollectionsModelsServiceImpl(pipeline); + } + + Response getAll(String endpoint, String accept, RequestContext requestContext); + + Response getDefault(String endpoint, String accept, RequestContext requestContext); + + Response putAll(String endpoint, String contentType, CollectionsModelProperty body, + RequestContext requestContext); + + Response putDefault(String endpoint, String contentType, CollectionsModelProperty body, + RequestContext requestContext); + } + + private static final class CollectionsModelsServiceImpl implements CollectionsModelsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(CollectionsModelsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private CollectionsModelsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response getAll(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/optional/collections/model/all"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, CollectionsModelProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response getDefault(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/optional/collections/model/default"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, CollectionsModelProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response putAll(String endpoint, String contentType, CollectionsModelProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/optional/collections/model/all"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 putDefault(String endpoint, String contentType, CollectionsModelProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/optional/collections/model/default"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/optional/collections/model/all", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getAll(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/optional/collections/model/default", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getDefault(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/optional/collections/model/all", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putAll(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") CollectionsModelProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/optional/collections/model/default", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putDefault(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") CollectionsModelProperty body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/DatetimeOperationsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/DatetimeOperationsImpl.java index f316c8bcd5f..6522c86c56d 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/DatetimeOperationsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/DatetimeOperationsImpl.java @@ -1,20 +1,23 @@ package type.property.optional.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.optional.DatetimeProperty; /** @@ -48,57 +51,144 @@ public final class DatetimeOperationsImpl { } /** - * The interface defining all the services for OptionalClientDatetimeOperations to be used by the proxy service to - * perform REST calls. + * The HTTP operations for OptionalClientDatetimeOperations. */ - @ServiceInterface(name = "OptionalClientDatetimeOperations", host = "{endpoint}") public interface DatetimeOperationsService { static DatetimeOperationsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.optional.implementation.DatetimeOperationsServiceImpl"); - return (DatetimeOperationsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new DatetimeOperationsServiceImpl(pipeline); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/optional/datetime/all", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getAll(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, + Response getAll(String endpoint, String accept, RequestContext requestContext); + + Response getDefault(String endpoint, String accept, RequestContext requestContext); + + Response putAll(String endpoint, String contentType, DatetimeProperty body, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/optional/datetime/default", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getDefault(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/optional/datetime/all", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putAll(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") DatetimeProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/optional/datetime/default", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putDefault(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") DatetimeProperty body, + Response putDefault(String endpoint, String contentType, DatetimeProperty body, RequestContext requestContext); } + private static final class DatetimeOperationsServiceImpl implements DatetimeOperationsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(DatetimeOperationsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private DatetimeOperationsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response getAll(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/property/optional/datetime/all"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, DatetimeProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response getDefault(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/optional/datetime/default"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, DatetimeProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response putAll(String endpoint, String contentType, DatetimeProperty body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/property/optional/datetime/all"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 putDefault(String endpoint, String contentType, DatetimeProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/optional/datetime/default"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } + } + /** * Get models that will return all properties in the model. * diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/DurationOperationsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/DurationOperationsImpl.java index 4e22630336d..4f01da48433 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/DurationOperationsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/DurationOperationsImpl.java @@ -1,20 +1,23 @@ package type.property.optional.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.optional.DurationProperty; /** @@ -48,57 +51,144 @@ public final class DurationOperationsImpl { } /** - * The interface defining all the services for OptionalClientDurationOperations to be used by the proxy service to - * perform REST calls. + * The HTTP operations for OptionalClientDurationOperations. */ - @ServiceInterface(name = "OptionalClientDurationOperations", host = "{endpoint}") public interface DurationOperationsService { static DurationOperationsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.optional.implementation.DurationOperationsServiceImpl"); - return (DurationOperationsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new DurationOperationsServiceImpl(pipeline); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/optional/duration/all", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getAll(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, + Response getAll(String endpoint, String accept, RequestContext requestContext); + + Response getDefault(String endpoint, String accept, RequestContext requestContext); + + Response putAll(String endpoint, String contentType, DurationProperty body, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/optional/duration/default", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getDefault(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/optional/duration/all", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putAll(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") DurationProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/optional/duration/default", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putDefault(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") DurationProperty body, + Response putDefault(String endpoint, String contentType, DurationProperty body, RequestContext requestContext); } + private static final class DurationOperationsServiceImpl implements DurationOperationsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(DurationOperationsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private DurationOperationsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response getAll(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/property/optional/duration/all"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, DurationProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response getDefault(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/optional/duration/default"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, DurationProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response putAll(String endpoint, String contentType, DurationProperty body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/property/optional/duration/all"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 putDefault(String endpoint, String contentType, DurationProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/optional/duration/default"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } + } + /** * Get models that will return all properties in the model. * diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/FloatLiteralsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/FloatLiteralsImpl.java index 0337bbe550a..8990bcd2099 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/FloatLiteralsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/FloatLiteralsImpl.java @@ -1,20 +1,23 @@ package type.property.optional.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.optional.FloatLiteralProperty; /** @@ -48,55 +51,143 @@ public final class FloatLiteralsImpl { } /** - * The interface defining all the services for OptionalClientFloatLiterals to be used by the proxy service to - * perform REST calls. + * The HTTP operations for OptionalClientFloatLiterals. */ - @ServiceInterface(name = "OptionalClientFloatLiterals", host = "{endpoint}") public interface FloatLiteralsService { static FloatLiteralsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.optional.implementation.FloatLiteralsServiceImpl"); - return (FloatLiteralsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new FloatLiteralsServiceImpl(pipeline); + } + + Response getAll(String endpoint, String accept, RequestContext requestContext); + + Response getDefault(String endpoint, String accept, RequestContext requestContext); + + Response putAll(String endpoint, String contentType, FloatLiteralProperty body, + RequestContext requestContext); + + Response putDefault(String endpoint, String contentType, FloatLiteralProperty body, + RequestContext requestContext); + } + + private static final class FloatLiteralsServiceImpl implements FloatLiteralsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(FloatLiteralsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private FloatLiteralsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response getAll(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/optional/float/literal/all"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, FloatLiteralProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response getDefault(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/optional/float/literal/default"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, FloatLiteralProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/optional/float/literal/all", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getAll(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/optional/float/literal/default", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getDefault(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/optional/float/literal/all", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putAll(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") FloatLiteralProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/optional/float/literal/default", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putDefault(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") FloatLiteralProperty body, - RequestContext requestContext); + @Override + public Response putAll(String endpoint, String contentType, FloatLiteralProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/optional/float/literal/all"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 putDefault(String endpoint, String contentType, FloatLiteralProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/optional/float/literal/default"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/IntLiteralsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/IntLiteralsImpl.java index 2d92ca9fc54..7e7fe939bc4 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/IntLiteralsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/IntLiteralsImpl.java @@ -1,20 +1,23 @@ package type.property.optional.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.optional.IntLiteralProperty; /** @@ -48,55 +51,142 @@ public final class IntLiteralsImpl { } /** - * The interface defining all the services for OptionalClientIntLiterals to be used by the proxy service to perform - * REST calls. + * The HTTP operations for OptionalClientIntLiterals. */ - @ServiceInterface(name = "OptionalClientIntLiterals", host = "{endpoint}") public interface IntLiteralsService { static IntLiteralsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.optional.implementation.IntLiteralsServiceImpl"); - return (IntLiteralsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new IntLiteralsServiceImpl(pipeline); + } + + Response getAll(String endpoint, String accept, RequestContext requestContext); + + Response getDefault(String endpoint, String accept, RequestContext requestContext); + + Response putAll(String endpoint, String contentType, IntLiteralProperty body, + RequestContext requestContext); + + Response putDefault(String endpoint, String contentType, IntLiteralProperty body, + RequestContext requestContext); + } + + private static final class IntLiteralsServiceImpl implements IntLiteralsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(IntLiteralsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private IntLiteralsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response getAll(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/optional/int/literal/all"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, IntLiteralProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response getDefault(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/optional/int/literal/default"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, IntLiteralProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/optional/int/literal/all", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getAll(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/optional/int/literal/default", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getDefault(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/optional/int/literal/all", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putAll(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") IntLiteralProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/optional/int/literal/default", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putDefault(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") IntLiteralProperty body, - RequestContext requestContext); + @Override + public Response putAll(String endpoint, String contentType, IntLiteralProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/optional/int/literal/all"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 putDefault(String endpoint, String contentType, IntLiteralProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/optional/int/literal/default"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/PlainDatesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/PlainDatesImpl.java index 5b3bcb1f620..f77aa8570bc 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/PlainDatesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/PlainDatesImpl.java @@ -1,20 +1,23 @@ package type.property.optional.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.optional.PlainDateProperty; /** @@ -48,56 +51,144 @@ public final class PlainDatesImpl { } /** - * The interface defining all the services for OptionalClientPlainDates to be used by the proxy service to perform - * REST calls. + * The HTTP operations for OptionalClientPlainDates. */ - @ServiceInterface(name = "OptionalClientPlainDates", host = "{endpoint}") public interface PlainDatesService { static PlainDatesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.optional.implementation.PlainDatesServiceImpl"); - return (PlainDatesService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new PlainDatesServiceImpl(pipeline); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/optional/plainDate/all", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getAll(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, + Response getAll(String endpoint, String accept, RequestContext requestContext); + + Response getDefault(String endpoint, String accept, RequestContext requestContext); + + Response putAll(String endpoint, String contentType, PlainDateProperty body, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/optional/plainDate/default", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getDefault(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/optional/plainDate/all", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putAll(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") PlainDateProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/optional/plainDate/default", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putDefault(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") PlainDateProperty body, + Response putDefault(String endpoint, String contentType, PlainDateProperty body, RequestContext requestContext); } + private static final class PlainDatesServiceImpl implements PlainDatesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(PlainDatesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private PlainDatesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response getAll(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/optional/plainDate/all"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, PlainDateProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response getDefault(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/optional/plainDate/default"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, PlainDateProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response putAll(String endpoint, String contentType, PlainDateProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/optional/plainDate/all"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 putDefault(String endpoint, String contentType, PlainDateProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/optional/plainDate/default"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } + } + /** * Get models that will return all properties in the model. * diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/PlainTimesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/PlainTimesImpl.java index 991d29878e3..c9e75a953ea 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/PlainTimesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/PlainTimesImpl.java @@ -1,20 +1,23 @@ package type.property.optional.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.optional.PlainTimeProperty; /** @@ -48,56 +51,144 @@ public final class PlainTimesImpl { } /** - * The interface defining all the services for OptionalClientPlainTimes to be used by the proxy service to perform - * REST calls. + * The HTTP operations for OptionalClientPlainTimes. */ - @ServiceInterface(name = "OptionalClientPlainTimes", host = "{endpoint}") public interface PlainTimesService { static PlainTimesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.optional.implementation.PlainTimesServiceImpl"); - return (PlainTimesService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new PlainTimesServiceImpl(pipeline); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/optional/plainTime/all", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getAll(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, + Response getAll(String endpoint, String accept, RequestContext requestContext); + + Response getDefault(String endpoint, String accept, RequestContext requestContext); + + Response putAll(String endpoint, String contentType, PlainTimeProperty body, RequestContext requestContext); - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/optional/plainTime/default", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getDefault(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/optional/plainTime/all", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putAll(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") PlainTimeProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/optional/plainTime/default", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putDefault(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") PlainTimeProperty body, + Response putDefault(String endpoint, String contentType, PlainTimeProperty body, RequestContext requestContext); } + private static final class PlainTimesServiceImpl implements PlainTimesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(PlainTimesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private PlainTimesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response getAll(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/optional/plainTime/all"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, PlainTimeProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response getDefault(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/optional/plainTime/default"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, PlainTimeProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response putAll(String endpoint, String contentType, PlainTimeProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/optional/plainTime/all"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 putDefault(String endpoint, String contentType, PlainTimeProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/optional/plainTime/default"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } + } + /** * Get models that will return all properties in the model. * diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/RequiredAndOptionalsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/RequiredAndOptionalsImpl.java index 6db28a20c0f..2c06ea05a68 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/RequiredAndOptionalsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/RequiredAndOptionalsImpl.java @@ -1,20 +1,23 @@ package type.property.optional.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.optional.RequiredAndOptionalProperty; /** @@ -48,55 +51,145 @@ public final class RequiredAndOptionalsImpl { } /** - * The interface defining all the services for OptionalClientRequiredAndOptionals to be used by the proxy service to - * perform REST calls. + * The HTTP operations for OptionalClientRequiredAndOptionals. */ - @ServiceInterface(name = "OptionalClientRequiredAndOptionals", host = "{endpoint}") public interface RequiredAndOptionalsService { static RequiredAndOptionalsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.optional.implementation.RequiredAndOptionalsServiceImpl"); - return (RequiredAndOptionalsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new RequiredAndOptionalsServiceImpl(pipeline); + } + + Response getAll(String endpoint, String accept, RequestContext requestContext); + + Response getRequiredOnly(String endpoint, String accept, + RequestContext requestContext); + + Response putAll(String endpoint, String contentType, RequiredAndOptionalProperty body, + RequestContext requestContext); + + Response putRequiredOnly(String endpoint, String contentType, RequiredAndOptionalProperty body, + RequestContext requestContext); + } + + private static final class RequiredAndOptionalsServiceImpl implements RequiredAndOptionalsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(RequiredAndOptionalsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private RequiredAndOptionalsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response getAll(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/optional/requiredAndOptional/all"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, RequiredAndOptionalProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response getRequiredOnly(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/optional/requiredAndOptional/requiredOnly"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, RequiredAndOptionalProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response putAll(String endpoint, String contentType, RequiredAndOptionalProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/optional/requiredAndOptional/all"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 putRequiredOnly(String endpoint, String contentType, RequiredAndOptionalProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/optional/requiredAndOptional/requiredOnly"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/optional/requiredAndOptional/all", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getAll(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/optional/requiredAndOptional/requiredOnly", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getRequiredOnly(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/optional/requiredAndOptional/all", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putAll(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") RequiredAndOptionalProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/optional/requiredAndOptional/requiredOnly", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putRequiredOnly(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") RequiredAndOptionalProperty body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/StringLiteralsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/StringLiteralsImpl.java index 1e22369d644..b6d755ef8a3 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/StringLiteralsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/StringLiteralsImpl.java @@ -1,20 +1,23 @@ package type.property.optional.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.optional.StringLiteralProperty; /** @@ -48,55 +51,143 @@ public final class StringLiteralsImpl { } /** - * The interface defining all the services for OptionalClientStringLiterals to be used by the proxy service to - * perform REST calls. + * The HTTP operations for OptionalClientStringLiterals. */ - @ServiceInterface(name = "OptionalClientStringLiterals", host = "{endpoint}") public interface StringLiteralsService { static StringLiteralsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.optional.implementation.StringLiteralsServiceImpl"); - return (StringLiteralsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new StringLiteralsServiceImpl(pipeline); + } + + Response getAll(String endpoint, String accept, RequestContext requestContext); + + Response getDefault(String endpoint, String accept, RequestContext requestContext); + + Response putAll(String endpoint, String contentType, StringLiteralProperty body, + RequestContext requestContext); + + Response putDefault(String endpoint, String contentType, StringLiteralProperty body, + RequestContext requestContext); + } + + private static final class StringLiteralsServiceImpl implements StringLiteralsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(StringLiteralsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private StringLiteralsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response getAll(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/optional/string/literal/all"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, StringLiteralProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response getDefault(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/optional/string/literal/default"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, StringLiteralProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/optional/string/literal/all", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getAll(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/optional/string/literal/default", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getDefault(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/optional/string/literal/all", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putAll(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") StringLiteralProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/optional/string/literal/default", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putDefault(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") StringLiteralProperty body, - RequestContext requestContext); + @Override + public Response putAll(String endpoint, String contentType, StringLiteralProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/optional/string/literal/all"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 putDefault(String endpoint, String contentType, StringLiteralProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/optional/string/literal/default"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/StringOperationsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/StringOperationsImpl.java index 03b9a9e0e88..8df2a3e2517 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/StringOperationsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/StringOperationsImpl.java @@ -1,20 +1,23 @@ package type.property.optional.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.optional.StringProperty; /** @@ -48,55 +51,141 @@ public final class StringOperationsImpl { } /** - * The interface defining all the services for OptionalClientStringOperations to be used by the proxy service to - * perform REST calls. + * The HTTP operations for OptionalClientStringOperations. */ - @ServiceInterface(name = "OptionalClientStringOperations", host = "{endpoint}") public interface StringOperationsService { static StringOperationsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.optional.implementation.StringOperationsServiceImpl"); - return (StringOperationsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new StringOperationsServiceImpl(pipeline); + } + + Response getAll(String endpoint, String accept, RequestContext requestContext); + + Response getDefault(String endpoint, String accept, RequestContext requestContext); + + Response putAll(String endpoint, String contentType, StringProperty body, RequestContext requestContext); + + Response putDefault(String endpoint, String contentType, StringProperty body, + RequestContext requestContext); + } + + private static final class StringOperationsServiceImpl implements StringOperationsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(StringOperationsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private StringOperationsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response getAll(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/property/optional/string/all"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, StringProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response getDefault(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/optional/string/default"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, StringProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/optional/string/all", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getAll(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); + @Override + public Response putAll(String endpoint, String contentType, StringProperty body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/property/optional/string/all"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/optional/string/default", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getDefault(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/optional/string/all", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putAll(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") StringProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/optional/string/default", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putDefault(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @BodyParam("application/json") StringProperty body, - RequestContext requestContext); + @Override + public Response putDefault(String endpoint, String contentType, StringProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/optional/string/default"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/UnionFloatLiteralsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/UnionFloatLiteralsImpl.java index 2756213a5ff..92e1d4b21d7 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/UnionFloatLiteralsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/UnionFloatLiteralsImpl.java @@ -1,20 +1,23 @@ package type.property.optional.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.optional.UnionFloatLiteralProperty; /** @@ -48,55 +51,144 @@ public final class UnionFloatLiteralsImpl { } /** - * The interface defining all the services for OptionalClientUnionFloatLiterals to be used by the proxy service to - * perform REST calls. + * The HTTP operations for OptionalClientUnionFloatLiterals. */ - @ServiceInterface(name = "OptionalClientUnionFloatLiterals", host = "{endpoint}") public interface UnionFloatLiteralsService { static UnionFloatLiteralsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.optional.implementation.UnionFloatLiteralsServiceImpl"); - return (UnionFloatLiteralsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new UnionFloatLiteralsServiceImpl(pipeline); + } + + Response getAll(String endpoint, String accept, RequestContext requestContext); + + Response getDefault(String endpoint, String accept, RequestContext requestContext); + + Response putAll(String endpoint, String contentType, UnionFloatLiteralProperty body, + RequestContext requestContext); + + Response putDefault(String endpoint, String contentType, UnionFloatLiteralProperty body, + RequestContext requestContext); + } + + private static final class UnionFloatLiteralsServiceImpl implements UnionFloatLiteralsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(UnionFloatLiteralsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private UnionFloatLiteralsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response getAll(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/optional/union/float/literal/all"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, UnionFloatLiteralProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response getDefault(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/optional/union/float/literal/default"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, UnionFloatLiteralProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response putAll(String endpoint, String contentType, UnionFloatLiteralProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/optional/union/float/literal/all"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 putDefault(String endpoint, String contentType, UnionFloatLiteralProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/optional/union/float/literal/default"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/optional/union/float/literal/all", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getAll(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/optional/union/float/literal/default", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getDefault(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/optional/union/float/literal/all", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putAll(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") UnionFloatLiteralProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/optional/union/float/literal/default", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putDefault(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") UnionFloatLiteralProperty body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/UnionIntLiteralsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/UnionIntLiteralsImpl.java index 0580989ac1a..955a79811ef 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/UnionIntLiteralsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/UnionIntLiteralsImpl.java @@ -1,20 +1,23 @@ package type.property.optional.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.optional.UnionIntLiteralProperty; /** @@ -48,55 +51,143 @@ public final class UnionIntLiteralsImpl { } /** - * The interface defining all the services for OptionalClientUnionIntLiterals to be used by the proxy service to - * perform REST calls. + * The HTTP operations for OptionalClientUnionIntLiterals. */ - @ServiceInterface(name = "OptionalClientUnionIntLiterals", host = "{endpoint}") public interface UnionIntLiteralsService { static UnionIntLiteralsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.optional.implementation.UnionIntLiteralsServiceImpl"); - return (UnionIntLiteralsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new UnionIntLiteralsServiceImpl(pipeline); + } + + Response getAll(String endpoint, String accept, RequestContext requestContext); + + Response getDefault(String endpoint, String accept, RequestContext requestContext); + + Response putAll(String endpoint, String contentType, UnionIntLiteralProperty body, + RequestContext requestContext); + + Response putDefault(String endpoint, String contentType, UnionIntLiteralProperty body, + RequestContext requestContext); + } + + private static final class UnionIntLiteralsServiceImpl implements UnionIntLiteralsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(UnionIntLiteralsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private UnionIntLiteralsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response getAll(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/optional/union/int/literal/all"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, UnionIntLiteralProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response getDefault(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/optional/union/int/literal/default"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, UnionIntLiteralProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response putAll(String endpoint, String contentType, UnionIntLiteralProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/optional/union/int/literal/all"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 putDefault(String endpoint, String contentType, UnionIntLiteralProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/optional/union/int/literal/default"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/optional/union/int/literal/all", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getAll(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/optional/union/int/literal/default", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getDefault(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/optional/union/int/literal/all", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putAll(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") UnionIntLiteralProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/optional/union/int/literal/default", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putDefault(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") UnionIntLiteralProperty body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/UnionStringLiteralsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/UnionStringLiteralsImpl.java index 6701c4186a3..f4919b0babe 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/UnionStringLiteralsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/optional/implementation/UnionStringLiteralsImpl.java @@ -1,20 +1,23 @@ package type.property.optional.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.optional.UnionStringLiteralProperty; /** @@ -48,55 +51,144 @@ public final class UnionStringLiteralsImpl { } /** - * The interface defining all the services for OptionalClientUnionStringLiterals to be used by the proxy service to - * perform REST calls. + * The HTTP operations for OptionalClientUnionStringLiterals. */ - @ServiceInterface(name = "OptionalClientUnionStringLiterals", host = "{endpoint}") public interface UnionStringLiteralsService { static UnionStringLiteralsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.optional.implementation.UnionStringLiteralsServiceImpl"); - return (UnionStringLiteralsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new UnionStringLiteralsServiceImpl(pipeline); + } + + Response getAll(String endpoint, String accept, RequestContext requestContext); + + Response getDefault(String endpoint, String accept, RequestContext requestContext); + + Response putAll(String endpoint, String contentType, UnionStringLiteralProperty body, + RequestContext requestContext); + + Response putDefault(String endpoint, String contentType, UnionStringLiteralProperty body, + RequestContext requestContext); + } + + private static final class UnionStringLiteralsServiceImpl implements UnionStringLiteralsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(UnionStringLiteralsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private UnionStringLiteralsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response getAll(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/optional/union/string/literal/all"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, UnionStringLiteralProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response getDefault(String endpoint, String accept, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/optional/union/string/literal/default"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, UnionStringLiteralProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response putAll(String endpoint, String contentType, UnionStringLiteralProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/optional/union/string/literal/all"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 putDefault(String endpoint, String contentType, UnionStringLiteralProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/optional/union/string/literal/default"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/optional/union/string/literal/all", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getAll(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/optional/union/string/literal/default", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getDefault(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/optional/union/string/literal/all", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putAll(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") UnionStringLiteralProperty body, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/optional/union/string/literal/default", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putDefault(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") UnionStringLiteralProperty body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/BooleanLiteralsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/BooleanLiteralsImpl.java index 713088e0bd4..e18c3963020 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/BooleanLiteralsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/BooleanLiteralsImpl.java @@ -1,20 +1,23 @@ package type.property.valuetypes.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.valuetypes.BooleanLiteralProperty; /** @@ -48,38 +51,90 @@ public final class BooleanLiteralsImpl { } /** - * The interface defining all the services for ValueTypesClientBooleanLiterals to be used by the proxy service to - * perform REST calls. + * The HTTP operations for ValueTypesClientBooleanLiterals. */ - @ServiceInterface(name = "ValueTypesClientBooleanLiterals", host = "{endpoint}") public interface BooleanLiteralsService { static BooleanLiteralsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.valuetypes.implementation.BooleanLiteralsServiceImpl"); - return (BooleanLiteralsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new BooleanLiteralsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, BooleanLiteralProperty body, + RequestContext requestContext); + } + + private static final class BooleanLiteralsServiceImpl implements BooleanLiteralsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(BooleanLiteralsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private BooleanLiteralsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/value-types/boolean/literal"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, BooleanLiteralProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, BooleanLiteralProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/value-types/boolean/literal"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/value-types/boolean/literal", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/value-types/boolean/literal", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") BooleanLiteralProperty body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/BooleanOperationsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/BooleanOperationsImpl.java index 18ca5443028..a00cb35d16d 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/BooleanOperationsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/BooleanOperationsImpl.java @@ -1,20 +1,23 @@ package type.property.valuetypes.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.valuetypes.BooleanProperty; /** @@ -48,38 +51,89 @@ public final class BooleanOperationsImpl { } /** - * The interface defining all the services for ValueTypesClientBooleanOperations to be used by the proxy service to - * perform REST calls. + * The HTTP operations for ValueTypesClientBooleanOperations. */ - @ServiceInterface(name = "ValueTypesClientBooleanOperations", host = "{endpoint}") public interface BooleanOperationsService { static BooleanOperationsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.valuetypes.implementation.BooleanOperationsServiceImpl"); - return (BooleanOperationsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new BooleanOperationsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, BooleanProperty body, RequestContext requestContext); + } + + private static final class BooleanOperationsServiceImpl implements BooleanOperationsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(BooleanOperationsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private BooleanOperationsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/property/value-types/boolean"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, BooleanProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, BooleanProperty body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/property/value-types/boolean"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/value-types/boolean", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/value-types/boolean", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") BooleanProperty body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/BytesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/BytesImpl.java index e46906ab499..4a9d9204236 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/BytesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/BytesImpl.java @@ -1,20 +1,23 @@ package type.property.valuetypes.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.valuetypes.BytesProperty; /** @@ -48,37 +51,89 @@ public final class BytesImpl { } /** - * The interface defining all the services for ValueTypesClientBytes to be used by the proxy service to perform REST - * calls. + * The HTTP operations for ValueTypesClientBytes. */ - @ServiceInterface(name = "ValueTypesClientBytes", host = "{endpoint}") public interface BytesService { static BytesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.valuetypes.implementation.BytesServiceImpl"); - return (BytesService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new BytesServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, BytesProperty body, RequestContext requestContext); + } + + private static final class BytesServiceImpl implements BytesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(BytesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private BytesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/property/value-types/bytes"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, BytesProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, BytesProperty body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/property/value-types/bytes"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/value-types/bytes", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/value-types/bytes", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") BytesProperty body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/CollectionsIntsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/CollectionsIntsImpl.java index 6588a626e24..66eabcad3a6 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/CollectionsIntsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/CollectionsIntsImpl.java @@ -1,20 +1,23 @@ package type.property.valuetypes.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.valuetypes.CollectionsIntProperty; /** @@ -48,38 +51,90 @@ public final class CollectionsIntsImpl { } /** - * The interface defining all the services for ValueTypesClientCollectionsInts to be used by the proxy service to - * perform REST calls. + * The HTTP operations for ValueTypesClientCollectionsInts. */ - @ServiceInterface(name = "ValueTypesClientCollectionsInts", host = "{endpoint}") public interface CollectionsIntsService { static CollectionsIntsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.valuetypes.implementation.CollectionsIntsServiceImpl"); - return (CollectionsIntsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new CollectionsIntsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, CollectionsIntProperty body, + RequestContext requestContext); + } + + private static final class CollectionsIntsServiceImpl implements CollectionsIntsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(CollectionsIntsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private CollectionsIntsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/value-types/collections/int"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, CollectionsIntProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, CollectionsIntProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/value-types/collections/int"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/value-types/collections/int", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/value-types/collections/int", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") CollectionsIntProperty body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/CollectionsModelsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/CollectionsModelsImpl.java index 882316946dc..bedb77099a2 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/CollectionsModelsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/CollectionsModelsImpl.java @@ -1,20 +1,23 @@ package type.property.valuetypes.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.valuetypes.CollectionsModelProperty; /** @@ -48,38 +51,90 @@ public final class CollectionsModelsImpl { } /** - * The interface defining all the services for ValueTypesClientCollectionsModels to be used by the proxy service to - * perform REST calls. + * The HTTP operations for ValueTypesClientCollectionsModels. */ - @ServiceInterface(name = "ValueTypesClientCollectionsModels", host = "{endpoint}") public interface CollectionsModelsService { static CollectionsModelsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.valuetypes.implementation.CollectionsModelsServiceImpl"); - return (CollectionsModelsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new CollectionsModelsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, CollectionsModelProperty body, + RequestContext requestContext); + } + + private static final class CollectionsModelsServiceImpl implements CollectionsModelsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(CollectionsModelsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private CollectionsModelsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/value-types/collections/model"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, CollectionsModelProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, CollectionsModelProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/value-types/collections/model"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/value-types/collections/model", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/value-types/collections/model", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") CollectionsModelProperty body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/CollectionsStringsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/CollectionsStringsImpl.java index fcd1648b0e3..e130fa41805 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/CollectionsStringsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/CollectionsStringsImpl.java @@ -1,20 +1,23 @@ package type.property.valuetypes.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.valuetypes.CollectionsStringProperty; /** @@ -48,38 +51,90 @@ public final class CollectionsStringsImpl { } /** - * The interface defining all the services for ValueTypesClientCollectionsStrings to be used by the proxy service to - * perform REST calls. + * The HTTP operations for ValueTypesClientCollectionsStrings. */ - @ServiceInterface(name = "ValueTypesClientCollectionsStrings", host = "{endpoint}") public interface CollectionsStringsService { static CollectionsStringsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.valuetypes.implementation.CollectionsStringsServiceImpl"); - return (CollectionsStringsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new CollectionsStringsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, CollectionsStringProperty body, + RequestContext requestContext); + } + + private static final class CollectionsStringsServiceImpl implements CollectionsStringsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(CollectionsStringsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private CollectionsStringsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/value-types/collections/string"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, CollectionsStringProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, CollectionsStringProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/value-types/collections/string"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/value-types/collections/string", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/value-types/collections/string", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") CollectionsStringProperty body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/DatetimeOperationsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/DatetimeOperationsImpl.java index 8378afe8794..4b52b429856 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/DatetimeOperationsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/DatetimeOperationsImpl.java @@ -1,20 +1,23 @@ package type.property.valuetypes.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.valuetypes.DatetimeProperty; /** @@ -48,38 +51,89 @@ public final class DatetimeOperationsImpl { } /** - * The interface defining all the services for ValueTypesClientDatetimeOperations to be used by the proxy service to - * perform REST calls. + * The HTTP operations for ValueTypesClientDatetimeOperations. */ - @ServiceInterface(name = "ValueTypesClientDatetimeOperations", host = "{endpoint}") public interface DatetimeOperationsService { static DatetimeOperationsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.valuetypes.implementation.DatetimeOperationsServiceImpl"); - return (DatetimeOperationsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new DatetimeOperationsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, DatetimeProperty body, RequestContext requestContext); + } + + private static final class DatetimeOperationsServiceImpl implements DatetimeOperationsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(DatetimeOperationsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private DatetimeOperationsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/property/value-types/datetime"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, DatetimeProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, DatetimeProperty body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/property/value-types/datetime"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/value-types/datetime", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/value-types/datetime", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") DatetimeProperty body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/Decimal128sImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/Decimal128sImpl.java index 3eb7a4ef8d9..824c4bdd274 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/Decimal128sImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/Decimal128sImpl.java @@ -1,20 +1,23 @@ package type.property.valuetypes.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.valuetypes.Decimal128Property; /** @@ -48,38 +51,89 @@ public final class Decimal128sImpl { } /** - * The interface defining all the services for ValueTypesClientDecimal128s to be used by the proxy service to - * perform REST calls. + * The HTTP operations for ValueTypesClientDecimal128s. */ - @ServiceInterface(name = "ValueTypesClientDecimal128s", host = "{endpoint}") public interface Decimal128sService { static Decimal128sService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.valuetypes.implementation.Decimal128sServiceImpl"); - return (Decimal128sService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new Decimal128sServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, Decimal128Property body, RequestContext requestContext); + } + + private static final class Decimal128sServiceImpl implements Decimal128sService { + private static final ClientLogger HTTP_LOG = new ClientLogger(Decimal128sService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private Decimal128sServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/value-types/decimal128"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, Decimal128Property.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, Decimal128Property body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/value-types/decimal128"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/value-types/decimal128", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/value-types/decimal128", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") Decimal128Property body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/DecimalsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/DecimalsImpl.java index b9b20667dae..7bbbe788674 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/DecimalsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/DecimalsImpl.java @@ -1,20 +1,23 @@ package type.property.valuetypes.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.valuetypes.DecimalProperty; /** @@ -48,37 +51,89 @@ public final class DecimalsImpl { } /** - * The interface defining all the services for ValueTypesClientDecimals to be used by the proxy service to perform - * REST calls. + * The HTTP operations for ValueTypesClientDecimals. */ - @ServiceInterface(name = "ValueTypesClientDecimals", host = "{endpoint}") public interface DecimalsService { static DecimalsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.valuetypes.implementation.DecimalsServiceImpl"); - return (DecimalsService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new DecimalsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, DecimalProperty body, RequestContext requestContext); + } + + private static final class DecimalsServiceImpl implements DecimalsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(DecimalsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private DecimalsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/property/value-types/decimal"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, DecimalProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, DecimalProperty body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/property/value-types/decimal"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/value-types/decimal", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/value-types/decimal", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") DecimalProperty body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/DictionaryStringsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/DictionaryStringsImpl.java index f30370b47d4..ab1afae0ed4 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/DictionaryStringsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/DictionaryStringsImpl.java @@ -1,20 +1,23 @@ package type.property.valuetypes.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.valuetypes.DictionaryStringProperty; /** @@ -48,38 +51,90 @@ public final class DictionaryStringsImpl { } /** - * The interface defining all the services for ValueTypesClientDictionaryStrings to be used by the proxy service to - * perform REST calls. + * The HTTP operations for ValueTypesClientDictionaryStrings. */ - @ServiceInterface(name = "ValueTypesClientDictionaryStrings", host = "{endpoint}") public interface DictionaryStringsService { static DictionaryStringsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.valuetypes.implementation.DictionaryStringsServiceImpl"); - return (DictionaryStringsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new DictionaryStringsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, DictionaryStringProperty body, + RequestContext requestContext); + } + + private static final class DictionaryStringsServiceImpl implements DictionaryStringsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(DictionaryStringsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private DictionaryStringsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/value-types/dictionary/string"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, DictionaryStringProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, DictionaryStringProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/value-types/dictionary/string"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/value-types/dictionary/string", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/value-types/dictionary/string", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") DictionaryStringProperty body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/DurationOperationsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/DurationOperationsImpl.java index 24400f870e3..0964be1785d 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/DurationOperationsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/DurationOperationsImpl.java @@ -1,20 +1,23 @@ package type.property.valuetypes.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.valuetypes.DurationProperty; /** @@ -48,38 +51,89 @@ public final class DurationOperationsImpl { } /** - * The interface defining all the services for ValueTypesClientDurationOperations to be used by the proxy service to - * perform REST calls. + * The HTTP operations for ValueTypesClientDurationOperations. */ - @ServiceInterface(name = "ValueTypesClientDurationOperations", host = "{endpoint}") public interface DurationOperationsService { static DurationOperationsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.valuetypes.implementation.DurationOperationsServiceImpl"); - return (DurationOperationsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new DurationOperationsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, DurationProperty body, RequestContext requestContext); + } + + private static final class DurationOperationsServiceImpl implements DurationOperationsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(DurationOperationsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private DurationOperationsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/property/value-types/duration"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, DurationProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, DurationProperty body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/property/value-types/duration"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/value-types/duration", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/value-types/duration", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") DurationProperty body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/EnumsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/EnumsImpl.java index 2cba3bf3af5..ae78a236d84 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/EnumsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/EnumsImpl.java @@ -1,20 +1,23 @@ package type.property.valuetypes.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.valuetypes.EnumProperty; /** @@ -48,37 +51,89 @@ public final class EnumsImpl { } /** - * The interface defining all the services for ValueTypesClientEnums to be used by the proxy service to perform REST - * calls. + * The HTTP operations for ValueTypesClientEnums. */ - @ServiceInterface(name = "ValueTypesClientEnums", host = "{endpoint}") public interface EnumsService { static EnumsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.valuetypes.implementation.EnumsServiceImpl"); - return (EnumsService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new EnumsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, EnumProperty body, RequestContext requestContext); + } + + private static final class EnumsServiceImpl implements EnumsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(EnumsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private EnumsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/property/value-types/enum"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, EnumProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, EnumProperty body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/property/value-types/enum"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/value-types/enum", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/value-types/enum", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") EnumProperty body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/ExtensibleEnumsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/ExtensibleEnumsImpl.java index b3889aafa3f..d8d9351c2e1 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/ExtensibleEnumsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/ExtensibleEnumsImpl.java @@ -1,20 +1,23 @@ package type.property.valuetypes.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.valuetypes.ExtensibleEnumProperty; /** @@ -48,38 +51,90 @@ public final class ExtensibleEnumsImpl { } /** - * The interface defining all the services for ValueTypesClientExtensibleEnums to be used by the proxy service to - * perform REST calls. + * The HTTP operations for ValueTypesClientExtensibleEnums. */ - @ServiceInterface(name = "ValueTypesClientExtensibleEnums", host = "{endpoint}") public interface ExtensibleEnumsService { static ExtensibleEnumsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.valuetypes.implementation.ExtensibleEnumsServiceImpl"); - return (ExtensibleEnumsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new ExtensibleEnumsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, ExtensibleEnumProperty body, + RequestContext requestContext); + } + + private static final class ExtensibleEnumsServiceImpl implements ExtensibleEnumsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ExtensibleEnumsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ExtensibleEnumsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/value-types/extensible-enum"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, ExtensibleEnumProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, ExtensibleEnumProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/value-types/extensible-enum"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/value-types/extensible-enum", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/value-types/extensible-enum", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") ExtensibleEnumProperty body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/FloatLiteralsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/FloatLiteralsImpl.java index bd85759910b..56470c9557d 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/FloatLiteralsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/FloatLiteralsImpl.java @@ -1,20 +1,23 @@ package type.property.valuetypes.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.valuetypes.FloatLiteralProperty; /** @@ -48,38 +51,90 @@ public final class FloatLiteralsImpl { } /** - * The interface defining all the services for ValueTypesClientFloatLiterals to be used by the proxy service to - * perform REST calls. + * The HTTP operations for ValueTypesClientFloatLiterals. */ - @ServiceInterface(name = "ValueTypesClientFloatLiterals", host = "{endpoint}") public interface FloatLiteralsService { static FloatLiteralsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.valuetypes.implementation.FloatLiteralsServiceImpl"); - return (FloatLiteralsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new FloatLiteralsServiceImpl(pipeline); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/value-types/float/literal", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, FloatLiteralProperty body, RequestContext requestContext); + } + + private static final class FloatLiteralsServiceImpl implements FloatLiteralsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(FloatLiteralsService.class); + + private final HttpPipeline httpPipeline; - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/value-types/float/literal", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") FloatLiteralProperty body, RequestContext requestContext); + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private FloatLiteralsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/value-types/float/literal"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, FloatLiteralProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response put(String endpoint, String contentType, FloatLiteralProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/value-types/float/literal"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/FloatOperationsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/FloatOperationsImpl.java index 2625a495c7e..ee4a97cc0e6 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/FloatOperationsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/FloatOperationsImpl.java @@ -1,20 +1,23 @@ package type.property.valuetypes.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.valuetypes.FloatProperty; /** @@ -48,38 +51,89 @@ public final class FloatOperationsImpl { } /** - * The interface defining all the services for ValueTypesClientFloatOperations to be used by the proxy service to - * perform REST calls. + * The HTTP operations for ValueTypesClientFloatOperations. */ - @ServiceInterface(name = "ValueTypesClientFloatOperations", host = "{endpoint}") public interface FloatOperationsService { static FloatOperationsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.valuetypes.implementation.FloatOperationsServiceImpl"); - return (FloatOperationsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new FloatOperationsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, FloatProperty body, RequestContext requestContext); + } + + private static final class FloatOperationsServiceImpl implements FloatOperationsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(FloatOperationsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private FloatOperationsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/property/value-types/float"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, FloatProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, FloatProperty body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/property/value-types/float"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/value-types/float", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/value-types/float", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") FloatProperty body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/IntLiteralsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/IntLiteralsImpl.java index 81ff22151cd..09abbf637a5 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/IntLiteralsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/IntLiteralsImpl.java @@ -1,20 +1,23 @@ package type.property.valuetypes.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.valuetypes.IntLiteralProperty; /** @@ -48,38 +51,89 @@ public final class IntLiteralsImpl { } /** - * The interface defining all the services for ValueTypesClientIntLiterals to be used by the proxy service to - * perform REST calls. + * The HTTP operations for ValueTypesClientIntLiterals. */ - @ServiceInterface(name = "ValueTypesClientIntLiterals", host = "{endpoint}") public interface IntLiteralsService { static IntLiteralsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.valuetypes.implementation.IntLiteralsServiceImpl"); - return (IntLiteralsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new IntLiteralsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, IntLiteralProperty body, RequestContext requestContext); + } + + private static final class IntLiteralsServiceImpl implements IntLiteralsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(IntLiteralsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private IntLiteralsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/value-types/int/literal"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, IntLiteralProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, IntLiteralProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/value-types/int/literal"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/value-types/int/literal", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/value-types/int/literal", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") IntLiteralProperty body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/IntsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/IntsImpl.java index 9a706857cbc..233498da085 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/IntsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/IntsImpl.java @@ -1,20 +1,23 @@ package type.property.valuetypes.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.valuetypes.IntProperty; /** @@ -48,37 +51,89 @@ public final class IntsImpl { } /** - * The interface defining all the services for ValueTypesClientInts to be used by the proxy service to perform REST - * calls. + * The HTTP operations for ValueTypesClientInts. */ - @ServiceInterface(name = "ValueTypesClientInts", host = "{endpoint}") public interface IntsService { static IntsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.valuetypes.implementation.IntsServiceImpl"); - return (IntsService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new IntsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, IntProperty body, RequestContext requestContext); + } + + private static final class IntsServiceImpl implements IntsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(IntsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private IntsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/property/value-types/int"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, IntProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, IntProperty body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/property/value-types/int"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/value-types/int", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/value-types/int", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") IntProperty body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/ModelsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/ModelsImpl.java index dd72f11db9c..d65dc163ce9 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/ModelsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/ModelsImpl.java @@ -1,20 +1,23 @@ package type.property.valuetypes.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.valuetypes.ModelProperty; /** @@ -48,37 +51,89 @@ public final class ModelsImpl { } /** - * The interface defining all the services for ValueTypesClientModels to be used by the proxy service to perform - * REST calls. + * The HTTP operations for ValueTypesClientModels. */ - @ServiceInterface(name = "ValueTypesClientModels", host = "{endpoint}") public interface ModelsService { static ModelsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.valuetypes.implementation.ModelsServiceImpl"); - return (ModelsService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new ModelsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, ModelProperty body, RequestContext requestContext); + } + + private static final class ModelsServiceImpl implements ModelsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ModelsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ModelsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/property/value-types/model"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, ModelProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, ModelProperty body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/property/value-types/model"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/value-types/model", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/value-types/model", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") ModelProperty body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/NeversImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/NeversImpl.java index dc1b053fe99..1f998049e91 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/NeversImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/NeversImpl.java @@ -1,20 +1,23 @@ package type.property.valuetypes.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.valuetypes.NeverProperty; /** @@ -48,37 +51,89 @@ public final class NeversImpl { } /** - * The interface defining all the services for ValueTypesClientNevers to be used by the proxy service to perform - * REST calls. + * The HTTP operations for ValueTypesClientNevers. */ - @ServiceInterface(name = "ValueTypesClientNevers", host = "{endpoint}") public interface NeversService { static NeversService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.valuetypes.implementation.NeversServiceImpl"); - return (NeversService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new NeversServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, NeverProperty body, RequestContext requestContext); + } + + private static final class NeversServiceImpl implements NeversService { + private static final ClientLogger HTTP_LOG = new ClientLogger(NeversService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private NeversServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/property/value-types/never"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, NeverProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, NeverProperty body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/property/value-types/never"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/value-types/never", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/value-types/never", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") NeverProperty body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/StringLiteralsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/StringLiteralsImpl.java index cbe1e2882ec..50fcebab2fa 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/StringLiteralsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/StringLiteralsImpl.java @@ -1,20 +1,23 @@ package type.property.valuetypes.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.valuetypes.StringLiteralProperty; /** @@ -48,38 +51,90 @@ public final class StringLiteralsImpl { } /** - * The interface defining all the services for ValueTypesClientStringLiterals to be used by the proxy service to - * perform REST calls. + * The HTTP operations for ValueTypesClientStringLiterals. */ - @ServiceInterface(name = "ValueTypesClientStringLiterals", host = "{endpoint}") public interface StringLiteralsService { static StringLiteralsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.valuetypes.implementation.StringLiteralsServiceImpl"); - return (StringLiteralsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new StringLiteralsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, StringLiteralProperty body, + RequestContext requestContext); + } + + private static final class StringLiteralsServiceImpl implements StringLiteralsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(StringLiteralsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private StringLiteralsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/value-types/string/literal"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, StringLiteralProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, StringLiteralProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/value-types/string/literal"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/value-types/string/literal", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/value-types/string/literal", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") StringLiteralProperty body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/StringOperationsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/StringOperationsImpl.java index 150cc67490b..c94b18f7414 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/StringOperationsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/StringOperationsImpl.java @@ -1,20 +1,23 @@ package type.property.valuetypes.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.valuetypes.StringProperty; /** @@ -48,38 +51,89 @@ public final class StringOperationsImpl { } /** - * The interface defining all the services for ValueTypesClientStringOperations to be used by the proxy service to - * perform REST calls. + * The HTTP operations for ValueTypesClientStringOperations. */ - @ServiceInterface(name = "ValueTypesClientStringOperations", host = "{endpoint}") public interface StringOperationsService { static StringOperationsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.valuetypes.implementation.StringOperationsServiceImpl"); - return (StringOperationsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new StringOperationsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, StringProperty body, RequestContext requestContext); + } + + private static final class StringOperationsServiceImpl implements StringOperationsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(StringOperationsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private StringOperationsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/property/value-types/string"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, StringProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, StringProperty body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/property/value-types/string"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/value-types/string", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/value-types/string", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") StringProperty body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/UnionEnumValuesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/UnionEnumValuesImpl.java index b53ff932b95..190eb0299c7 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/UnionEnumValuesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/UnionEnumValuesImpl.java @@ -1,20 +1,23 @@ package type.property.valuetypes.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.valuetypes.UnionEnumValueProperty; /** @@ -48,38 +51,90 @@ public final class UnionEnumValuesImpl { } /** - * The interface defining all the services for ValueTypesClientUnionEnumValues to be used by the proxy service to - * perform REST calls. + * The HTTP operations for ValueTypesClientUnionEnumValues. */ - @ServiceInterface(name = "ValueTypesClientUnionEnumValues", host = "{endpoint}") public interface UnionEnumValuesService { static UnionEnumValuesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.valuetypes.implementation.UnionEnumValuesServiceImpl"); - return (UnionEnumValuesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new UnionEnumValuesServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, UnionEnumValueProperty body, + RequestContext requestContext); + } + + private static final class UnionEnumValuesServiceImpl implements UnionEnumValuesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(UnionEnumValuesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private UnionEnumValuesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/value-types/union-enum-value"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, UnionEnumValueProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, UnionEnumValueProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/value-types/union-enum-value"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/value-types/union-enum-value", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/value-types/union-enum-value", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") UnionEnumValueProperty body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/UnionFloatLiteralsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/UnionFloatLiteralsImpl.java index c30146461d6..0c6f9dce6bf 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/UnionFloatLiteralsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/UnionFloatLiteralsImpl.java @@ -1,20 +1,23 @@ package type.property.valuetypes.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.valuetypes.UnionFloatLiteralProperty; /** @@ -48,38 +51,90 @@ public final class UnionFloatLiteralsImpl { } /** - * The interface defining all the services for ValueTypesClientUnionFloatLiterals to be used by the proxy service to - * perform REST calls. + * The HTTP operations for ValueTypesClientUnionFloatLiterals. */ - @ServiceInterface(name = "ValueTypesClientUnionFloatLiterals", host = "{endpoint}") public interface UnionFloatLiteralsService { static UnionFloatLiteralsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.valuetypes.implementation.UnionFloatLiteralsServiceImpl"); - return (UnionFloatLiteralsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new UnionFloatLiteralsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, UnionFloatLiteralProperty body, + RequestContext requestContext); + } + + private static final class UnionFloatLiteralsServiceImpl implements UnionFloatLiteralsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(UnionFloatLiteralsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private UnionFloatLiteralsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/value-types/union/float/literal"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, UnionFloatLiteralProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, UnionFloatLiteralProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/value-types/union/float/literal"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/value-types/union/float/literal", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/value-types/union/float/literal", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") UnionFloatLiteralProperty body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/UnionIntLiteralsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/UnionIntLiteralsImpl.java index 4d684748751..c6d77deeedc 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/UnionIntLiteralsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/UnionIntLiteralsImpl.java @@ -1,20 +1,23 @@ package type.property.valuetypes.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.valuetypes.UnionIntLiteralProperty; /** @@ -48,38 +51,90 @@ public final class UnionIntLiteralsImpl { } /** - * The interface defining all the services for ValueTypesClientUnionIntLiterals to be used by the proxy service to - * perform REST calls. + * The HTTP operations for ValueTypesClientUnionIntLiterals. */ - @ServiceInterface(name = "ValueTypesClientUnionIntLiterals", host = "{endpoint}") public interface UnionIntLiteralsService { static UnionIntLiteralsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.valuetypes.implementation.UnionIntLiteralsServiceImpl"); - return (UnionIntLiteralsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new UnionIntLiteralsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, UnionIntLiteralProperty body, + RequestContext requestContext); + } + + private static final class UnionIntLiteralsServiceImpl implements UnionIntLiteralsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(UnionIntLiteralsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private UnionIntLiteralsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/value-types/union/int/literal"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, UnionIntLiteralProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, UnionIntLiteralProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/value-types/union/int/literal"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/value-types/union/int/literal", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/value-types/union/int/literal", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") UnionIntLiteralProperty body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/UnionStringLiteralsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/UnionStringLiteralsImpl.java index ed17f89dfb6..6e61abe044e 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/UnionStringLiteralsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/UnionStringLiteralsImpl.java @@ -1,20 +1,23 @@ package type.property.valuetypes.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.valuetypes.UnionStringLiteralProperty; /** @@ -48,39 +51,90 @@ public final class UnionStringLiteralsImpl { } /** - * The interface defining all the services for ValueTypesClientUnionStringLiterals to be used by the proxy service - * to perform REST calls. + * The HTTP operations for ValueTypesClientUnionStringLiterals. */ - @ServiceInterface(name = "ValueTypesClientUnionStringLiterals", host = "{endpoint}") public interface UnionStringLiteralsService { static UnionStringLiteralsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz - = Class.forName("type.property.valuetypes.implementation.UnionStringLiteralsServiceImpl"); - return (UnionStringLiteralsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new UnionStringLiteralsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, UnionStringLiteralProperty body, + RequestContext requestContext); + } + + private static final class UnionStringLiteralsServiceImpl implements UnionStringLiteralsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(UnionStringLiteralsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private UnionStringLiteralsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/value-types/union/string/literal"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, UnionStringLiteralProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, UnionStringLiteralProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/value-types/union/string/literal"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/value-types/union/string/literal", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/value-types/union/string/literal", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") UnionStringLiteralProperty body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/UnknownArraysImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/UnknownArraysImpl.java index 60b3fde9c2f..59423b5706e 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/UnknownArraysImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/UnknownArraysImpl.java @@ -1,20 +1,23 @@ package type.property.valuetypes.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.valuetypes.UnknownArrayProperty; /** @@ -48,38 +51,90 @@ public final class UnknownArraysImpl { } /** - * The interface defining all the services for ValueTypesClientUnknownArrays to be used by the proxy service to - * perform REST calls. + * The HTTP operations for ValueTypesClientUnknownArrays. */ - @ServiceInterface(name = "ValueTypesClientUnknownArrays", host = "{endpoint}") public interface UnknownArraysService { static UnknownArraysService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.valuetypes.implementation.UnknownArraysServiceImpl"); - return (UnknownArraysService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new UnknownArraysServiceImpl(pipeline); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/value-types/unknown/array", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, UnknownArrayProperty body, RequestContext requestContext); + } + + private static final class UnknownArraysServiceImpl implements UnknownArraysService { + private static final ClientLogger HTTP_LOG = new ClientLogger(UnknownArraysService.class); + + private final HttpPipeline httpPipeline; - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/value-types/unknown/array", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") UnknownArrayProperty body, RequestContext requestContext); + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private UnknownArraysServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/value-types/unknown/array"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, UnknownArrayProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response put(String endpoint, String contentType, UnknownArrayProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/value-types/unknown/array"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/UnknownDictsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/UnknownDictsImpl.java index 376ae2b1302..a04575b5420 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/UnknownDictsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/UnknownDictsImpl.java @@ -1,20 +1,23 @@ package type.property.valuetypes.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.valuetypes.UnknownDictProperty; /** @@ -48,38 +51,90 @@ public final class UnknownDictsImpl { } /** - * The interface defining all the services for ValueTypesClientUnknownDicts to be used by the proxy service to - * perform REST calls. + * The HTTP operations for ValueTypesClientUnknownDicts. */ - @ServiceInterface(name = "ValueTypesClientUnknownDicts", host = "{endpoint}") public interface UnknownDictsService { static UnknownDictsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.valuetypes.implementation.UnknownDictsServiceImpl"); - return (UnknownDictsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new UnknownDictsServiceImpl(pipeline); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/value-types/unknown/dict", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, UnknownDictProperty body, RequestContext requestContext); + } + + private static final class UnknownDictsServiceImpl implements UnknownDictsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(UnknownDictsService.class); + + private final HttpPipeline httpPipeline; - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/value-types/unknown/dict", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") UnknownDictProperty body, RequestContext requestContext); + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private UnknownDictsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/value-types/unknown/dict"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, UnknownDictProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response put(String endpoint, String contentType, UnknownDictProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/value-types/unknown/dict"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/UnknownIntsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/UnknownIntsImpl.java index 67e2195258d..9389739a58c 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/UnknownIntsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/UnknownIntsImpl.java @@ -1,20 +1,23 @@ package type.property.valuetypes.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.valuetypes.UnknownIntProperty; /** @@ -48,38 +51,89 @@ public final class UnknownIntsImpl { } /** - * The interface defining all the services for ValueTypesClientUnknownInts to be used by the proxy service to - * perform REST calls. + * The HTTP operations for ValueTypesClientUnknownInts. */ - @ServiceInterface(name = "ValueTypesClientUnknownInts", host = "{endpoint}") public interface UnknownIntsService { static UnknownIntsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.valuetypes.implementation.UnknownIntsServiceImpl"); - return (UnknownIntsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new UnknownIntsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, UnknownIntProperty body, RequestContext requestContext); + } + + private static final class UnknownIntsServiceImpl implements UnknownIntsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(UnknownIntsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private UnknownIntsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/value-types/unknown/int"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, UnknownIntProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, UnknownIntProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/value-types/unknown/int"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/value-types/unknown/int", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/value-types/unknown/int", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") UnknownIntProperty body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/UnknownStringsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/UnknownStringsImpl.java index dc1c90876c9..419fb660f37 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/UnknownStringsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/property/valuetypes/implementation/UnknownStringsImpl.java @@ -1,20 +1,23 @@ package type.property.valuetypes.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.property.valuetypes.UnknownStringProperty; /** @@ -48,38 +51,90 @@ public final class UnknownStringsImpl { } /** - * The interface defining all the services for ValueTypesClientUnknownStrings to be used by the proxy service to - * perform REST calls. + * The HTTP operations for ValueTypesClientUnknownStrings. */ - @ServiceInterface(name = "ValueTypesClientUnknownStrings", host = "{endpoint}") public interface UnknownStringsService { static UnknownStringsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.property.valuetypes.implementation.UnknownStringsServiceImpl"); - return (UnknownStringsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new UnknownStringsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, UnknownStringProperty body, + RequestContext requestContext); + } + + private static final class UnknownStringsServiceImpl implements UnknownStringsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(UnknownStringsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private UnknownStringsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/property/value-types/unknown/string"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, UnknownStringProperty.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, UnknownStringProperty body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/property/value-types/unknown/string"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/property/value-types/unknown/string", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/property/value-types/unknown/string", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") UnknownStringProperty body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/scalar/implementation/BooleanOperationsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/scalar/implementation/BooleanOperationsImpl.java index 56670d4fab4..744e4614e43 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/scalar/implementation/BooleanOperationsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/scalar/implementation/BooleanOperationsImpl.java @@ -1,20 +1,23 @@ package type.scalar.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; /** * An instance of this class provides access to all the operations defined in BooleanOperations. @@ -47,32 +50,85 @@ public final class BooleanOperationsImpl { } /** - * The interface defining all the services for ScalarClientBooleanOperations to be used by the proxy service to - * perform REST calls. + * The HTTP operations for ScalarClientBooleanOperations. */ - @ServiceInterface(name = "ScalarClientBooleanOperations", host = "{endpoint}") public interface BooleanOperationsService { static BooleanOperationsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.scalar.implementation.BooleanOperationsServiceImpl"); - return (BooleanOperationsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new BooleanOperationsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, boolean body, RequestContext requestContext); + } + + private static final class BooleanOperationsServiceImpl implements BooleanOperationsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(BooleanOperationsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + private BooleanOperationsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation(method = HttpMethod.GET, path = "/type/scalar/boolean", expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/scalar/boolean"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, Boolean.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } - @HttpRequestInformation(method = HttpMethod.PUT, path = "/type/scalar/boolean", expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("content-type") String contentType, - @BodyParam("application/json") boolean body, RequestContext requestContext); + @Override + public Response put(String endpoint, String contentType, boolean body, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/scalar/boolean"); + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/scalar/implementation/Decimal128TypesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/scalar/implementation/Decimal128TypesImpl.java index 751992ec907..7775d02ab23 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/scalar/implementation/Decimal128TypesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/scalar/implementation/Decimal128TypesImpl.java @@ -1,21 +1,24 @@ package type.scalar.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -import io.clientcore.core.http.annotations.HostParam; -import io.clientcore.core.http.annotations.HttpRequestInformation; -import io.clientcore.core.http.annotations.QueryParam; -import io.clientcore.core.http.annotations.UnexpectedResponseExceptionDetail; +import io.clientcore.core.http.models.HttpHeaderName; 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.SerializationFormat; +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 io.clientcore.core.utils.UriBuilder; +import java.lang.reflect.ParameterizedType; import java.math.BigDecimal; /** @@ -49,47 +52,109 @@ public final class Decimal128TypesImpl { } /** - * The interface defining all the services for ScalarClientDecimal128Types to be used by the proxy service to - * perform REST calls. + * The HTTP operations for ScalarClientDecimal128Types. */ - @ServiceInterface(name = "ScalarClientDecimal128Types", host = "{endpoint}") public interface Decimal128TypesService { static Decimal128TypesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.scalar.implementation.Decimal128TypesServiceImpl"); - return (Decimal128TypesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new Decimal128TypesServiceImpl(pipeline); + } + + Response responseBody(String endpoint, String accept, RequestContext requestContext); + + Response requestBody(String endpoint, String contentType, BigDecimal body, RequestContext requestContext); + + Response requestParameter(String endpoint, BigDecimal value, RequestContext requestContext); + } + + private static final class Decimal128TypesServiceImpl implements Decimal128TypesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(Decimal128TypesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private Decimal128TypesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response responseBody(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/scalar/decimal128/response_body"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, BigDecimal.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response requestBody(String endpoint, String contentType, BigDecimal body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/scalar/decimal128/resquest_body"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/scalar/decimal128/response_body", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response responseBody(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/scalar/decimal128/resquest_body", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response requestBody(@HostParam("endpoint") String endpoint, - @HeaderParam("content-type") String contentType, @BodyParam("application/json") BigDecimal body, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/scalar/decimal128/request_parameter", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response requestParameter(@HostParam("endpoint") String endpoint, @QueryParam("value") BigDecimal value, - RequestContext requestContext); + @Override + public Response requestParameter(String endpoint, BigDecimal value, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/type/scalar/decimal128/request_parameter"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "value", true, value, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/scalar/implementation/Decimal128VerifiesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/scalar/implementation/Decimal128VerifiesImpl.java index 5bbfad9234b..4c848ff81f8 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/scalar/implementation/Decimal128VerifiesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/scalar/implementation/Decimal128VerifiesImpl.java @@ -1,20 +1,23 @@ package type.scalar.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import java.math.BigDecimal; import java.util.List; @@ -49,38 +52,90 @@ public final class Decimal128VerifiesImpl { } /** - * The interface defining all the services for ScalarClientDecimal128Verifies to be used by the proxy service to - * perform REST calls. + * The HTTP operations for ScalarClientDecimal128Verifies. */ - @ServiceInterface(name = "ScalarClientDecimal128Verifies", host = "{endpoint}") public interface Decimal128VerifiesService { static Decimal128VerifiesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.scalar.implementation.Decimal128VerifiesServiceImpl"); - return (Decimal128VerifiesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new Decimal128VerifiesServiceImpl(pipeline); + } + + Response> prepareVerify(String endpoint, String accept, RequestContext requestContext); + + Response verify(String endpoint, String contentType, BigDecimal body, RequestContext requestContext); + } + + private static final class Decimal128VerifiesServiceImpl implements Decimal128VerifiesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(Decimal128VerifiesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private Decimal128VerifiesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response> prepareVerify(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/scalar/decimal128/prepare_verify"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, + CoreUtils.createParameterizedType(List.class, BigDecimal.class)), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response verify(String endpoint, String contentType, BigDecimal body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/type/scalar/decimal128/verify"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/scalar/decimal128/prepare_verify", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response> prepareVerify(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/type/scalar/decimal128/verify", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response verify(@HostParam("endpoint") String endpoint, @HeaderParam("content-type") String contentType, - @BodyParam("application/json") BigDecimal body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/scalar/implementation/DecimalTypesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/scalar/implementation/DecimalTypesImpl.java index a25c5c32ac4..4cb71c467bd 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/scalar/implementation/DecimalTypesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/scalar/implementation/DecimalTypesImpl.java @@ -1,21 +1,24 @@ package type.scalar.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -import io.clientcore.core.http.annotations.HostParam; -import io.clientcore.core.http.annotations.HttpRequestInformation; -import io.clientcore.core.http.annotations.QueryParam; -import io.clientcore.core.http.annotations.UnexpectedResponseExceptionDetail; +import io.clientcore.core.http.models.HttpHeaderName; 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.SerializationFormat; +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 io.clientcore.core.utils.UriBuilder; +import java.lang.reflect.ParameterizedType; import java.math.BigDecimal; /** @@ -49,47 +52,109 @@ public final class DecimalTypesImpl { } /** - * The interface defining all the services for ScalarClientDecimalTypes to be used by the proxy service to perform - * REST calls. + * The HTTP operations for ScalarClientDecimalTypes. */ - @ServiceInterface(name = "ScalarClientDecimalTypes", host = "{endpoint}") public interface DecimalTypesService { static DecimalTypesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.scalar.implementation.DecimalTypesServiceImpl"); - return (DecimalTypesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new DecimalTypesServiceImpl(pipeline); + } + + Response responseBody(String endpoint, String accept, RequestContext requestContext); + + Response requestBody(String endpoint, String contentType, BigDecimal body, RequestContext requestContext); + + Response requestParameter(String endpoint, BigDecimal value, RequestContext requestContext); + } + + private static final class DecimalTypesServiceImpl implements DecimalTypesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(DecimalTypesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private DecimalTypesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response responseBody(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/scalar/decimal/response_body"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, BigDecimal.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response requestBody(String endpoint, String contentType, BigDecimal body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/scalar/decimal/resquest_body"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/scalar/decimal/response_body", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response responseBody(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/scalar/decimal/resquest_body", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response requestBody(@HostParam("endpoint") String endpoint, - @HeaderParam("content-type") String contentType, @BodyParam("application/json") BigDecimal body, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/scalar/decimal/request_parameter", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response requestParameter(@HostParam("endpoint") String endpoint, @QueryParam("value") BigDecimal value, - RequestContext requestContext); + @Override + public Response requestParameter(String endpoint, BigDecimal value, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/type/scalar/decimal/request_parameter"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "value", true, value, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/scalar/implementation/DecimalVerifiesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/scalar/implementation/DecimalVerifiesImpl.java index 9046f68f4a9..1c7c6848782 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/scalar/implementation/DecimalVerifiesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/scalar/implementation/DecimalVerifiesImpl.java @@ -1,20 +1,23 @@ package type.scalar.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import java.math.BigDecimal; import java.util.List; @@ -49,38 +52,90 @@ public final class DecimalVerifiesImpl { } /** - * The interface defining all the services for ScalarClientDecimalVerifies to be used by the proxy service to - * perform REST calls. + * The HTTP operations for ScalarClientDecimalVerifies. */ - @ServiceInterface(name = "ScalarClientDecimalVerifies", host = "{endpoint}") public interface DecimalVerifiesService { static DecimalVerifiesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.scalar.implementation.DecimalVerifiesServiceImpl"); - return (DecimalVerifiesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new DecimalVerifiesServiceImpl(pipeline); + } + + Response> prepareVerify(String endpoint, String accept, RequestContext requestContext); + + Response verify(String endpoint, String contentType, BigDecimal body, RequestContext requestContext); + } + + private static final class DecimalVerifiesServiceImpl implements DecimalVerifiesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(DecimalVerifiesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private DecimalVerifiesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response> prepareVerify(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/scalar/decimal/prepare_verify"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, + CoreUtils.createParameterizedType(List.class, BigDecimal.class)), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response verify(String endpoint, String contentType, BigDecimal body, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/type/scalar/decimal/verify"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/scalar/decimal/prepare_verify", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response> prepareVerify(@HostParam("endpoint") String endpoint, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/type/scalar/decimal/verify", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response verify(@HostParam("endpoint") String endpoint, @HeaderParam("content-type") String contentType, - @BodyParam("application/json") BigDecimal body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/scalar/implementation/StringOperationsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/scalar/implementation/StringOperationsImpl.java index d9d6b845aaf..2afa343f09d 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/scalar/implementation/StringOperationsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/scalar/implementation/StringOperationsImpl.java @@ -1,20 +1,23 @@ package type.scalar.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; /** * An instance of this class provides access to all the operations defined in StringOperations. @@ -47,32 +50,88 @@ public final class StringOperationsImpl { } /** - * The interface defining all the services for ScalarClientStringOperations to be used by the proxy service to - * perform REST calls. + * The HTTP operations for ScalarClientStringOperations. */ - @ServiceInterface(name = "ScalarClientStringOperations", host = "{endpoint}") public interface StringOperationsService { static StringOperationsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.scalar.implementation.StringOperationsServiceImpl"); - return (StringOperationsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new StringOperationsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, String body, RequestContext requestContext); + } + private static final class StringOperationsServiceImpl implements StringOperationsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(StringOperationsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private StringOperationsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation(method = HttpMethod.GET, path = "/type/scalar/string", expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/scalar/string"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, String.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } - @HttpRequestInformation(method = HttpMethod.PUT, path = "/type/scalar/string", expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("content-type") String contentType, - @BodyParam("application/json") String body, RequestContext requestContext); + @Override + public Response put(String endpoint, String contentType, String body, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/scalar/string"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/scalar/implementation/UnknownsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/scalar/implementation/UnknownsImpl.java index 018facdf91f..e616d695d73 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/scalar/implementation/UnknownsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/scalar/implementation/UnknownsImpl.java @@ -1,21 +1,22 @@ package type.scalar.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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 io.clientcore.core.instrumentation.logging.ClientLogger; import io.clientcore.core.models.binarydata.BinaryData; -import java.lang.reflect.InvocationTargetException; +import io.clientcore.core.serialization.SerializationFormat; +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; /** * An instance of this class provides access to all the operations defined in Unknowns. @@ -48,31 +49,73 @@ public final class UnknownsImpl { } /** - * The interface defining all the services for ScalarClientUnknowns to be used by the proxy service to perform REST - * calls. + * The HTTP operations for ScalarClientUnknowns. */ - @ServiceInterface(name = "ScalarClientUnknowns", host = "{endpoint}") public interface UnknownsService { static UnknownsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.scalar.implementation.UnknownsServiceImpl"); - return (UnknownsService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new UnknownsServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, BinaryData body, RequestContext requestContext); + } + + private static final class UnknownsServiceImpl implements UnknownsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(UnknownsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private UnknownsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation(method = HttpMethod.GET, path = "/type/scalar/unknown", expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/scalar/unknown"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return networkResponse; + } - @HttpRequestInformation(method = HttpMethod.PUT, path = "/type/scalar/unknown", expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("content-type") String contentType, - @BodyParam("application/json") BinaryData body, RequestContext requestContext); + @Override + public Response put(String endpoint, String contentType, BinaryData body, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.PUT).setUri(endpoint + "/type/scalar/unknown"); + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/discriminated/extendsnamespace/implementation/CustomDiscriminatorOperationsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/discriminated/extendsnamespace/implementation/CustomDiscriminatorOperationsImpl.java index 4ac1824e95c..d0498b2e1c8 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/discriminated/extendsnamespace/implementation/CustomDiscriminatorOperationsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/discriminated/extendsnamespace/implementation/CustomDiscriminatorOperationsImpl.java @@ -1,20 +1,23 @@ package type.union.discriminated.extendsnamespace.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.union.discriminated.extendsnamespace.customdiscriminator.PetBase2; /** @@ -48,55 +51,141 @@ public final class CustomDiscriminatorOperationsImpl { } /** - * The interface defining all the services for ExtendsClientCustomDiscriminatorOperations to be used by the proxy - * service to perform REST calls. + * The HTTP operations for ExtendsClientCustomDiscriminatorOperations. */ - @ServiceInterface(name = "ExtendsClientCustomDiscriminatorOperations", host = "{endpoint}") public interface CustomDiscriminatorOperationsService { static CustomDiscriminatorOperationsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName( - "type.union.discriminated.extendsnamespace.implementation.CustomDiscriminatorOperationsServiceImpl"); - return (CustomDiscriminatorOperationsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new CustomDiscriminatorOperationsServiceImpl(pipeline); + } + + Response getCat(String endpoint, String accept, RequestContext requestContext); + + Response putCat(String endpoint, String contentType, PetBase2 input, RequestContext requestContext); + + Response getDog(String endpoint, String accept, RequestContext requestContext); + + Response putDog(String endpoint, String contentType, PetBase2 input, RequestContext requestContext); + } + + private static final class CustomDiscriminatorOperationsServiceImpl + implements CustomDiscriminatorOperationsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(CustomDiscriminatorOperationsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private CustomDiscriminatorOperationsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response getCat(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/union/discriminated/extends/custom-discriminator/cat"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, PetBase2.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response putCat(String endpoint, String contentType, PetBase2 input, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/union/discriminated/extends/custom-discriminator/cat"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 getDog(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/union/discriminated/extends/custom-discriminator/dog"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, PetBase2.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response putDog(String endpoint, String contentType, PetBase2 input, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/union/discriminated/extends/custom-discriminator/dog"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/union/discriminated/extends/custom-discriminator/cat", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getCat(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/union/discriminated/extends/custom-discriminator/cat", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putCat(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") PetBase2 input, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/union/discriminated/extends/custom-discriminator/dog", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getDog(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/union/discriminated/extends/custom-discriminator/dog", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putDog(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") PetBase2 input, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/discriminated/extendsnamespace/implementation/EnumDiscriminatorOperationsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/discriminated/extendsnamespace/implementation/EnumDiscriminatorOperationsImpl.java index 49d9dc15b6a..05664f546c4 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/discriminated/extendsnamespace/implementation/EnumDiscriminatorOperationsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/discriminated/extendsnamespace/implementation/EnumDiscriminatorOperationsImpl.java @@ -1,20 +1,23 @@ package type.union.discriminated.extendsnamespace.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.union.discriminated.extendsnamespace.enumdiscriminator.PetBase3; /** @@ -48,55 +51,140 @@ public final class EnumDiscriminatorOperationsImpl { } /** - * The interface defining all the services for ExtendsClientEnumDiscriminatorOperations to be used by the proxy - * service to perform REST calls. + * The HTTP operations for ExtendsClientEnumDiscriminatorOperations. */ - @ServiceInterface(name = "ExtendsClientEnumDiscriminatorOperations", host = "{endpoint}") public interface EnumDiscriminatorOperationsService { static EnumDiscriminatorOperationsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName( - "type.union.discriminated.extendsnamespace.implementation.EnumDiscriminatorOperationsServiceImpl"); - return (EnumDiscriminatorOperationsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new EnumDiscriminatorOperationsServiceImpl(pipeline); + } + + Response getCat(String endpoint, String accept, RequestContext requestContext); + + Response putCat(String endpoint, String contentType, PetBase3 input, RequestContext requestContext); + + Response getDog(String endpoint, String accept, RequestContext requestContext); + + Response putDog(String endpoint, String contentType, PetBase3 input, RequestContext requestContext); + } + + private static final class EnumDiscriminatorOperationsServiceImpl implements EnumDiscriminatorOperationsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(EnumDiscriminatorOperationsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private EnumDiscriminatorOperationsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response getCat(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/union/discriminated/extends/enum-discriminator/cat"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, PetBase3.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response putCat(String endpoint, String contentType, PetBase3 input, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/union/discriminated/extends/enum-discriminator/cat"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 getDog(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/union/discriminated/extends/enum-discriminator/dog"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, PetBase3.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response putDog(String endpoint, String contentType, PetBase3 input, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/union/discriminated/extends/enum-discriminator/dog"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/union/discriminated/extends/enum-discriminator/cat", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getCat(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/union/discriminated/extends/enum-discriminator/cat", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putCat(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") PetBase3 input, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/union/discriminated/extends/enum-discriminator/dog", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getDog(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/union/discriminated/extends/enum-discriminator/dog", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putDog(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") PetBase3 input, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/discriminated/extendsnamespace/implementation/ExplicitDiscriminatorOperationsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/discriminated/extendsnamespace/implementation/ExplicitDiscriminatorOperationsImpl.java index ac1d5da08d7..2e8ccb1a181 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/discriminated/extendsnamespace/implementation/ExplicitDiscriminatorOperationsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/discriminated/extendsnamespace/implementation/ExplicitDiscriminatorOperationsImpl.java @@ -1,20 +1,23 @@ package type.union.discriminated.extendsnamespace.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.union.discriminated.extendsnamespace.explicitdiscriminator.PetBase; /** @@ -48,55 +51,141 @@ public final class ExplicitDiscriminatorOperationsImpl { } /** - * The interface defining all the services for ExtendsClientExplicitDiscriminatorOperations to be used by the proxy - * service to perform REST calls. + * The HTTP operations for ExtendsClientExplicitDiscriminatorOperations. */ - @ServiceInterface(name = "ExtendsClientExplicitDiscriminatorOperations", host = "{endpoint}") public interface ExplicitDiscriminatorOperationsService { static ExplicitDiscriminatorOperationsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName( - "type.union.discriminated.extendsnamespace.implementation.ExplicitDiscriminatorOperationsServiceImpl"); - return (ExplicitDiscriminatorOperationsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new ExplicitDiscriminatorOperationsServiceImpl(pipeline); + } + + Response getCat(String endpoint, String accept, RequestContext requestContext); + + Response putCat(String endpoint, String contentType, PetBase input, RequestContext requestContext); + + Response getDog(String endpoint, String accept, RequestContext requestContext); + + Response putDog(String endpoint, String contentType, PetBase input, RequestContext requestContext); + } + + private static final class ExplicitDiscriminatorOperationsServiceImpl + implements ExplicitDiscriminatorOperationsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ExplicitDiscriminatorOperationsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ExplicitDiscriminatorOperationsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response getCat(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/union/discriminated/extends/explicit-discriminator/cat"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, PetBase.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response putCat(String endpoint, String contentType, PetBase input, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/union/discriminated/extends/explicit-discriminator/cat"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 getDog(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/union/discriminated/extends/explicit-discriminator/dog"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, PetBase.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response putDog(String endpoint, String contentType, PetBase input, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/union/discriminated/extends/explicit-discriminator/dog"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/union/discriminated/extends/explicit-discriminator/cat", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getCat(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/union/discriminated/extends/explicit-discriminator/cat", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putCat(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") PetBase input, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/union/discriminated/extends/explicit-discriminator/dog", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getDog(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/union/discriminated/extends/explicit-discriminator/dog", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putDog(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") PetBase input, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/discriminated/extendsnamespace/implementation/InjectedDiscriminatorOperationsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/discriminated/extendsnamespace/implementation/InjectedDiscriminatorOperationsImpl.java index fcb0e80f8e9..87f58e79c07 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/discriminated/extendsnamespace/implementation/InjectedDiscriminatorOperationsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/discriminated/extendsnamespace/implementation/InjectedDiscriminatorOperationsImpl.java @@ -1,20 +1,23 @@ package type.union.discriminated.extendsnamespace.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.union.discriminated.extendsnamespace.injecteddiscriminator.PetBase1; /** @@ -48,55 +51,141 @@ public final class InjectedDiscriminatorOperationsImpl { } /** - * The interface defining all the services for ExtendsClientInjectedDiscriminatorOperations to be used by the proxy - * service to perform REST calls. + * The HTTP operations for ExtendsClientInjectedDiscriminatorOperations. */ - @ServiceInterface(name = "ExtendsClientInjectedDiscriminatorOperations", host = "{endpoint}") public interface InjectedDiscriminatorOperationsService { static InjectedDiscriminatorOperationsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName( - "type.union.discriminated.extendsnamespace.implementation.InjectedDiscriminatorOperationsServiceImpl"); - return (InjectedDiscriminatorOperationsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new InjectedDiscriminatorOperationsServiceImpl(pipeline); + } + + Response getCat(String endpoint, String accept, RequestContext requestContext); + + Response putCat(String endpoint, String contentType, PetBase1 input, RequestContext requestContext); + + Response getDog(String endpoint, String accept, RequestContext requestContext); + + Response putDog(String endpoint, String contentType, PetBase1 input, RequestContext requestContext); + } + + private static final class InjectedDiscriminatorOperationsServiceImpl + implements InjectedDiscriminatorOperationsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(InjectedDiscriminatorOperationsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private InjectedDiscriminatorOperationsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response getCat(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/union/discriminated/extends/injected-discriminator/cat"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, PetBase1.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response putCat(String endpoint, String contentType, PetBase1 input, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/union/discriminated/extends/injected-discriminator/cat"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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 getDog(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/union/discriminated/extends/injected-discriminator/dog"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, PetBase1.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response putDog(String endpoint, String contentType, PetBase1 input, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/union/discriminated/extends/injected-discriminator/dog"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/union/discriminated/extends/injected-discriminator/cat", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getCat(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/union/discriminated/extends/injected-discriminator/cat", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putCat(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") PetBase1 input, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/union/discriminated/extends/injected-discriminator/dog", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response getDog(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/union/discriminated/extends/injected-discriminator/dog", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response putDog(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") PetBase1 input, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/discriminated/extendsnamespace/implementation/RecursivesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/discriminated/extendsnamespace/implementation/RecursivesImpl.java index b04a99a9829..5928b5b6f80 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/discriminated/extendsnamespace/implementation/RecursivesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/discriminated/extendsnamespace/implementation/RecursivesImpl.java @@ -1,20 +1,23 @@ package type.union.discriminated.extendsnamespace.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.union.discriminated.extendsnamespace.recursive.PetBase4; /** @@ -48,38 +51,88 @@ public final class RecursivesImpl { } /** - * The interface defining all the services for ExtendsClientRecursives to be used by the proxy service to perform - * REST calls. + * The HTTP operations for ExtendsClientRecursives. */ - @ServiceInterface(name = "ExtendsClientRecursives", host = "{endpoint}") public interface RecursivesService { static RecursivesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz - = Class.forName("type.union.discriminated.extendsnamespace.implementation.RecursivesServiceImpl"); - return (RecursivesService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new RecursivesServiceImpl(pipeline); + } + + Response get(String endpoint, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, PetBase4 input, RequestContext requestContext); + } + + private static final class RecursivesServiceImpl implements RecursivesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(RecursivesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private RecursivesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET) + .setUri(endpoint + "/type/union/discriminated/extends/recursive"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, PetBase4.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response put(String endpoint, String contentType, PetBase4 input, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/union/discriminated/extends/recursive"); + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/union/discriminated/extends/recursive", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, - RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/union/discriminated/extends/recursive", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") PetBase4 input, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/discriminated/implementation/EnvelopeObjectCustomPropertiesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/discriminated/implementation/EnvelopeObjectCustomPropertiesImpl.java index bb3b3dccb0e..2e464a018b8 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/discriminated/implementation/EnvelopeObjectCustomPropertiesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/discriminated/implementation/EnvelopeObjectCustomPropertiesImpl.java @@ -1,22 +1,23 @@ package type.union.discriminated.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -import io.clientcore.core.http.annotations.HostParam; -import io.clientcore.core.http.annotations.HttpRequestInformation; -import io.clientcore.core.http.annotations.QueryParam; -import io.clientcore.core.http.annotations.UnexpectedResponseExceptionDetail; +import io.clientcore.core.http.models.HttpHeaderName; 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 io.clientcore.core.instrumentation.logging.ClientLogger; import io.clientcore.core.models.binarydata.BinaryData; -import java.lang.reflect.InvocationTargetException; +import io.clientcore.core.serialization.SerializationFormat; +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 io.clientcore.core.utils.UriBuilder; /** * An instance of this class provides access to all the operations defined in EnvelopeObjectCustomProperties. @@ -49,40 +50,80 @@ public final class EnvelopeObjectCustomPropertiesImpl { } /** - * The interface defining all the services for DiscriminatedClientEnvelopeObjectCustomProperties to be used by the - * proxy service to perform REST calls. + * The HTTP operations for DiscriminatedClientEnvelopeObjectCustomProperties. */ - @ServiceInterface(name = "DiscriminatedClientEnvelopeObjectCustomProperties", host = "{endpoint}") public interface EnvelopeObjectCustomPropertiesService { static EnvelopeObjectCustomPropertiesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class - .forName("type.union.discriminated.implementation.EnvelopeObjectCustomPropertiesServiceImpl"); - return (EnvelopeObjectCustomPropertiesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new EnvelopeObjectCustomPropertiesServiceImpl(pipeline); + } + + Response get(String endpoint, String petType, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, String accept, BinaryData input, + RequestContext requestContext); + } + + private static final class EnvelopeObjectCustomPropertiesServiceImpl + implements EnvelopeObjectCustomPropertiesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(EnvelopeObjectCustomPropertiesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + private EnvelopeObjectCustomPropertiesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/union/discriminated/envelope/object/custom-properties", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @QueryParam("petType") String petType, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/union/discriminated/envelope/object/custom-properties", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") BinaryData input, RequestContext requestContext); + @Override + public Response get(String endpoint, String petType, String accept, RequestContext requestContext) { + UriBuilder uriBuilder + = UriBuilder.parse(endpoint + "/type/union/discriminated/envelope/object/custom-properties"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "petType", true, petType, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return networkResponse; + } + + @Override + public Response put(String endpoint, String contentType, String accept, BinaryData input, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/union/discriminated/envelope/object/custom-properties"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return networkResponse; + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/discriminated/implementation/EnvelopeObjectDefaultsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/discriminated/implementation/EnvelopeObjectDefaultsImpl.java index 7ae7e037447..d621fcc7002 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/discriminated/implementation/EnvelopeObjectDefaultsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/discriminated/implementation/EnvelopeObjectDefaultsImpl.java @@ -1,22 +1,23 @@ package type.union.discriminated.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -import io.clientcore.core.http.annotations.HostParam; -import io.clientcore.core.http.annotations.HttpRequestInformation; -import io.clientcore.core.http.annotations.QueryParam; -import io.clientcore.core.http.annotations.UnexpectedResponseExceptionDetail; +import io.clientcore.core.http.models.HttpHeaderName; 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 io.clientcore.core.instrumentation.logging.ClientLogger; import io.clientcore.core.models.binarydata.BinaryData; -import java.lang.reflect.InvocationTargetException; +import io.clientcore.core.serialization.SerializationFormat; +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 io.clientcore.core.utils.UriBuilder; /** * An instance of this class provides access to all the operations defined in EnvelopeObjectDefaults. @@ -49,40 +50,78 @@ public final class EnvelopeObjectDefaultsImpl { } /** - * The interface defining all the services for DiscriminatedClientEnvelopeObjectDefaults to be used by the proxy - * service to perform REST calls. + * The HTTP operations for DiscriminatedClientEnvelopeObjectDefaults. */ - @ServiceInterface(name = "DiscriminatedClientEnvelopeObjectDefaults", host = "{endpoint}") public interface EnvelopeObjectDefaultsService { static EnvelopeObjectDefaultsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz - = Class.forName("type.union.discriminated.implementation.EnvelopeObjectDefaultsServiceImpl"); - return (EnvelopeObjectDefaultsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new EnvelopeObjectDefaultsServiceImpl(pipeline); + } + + Response get(String endpoint, String kind, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, String accept, BinaryData input, + RequestContext requestContext); + } + + private static final class EnvelopeObjectDefaultsServiceImpl implements EnvelopeObjectDefaultsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(EnvelopeObjectDefaultsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + private EnvelopeObjectDefaultsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/union/discriminated/envelope/object/default", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @QueryParam("kind") String kind, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/union/discriminated/envelope/object/default", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") BinaryData input, RequestContext requestContext); + @Override + public Response get(String endpoint, String kind, String accept, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/type/union/discriminated/envelope/object/default"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "kind", true, kind, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return networkResponse; + } + + @Override + public Response put(String endpoint, String contentType, String accept, BinaryData input, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/union/discriminated/envelope/object/default"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return networkResponse; + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/discriminated/implementation/NoEnvelopeCustomDiscriminatorsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/discriminated/implementation/NoEnvelopeCustomDiscriminatorsImpl.java index 0193ccaa642..6c53dc1c9fc 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/discriminated/implementation/NoEnvelopeCustomDiscriminatorsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/discriminated/implementation/NoEnvelopeCustomDiscriminatorsImpl.java @@ -1,22 +1,23 @@ package type.union.discriminated.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -import io.clientcore.core.http.annotations.HostParam; -import io.clientcore.core.http.annotations.HttpRequestInformation; -import io.clientcore.core.http.annotations.QueryParam; -import io.clientcore.core.http.annotations.UnexpectedResponseExceptionDetail; +import io.clientcore.core.http.models.HttpHeaderName; 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 io.clientcore.core.instrumentation.logging.ClientLogger; import io.clientcore.core.models.binarydata.BinaryData; -import java.lang.reflect.InvocationTargetException; +import io.clientcore.core.serialization.SerializationFormat; +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 io.clientcore.core.utils.UriBuilder; /** * An instance of this class provides access to all the operations defined in NoEnvelopeCustomDiscriminators. @@ -49,40 +50,80 @@ public final class NoEnvelopeCustomDiscriminatorsImpl { } /** - * The interface defining all the services for DiscriminatedClientNoEnvelopeCustomDiscriminators to be used by the - * proxy service to perform REST calls. + * The HTTP operations for DiscriminatedClientNoEnvelopeCustomDiscriminators. */ - @ServiceInterface(name = "DiscriminatedClientNoEnvelopeCustomDiscriminators", host = "{endpoint}") public interface NoEnvelopeCustomDiscriminatorsService { static NoEnvelopeCustomDiscriminatorsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class - .forName("type.union.discriminated.implementation.NoEnvelopeCustomDiscriminatorsServiceImpl"); - return (NoEnvelopeCustomDiscriminatorsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new NoEnvelopeCustomDiscriminatorsServiceImpl(pipeline); + } + + Response get(String endpoint, String type, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, String accept, BinaryData input, + RequestContext requestContext); + } + + private static final class NoEnvelopeCustomDiscriminatorsServiceImpl + implements NoEnvelopeCustomDiscriminatorsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(NoEnvelopeCustomDiscriminatorsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + private NoEnvelopeCustomDiscriminatorsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/union/discriminated/no-envelope/custom-discriminator", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @QueryParam("type") String type, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/union/discriminated/no-envelope/custom-discriminator", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") BinaryData input, RequestContext requestContext); + @Override + public Response get(String endpoint, String type, String accept, RequestContext requestContext) { + UriBuilder uriBuilder + = UriBuilder.parse(endpoint + "/type/union/discriminated/no-envelope/custom-discriminator"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "type", true, type, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return networkResponse; + } + + @Override + public Response put(String endpoint, String contentType, String accept, BinaryData input, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/union/discriminated/no-envelope/custom-discriminator"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return networkResponse; + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/discriminated/implementation/NoEnvelopeDefaultsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/discriminated/implementation/NoEnvelopeDefaultsImpl.java index 15b7e0dab43..a41ea6cb1ba 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/discriminated/implementation/NoEnvelopeDefaultsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/discriminated/implementation/NoEnvelopeDefaultsImpl.java @@ -1,22 +1,23 @@ package type.union.discriminated.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -import io.clientcore.core.http.annotations.HostParam; -import io.clientcore.core.http.annotations.HttpRequestInformation; -import io.clientcore.core.http.annotations.QueryParam; -import io.clientcore.core.http.annotations.UnexpectedResponseExceptionDetail; +import io.clientcore.core.http.models.HttpHeaderName; 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 io.clientcore.core.instrumentation.logging.ClientLogger; import io.clientcore.core.models.binarydata.BinaryData; -import java.lang.reflect.InvocationTargetException; +import io.clientcore.core.serialization.SerializationFormat; +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 io.clientcore.core.utils.UriBuilder; /** * An instance of this class provides access to all the operations defined in NoEnvelopeDefaults. @@ -49,39 +50,78 @@ public final class NoEnvelopeDefaultsImpl { } /** - * The interface defining all the services for DiscriminatedClientNoEnvelopeDefaults to be used by the proxy service - * to perform REST calls. + * The HTTP operations for DiscriminatedClientNoEnvelopeDefaults. */ - @ServiceInterface(name = "DiscriminatedClientNoEnvelopeDefaults", host = "{endpoint}") public interface NoEnvelopeDefaultsService { static NoEnvelopeDefaultsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.union.discriminated.implementation.NoEnvelopeDefaultsServiceImpl"); - return (NoEnvelopeDefaultsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new NoEnvelopeDefaultsServiceImpl(pipeline); + } + + Response get(String endpoint, String kind, String accept, RequestContext requestContext); + + Response put(String endpoint, String contentType, String accept, BinaryData input, + RequestContext requestContext); + } + + private static final class NoEnvelopeDefaultsServiceImpl implements NoEnvelopeDefaultsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(NoEnvelopeDefaultsService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + private NoEnvelopeDefaultsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/union/discriminated/no-envelope/default", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @QueryParam("kind") String kind, - @HeaderParam("Accept") String accept, RequestContext requestContext); - - @HttpRequestInformation( - method = HttpMethod.PUT, - path = "/type/union/discriminated/no-envelope/default", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response put(@HostParam("endpoint") String endpoint, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") BinaryData input, RequestContext requestContext); + @Override + public Response get(String endpoint, String kind, String accept, RequestContext requestContext) { + UriBuilder uriBuilder = UriBuilder.parse(endpoint + "/type/union/discriminated/no-envelope/default"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "kind", true, kind, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(uriBuilder.toString()); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return networkResponse; + } + + @Override + public Response put(String endpoint, String contentType, String accept, BinaryData input, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.PUT) + .setUri(endpoint + "/type/union/discriminated/no-envelope/default"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (input != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(input, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return networkResponse; + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/implementation/EnumsOnliesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/implementation/EnumsOnliesImpl.java index a0de9d8f009..1c63595a4ba 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/implementation/EnumsOnliesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/implementation/EnumsOnliesImpl.java @@ -1,20 +1,23 @@ package type.union.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.union.EnumsOnlyCases; import type.union.GetResponse6; @@ -49,35 +52,90 @@ public final class EnumsOnliesImpl { } /** - * The interface defining all the services for UnionClientEnumsOnlies to be used by the proxy service to perform - * REST calls. + * The HTTP operations for UnionClientEnumsOnlies. */ - @ServiceInterface(name = "UnionClientEnumsOnlies", host = "{endpoint}") public interface EnumsOnliesService { static EnumsOnliesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.union.implementation.EnumsOnliesServiceImpl"); - return (EnumsOnliesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new EnumsOnliesServiceImpl(pipeline); } - @HttpRequestInformation(method = HttpMethod.GET, path = "/type/union/enums-only", expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, + Response get(String endpoint, String accept, RequestContext requestContext); + + Response send(String endpoint, String contentType, SendRequest6 sendRequest6, RequestContext requestContext); + } + + private static final class EnumsOnliesServiceImpl implements EnumsOnliesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(EnumsOnliesService.class); + + private final HttpPipeline httpPipeline; - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/type/union/enums-only", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response send(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") SendRequest6 sendRequest6, RequestContext requestContext); + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private EnumsOnliesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/union/enums-only"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, GetResponse6.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response send(String endpoint, String contentType, SendRequest6 sendRequest6, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/type/union/enums-only"); + if (sendRequest6 != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(sendRequest6, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/implementation/FloatsOnliesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/implementation/FloatsOnliesImpl.java index ca501df9f61..24cd557f097 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/implementation/FloatsOnliesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/implementation/FloatsOnliesImpl.java @@ -1,20 +1,23 @@ package type.union.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.union.GetResponse4; import type.union.GetResponseProp3; @@ -49,38 +52,90 @@ public final class FloatsOnliesImpl { } /** - * The interface defining all the services for UnionClientFloatsOnlies to be used by the proxy service to perform - * REST calls. + * The HTTP operations for UnionClientFloatsOnlies. */ - @ServiceInterface(name = "UnionClientFloatsOnlies", host = "{endpoint}") public interface FloatsOnliesService { static FloatsOnliesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.union.implementation.FloatsOnliesServiceImpl"); - return (FloatsOnliesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new FloatsOnliesServiceImpl(pipeline); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/union/floats-only", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, + Response get(String endpoint, String accept, RequestContext requestContext); + + Response send(String endpoint, String contentType, SendRequest4 sendRequest4, RequestContext requestContext); + } + + private static final class FloatsOnliesServiceImpl implements FloatsOnliesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(FloatsOnliesService.class); + + private final HttpPipeline httpPipeline; - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/type/union/floats-only", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response send(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") SendRequest4 sendRequest4, RequestContext requestContext); + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private FloatsOnliesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/union/floats-only"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, GetResponse4.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response send(String endpoint, String contentType, SendRequest4 sendRequest4, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/type/union/floats-only"); + if (sendRequest4 != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(sendRequest4, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/implementation/IntsOnliesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/implementation/IntsOnliesImpl.java index b63e7a45983..5e17cd72f5d 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/implementation/IntsOnliesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/implementation/IntsOnliesImpl.java @@ -1,20 +1,23 @@ package type.union.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.union.GetResponse3; import type.union.GetResponseProp2; @@ -49,31 +52,90 @@ public final class IntsOnliesImpl { } /** - * The interface defining all the services for UnionClientIntsOnlies to be used by the proxy service to perform REST - * calls. + * The HTTP operations for UnionClientIntsOnlies. */ - @ServiceInterface(name = "UnionClientIntsOnlies", host = "{endpoint}") public interface IntsOnliesService { static IntsOnliesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.union.implementation.IntsOnliesServiceImpl"); - return (IntsOnliesService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new IntsOnliesServiceImpl(pipeline); } - @HttpRequestInformation(method = HttpMethod.GET, path = "/type/union/ints-only", expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, + Response get(String endpoint, String accept, RequestContext requestContext); + + Response send(String endpoint, String contentType, SendRequest3 sendRequest3, RequestContext requestContext); + } + + private static final class IntsOnliesServiceImpl implements IntsOnliesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(IntsOnliesService.class); + + private final HttpPipeline httpPipeline; - @HttpRequestInformation(method = HttpMethod.POST, path = "/type/union/ints-only", expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response send(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") SendRequest3 sendRequest3, RequestContext requestContext); + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private IntsOnliesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/union/ints-only"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, GetResponse3.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response send(String endpoint, String contentType, SendRequest3 sendRequest3, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/type/union/ints-only"); + if (sendRequest3 != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(sendRequest3, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/implementation/MixedLiteralsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/implementation/MixedLiteralsImpl.java index 22cd34aa90c..c60c0b10a97 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/implementation/MixedLiteralsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/implementation/MixedLiteralsImpl.java @@ -1,20 +1,23 @@ package type.union.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.union.GetResponse8; import type.union.MixedLiteralsCases; @@ -49,38 +52,90 @@ public final class MixedLiteralsImpl { } /** - * The interface defining all the services for UnionClientMixedLiterals to be used by the proxy service to perform - * REST calls. + * The HTTP operations for UnionClientMixedLiterals. */ - @ServiceInterface(name = "UnionClientMixedLiterals", host = "{endpoint}") public interface MixedLiteralsService { static MixedLiteralsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.union.implementation.MixedLiteralsServiceImpl"); - return (MixedLiteralsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new MixedLiteralsServiceImpl(pipeline); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/union/mixed-literals", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, + Response get(String endpoint, String accept, RequestContext requestContext); + + Response send(String endpoint, String contentType, SendRequest8 sendRequest8, RequestContext requestContext); + } + + private static final class MixedLiteralsServiceImpl implements MixedLiteralsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(MixedLiteralsService.class); + + private final HttpPipeline httpPipeline; - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/type/union/mixed-literals", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response send(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") SendRequest8 sendRequest8, RequestContext requestContext); + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private MixedLiteralsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/union/mixed-literals"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, GetResponse8.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response send(String endpoint, String contentType, SendRequest8 sendRequest8, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/type/union/mixed-literals"); + if (sendRequest8 != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(sendRequest8, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/implementation/MixedTypesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/implementation/MixedTypesImpl.java index 78dca8f6bb7..163997513ac 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/implementation/MixedTypesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/implementation/MixedTypesImpl.java @@ -1,20 +1,23 @@ package type.union.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.union.GetResponse9; import type.union.MixedTypesCases; @@ -49,37 +52,90 @@ public final class MixedTypesImpl { } /** - * The interface defining all the services for UnionClientMixedTypes to be used by the proxy service to perform REST - * calls. + * The HTTP operations for UnionClientMixedTypes. */ - @ServiceInterface(name = "UnionClientMixedTypes", host = "{endpoint}") public interface MixedTypesService { static MixedTypesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.union.implementation.MixedTypesServiceImpl"); - return (MixedTypesService) clazz.getMethod("getNewInstance", HttpPipeline.class).invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new MixedTypesServiceImpl(pipeline); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/union/mixed-types", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, + Response get(String endpoint, String accept, RequestContext requestContext); + + Response send(String endpoint, String contentType, SendRequest9 sendRequest9, RequestContext requestContext); + } + + private static final class MixedTypesServiceImpl implements MixedTypesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(MixedTypesService.class); + + private final HttpPipeline httpPipeline; - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/type/union/mixed-types", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response send(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") SendRequest9 sendRequest9, RequestContext requestContext); + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private MixedTypesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/union/mixed-types"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, GetResponse9.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response send(String endpoint, String contentType, SendRequest9 sendRequest9, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/type/union/mixed-types"); + if (sendRequest9 != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(sendRequest9, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/implementation/ModelsOnliesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/implementation/ModelsOnliesImpl.java index 88817d7945a..a55af964df0 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/implementation/ModelsOnliesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/implementation/ModelsOnliesImpl.java @@ -1,21 +1,23 @@ package type.union.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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 io.clientcore.core.instrumentation.logging.ClientLogger; import io.clientcore.core.models.binarydata.BinaryData; -import java.lang.reflect.InvocationTargetException; +import io.clientcore.core.serialization.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.union.GetResponse5; /** @@ -49,38 +51,90 @@ public final class ModelsOnliesImpl { } /** - * The interface defining all the services for UnionClientModelsOnlies to be used by the proxy service to perform - * REST calls. + * The HTTP operations for UnionClientModelsOnlies. */ - @ServiceInterface(name = "UnionClientModelsOnlies", host = "{endpoint}") public interface ModelsOnliesService { static ModelsOnliesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.union.implementation.ModelsOnliesServiceImpl"); - return (ModelsOnliesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new ModelsOnliesServiceImpl(pipeline); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/union/models-only", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, + Response get(String endpoint, String accept, RequestContext requestContext); + + Response send(String endpoint, String contentType, SendRequest5 sendRequest5, RequestContext requestContext); + } + + private static final class ModelsOnliesServiceImpl implements ModelsOnliesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ModelsOnliesService.class); + + private final HttpPipeline httpPipeline; - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/type/union/models-only", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response send(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") SendRequest5 sendRequest5, RequestContext requestContext); + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private ModelsOnliesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/union/models-only"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, GetResponse5.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response send(String endpoint, String contentType, SendRequest5 sendRequest5, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/type/union/models-only"); + if (sendRequest5 != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(sendRequest5, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/implementation/StringAndArraysImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/implementation/StringAndArraysImpl.java index c020fe206d8..43c7eb1dbb8 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/implementation/StringAndArraysImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/implementation/StringAndArraysImpl.java @@ -1,20 +1,23 @@ package type.union.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.union.GetResponse7; import type.union.StringAndArrayCases; @@ -49,38 +52,90 @@ public final class StringAndArraysImpl { } /** - * The interface defining all the services for UnionClientStringAndArrays to be used by the proxy service to perform - * REST calls. + * The HTTP operations for UnionClientStringAndArrays. */ - @ServiceInterface(name = "UnionClientStringAndArrays", host = "{endpoint}") public interface StringAndArraysService { static StringAndArraysService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.union.implementation.StringAndArraysServiceImpl"); - return (StringAndArraysService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new StringAndArraysServiceImpl(pipeline); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/union/string-and-array", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, + Response get(String endpoint, String accept, RequestContext requestContext); + + Response send(String endpoint, String contentType, SendRequest7 sendRequest7, RequestContext requestContext); + } + + private static final class StringAndArraysServiceImpl implements StringAndArraysService { + private static final ClientLogger HTTP_LOG = new ClientLogger(StringAndArraysService.class); + + private final HttpPipeline httpPipeline; - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/type/union/string-and-array", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response send(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") SendRequest7 sendRequest7, RequestContext requestContext); + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private StringAndArraysServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/union/string-and-array"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, GetResponse7.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response send(String endpoint, String contentType, SendRequest7 sendRequest7, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/type/union/string-and-array"); + if (sendRequest7 != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(sendRequest7, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/implementation/StringExtensibleNamedsImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/implementation/StringExtensibleNamedsImpl.java index a82ba63ab7d..130ea0b61c6 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/implementation/StringExtensibleNamedsImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/implementation/StringExtensibleNamedsImpl.java @@ -1,20 +1,23 @@ package type.union.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.union.GetResponse2; import type.union.StringExtensibleNamedUnion; @@ -49,38 +52,90 @@ public final class StringExtensibleNamedsImpl { } /** - * The interface defining all the services for UnionClientStringExtensibleNameds to be used by the proxy service to - * perform REST calls. + * The HTTP operations for UnionClientStringExtensibleNameds. */ - @ServiceInterface(name = "UnionClientStringExtensibleNameds", host = "{endpoint}") public interface StringExtensibleNamedsService { static StringExtensibleNamedsService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.union.implementation.StringExtensibleNamedsServiceImpl"); - return (StringExtensibleNamedsService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new StringExtensibleNamedsServiceImpl(pipeline); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/union/string-extensible-named", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, + Response get(String endpoint, String accept, RequestContext requestContext); + + Response send(String endpoint, String contentType, SendRequest2 sendRequest2, RequestContext requestContext); + } + + private static final class StringExtensibleNamedsServiceImpl implements StringExtensibleNamedsService { + private static final ClientLogger HTTP_LOG = new ClientLogger(StringExtensibleNamedsService.class); + + private final HttpPipeline httpPipeline; - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/type/union/string-extensible-named", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response send(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") SendRequest2 sendRequest2, RequestContext requestContext); + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private StringExtensibleNamedsServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/union/string-extensible-named"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, GetResponse2.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response send(String endpoint, String contentType, SendRequest2 sendRequest2, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/type/union/string-extensible-named"); + if (sendRequest2 != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(sendRequest2, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/implementation/StringExtensiblesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/implementation/StringExtensiblesImpl.java index 052af52e97c..e756d2f14f7 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/implementation/StringExtensiblesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/implementation/StringExtensiblesImpl.java @@ -1,20 +1,23 @@ package type.union.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.union.GetResponse1; import type.union.GetResponseProp1; @@ -49,38 +52,90 @@ public final class StringExtensiblesImpl { } /** - * The interface defining all the services for UnionClientStringExtensibles to be used by the proxy service to - * perform REST calls. + * The HTTP operations for UnionClientStringExtensibles. */ - @ServiceInterface(name = "UnionClientStringExtensibles", host = "{endpoint}") public interface StringExtensiblesService { static StringExtensiblesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.union.implementation.StringExtensiblesServiceImpl"); - return (StringExtensiblesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new StringExtensiblesServiceImpl(pipeline); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/union/string-extensible", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, + Response get(String endpoint, String accept, RequestContext requestContext); + + Response send(String endpoint, String contentType, SendRequest1 sendRequest1, RequestContext requestContext); + } + + private static final class StringExtensiblesServiceImpl implements StringExtensiblesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(StringExtensiblesService.class); + + private final HttpPipeline httpPipeline; - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/type/union/string-extensible", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response send(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") SendRequest1 sendRequest1, RequestContext requestContext); + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private StringExtensiblesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/union/string-extensible"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, GetResponse1.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response send(String endpoint, String contentType, SendRequest1 sendRequest1, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/type/union/string-extensible"); + if (sendRequest1 != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(sendRequest1, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/implementation/StringsOnliesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/implementation/StringsOnliesImpl.java index dbfb9a36455..0763d29680d 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/implementation/StringsOnliesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/union/implementation/StringsOnliesImpl.java @@ -1,20 +1,23 @@ package type.union.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import type.union.GetResponse; import type.union.GetResponseProp; @@ -49,38 +52,90 @@ public final class StringsOnliesImpl { } /** - * The interface defining all the services for UnionClientStringsOnlies to be used by the proxy service to perform - * REST calls. + * The HTTP operations for UnionClientStringsOnlies. */ - @ServiceInterface(name = "UnionClientStringsOnlies", host = "{endpoint}") public interface StringsOnliesService { static StringsOnliesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("type.union.implementation.StringsOnliesServiceImpl"); - return (StringsOnliesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new StringsOnliesServiceImpl(pipeline); } - @HttpRequestInformation( - method = HttpMethod.GET, - path = "/type/union/strings-only", - expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response get(@HostParam("endpoint") String endpoint, @HeaderParam("Accept") String accept, + Response get(String endpoint, String accept, RequestContext requestContext); + + Response send(String endpoint, String contentType, SendRequest sendRequest, RequestContext requestContext); + } + + private static final class StringsOnliesServiceImpl implements StringsOnliesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(StringsOnliesService.class); + + private final HttpPipeline httpPipeline; - @HttpRequestInformation( - method = HttpMethod.POST, - path = "/type/union/strings-only", - expectedStatusCodes = { 204 }) - @UnexpectedResponseExceptionDetail - Response send(@HostParam("endpoint") String endpoint, @HeaderParam("Content-Type") String contentType, - @BodyParam("application/json") SendRequest sendRequest, RequestContext requestContext); + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private StringsOnliesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response get(String endpoint, String accept, RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.GET).setUri(endpoint + "/type/union/strings-only"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, GetResponse.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response send(String endpoint, String contentType, SendRequest sendRequest, + RequestContext requestContext) { + HttpRequest httpRequest + = new HttpRequest().setMethod(HttpMethod.POST).setUri(endpoint + "/type/union/strings-only"); + if (sendRequest != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(sendRequest, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + try (Response 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); + } + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/versioning/added/implementation/AddedClientImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/versioning/added/implementation/AddedClientImpl.java index c8040427fb1..18b7c791c15 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/versioning/added/implementation/AddedClientImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/versioning/added/implementation/AddedClientImpl.java @@ -1,20 +1,23 @@ package versioning.added.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import versioning.added.AddedServiceVersion; import versioning.added.ModelV1; import versioning.added.ModelV2; @@ -117,34 +120,107 @@ public AddedClientImpl(HttpPipeline httpPipeline, Instrumentation instrumentatio } /** - * The interface defining all the services for AddedClient to be used by the proxy service to perform REST calls. + * The HTTP operations for AddedClient. */ - @ServiceInterface(name = "AddedClient", host = "{endpoint}/versioning/added/api-version:{version}") public interface AddedClientService { static AddedClientService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("versioning.added.implementation.AddedClientServiceImpl"); - return (AddedClientService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - + return new AddedClientServiceImpl(pipeline); } - @HttpRequestInformation(method = HttpMethod.POST, path = "/v1", expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response v1(@HostParam("endpoint") String endpoint, @HostParam("version") String version, - @HeaderParam("header-v2") String headerV2, @HeaderParam("Content-Type") String contentType, - @HeaderParam("Accept") String accept, @BodyParam("application/json") ModelV1 body, + Response v1(String endpoint, String version, String headerV2, String contentType, String accept, + ModelV1 body, RequestContext requestContext); + + Response v2(String endpoint, String version, String contentType, String accept, ModelV2 body, RequestContext requestContext); + } + + private static final class AddedClientServiceImpl implements AddedClientService { + private static final ClientLogger HTTP_LOG = new ClientLogger(AddedClientService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private AddedClientServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response v1(String endpoint, String version, String headerV2, String contentType, String accept, + ModelV1 body, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/versioning/added/api-version:" + version + "/v1"); + if (headerV2 != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("header-v2"), headerV2); + } + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, ModelV1.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + @Override + public Response v2(String endpoint, String version, String contentType, String accept, ModelV2 body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/versioning/added/api-version:" + version + "/v2"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, ModelV2.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } - @HttpRequestInformation(method = HttpMethod.POST, path = "/v2", expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response v2(@HostParam("endpoint") String endpoint, @HostParam("version") String version, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") ModelV2 body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/versioning/added/implementation/InterfaceV2sImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/versioning/added/implementation/InterfaceV2sImpl.java index 6e569f13fb8..b3cae6afeb1 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/versioning/added/implementation/InterfaceV2sImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/versioning/added/implementation/InterfaceV2sImpl.java @@ -1,20 +1,23 @@ package versioning.added.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import versioning.added.AddedServiceVersion; import versioning.added.ModelV2; @@ -58,28 +61,71 @@ public AddedServiceVersion getServiceVersion() { } /** - * The interface defining all the services for AddedClientInterfaceV2s to be used by the proxy service to perform - * REST calls. + * The HTTP operations for AddedClientInterfaceV2s. */ - @ServiceInterface(name = "AddedClientInterfaceV2s", host = "{endpoint}/versioning/added/api-version:{version}") public interface InterfaceV2sService { static InterfaceV2sService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("versioning.added.implementation.InterfaceV2sServiceImpl"); - return (InterfaceV2sService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new InterfaceV2sServiceImpl(pipeline); + } + + Response v2InInterface(String endpoint, String version, String contentType, String accept, + ModelV2 body, RequestContext requestContext); + } + + private static final class InterfaceV2sServiceImpl implements InterfaceV2sService { + private static final ClientLogger HTTP_LOG = new ClientLogger(InterfaceV2sService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + private InterfaceV2sServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation(method = HttpMethod.POST, path = "/interface-v2/v2", expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response v2InInterface(@HostParam("endpoint") String endpoint, @HostParam("version") String version, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") ModelV2 body, RequestContext requestContext); + @Override + public Response v2InInterface(String endpoint, String version, String contentType, String accept, + ModelV2 body, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/versioning/added/api-version:" + version + "/interface-v2/v2"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, ModelV2.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/versioning/madeoptional/implementation/MadeOptionalClientImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/versioning/madeoptional/implementation/MadeOptionalClientImpl.java index fd16aae2bea..2b03b95f5b9 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/versioning/madeoptional/implementation/MadeOptionalClientImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/versioning/madeoptional/implementation/MadeOptionalClientImpl.java @@ -1,21 +1,24 @@ package versioning.madeoptional.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -import io.clientcore.core.http.annotations.HostParam; -import io.clientcore.core.http.annotations.HttpRequestInformation; -import io.clientcore.core.http.annotations.QueryParam; -import io.clientcore.core.http.annotations.UnexpectedResponseExceptionDetail; +import io.clientcore.core.http.models.HttpHeaderName; 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.SerializationFormat; +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 io.clientcore.core.utils.UriBuilder; +import java.lang.reflect.ParameterizedType; import versioning.madeoptional.MadeOptionalServiceVersion; import versioning.madeoptional.TestModel; @@ -102,29 +105,73 @@ public MadeOptionalClientImpl(HttpPipeline httpPipeline, Instrumentation instrum } /** - * The interface defining all the services for MadeOptionalClient to be used by the proxy service to perform REST - * calls. + * The HTTP operations for MadeOptionalClient. */ - @ServiceInterface(name = "MadeOptionalClient", host = "{endpoint}/versioning/made-optional/api-version:{version}") public interface MadeOptionalClientService { static MadeOptionalClientService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("versioning.madeoptional.implementation.MadeOptionalClientServiceImpl"); - return (MadeOptionalClientService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new MadeOptionalClientServiceImpl(pipeline); + } + + Response test(String endpoint, String version, String param, String contentType, String accept, + TestModel body, RequestContext requestContext); + } + + private static final class MadeOptionalClientServiceImpl implements MadeOptionalClientService { + private static final ClientLogger HTTP_LOG = new ClientLogger(MadeOptionalClientService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + private MadeOptionalClientServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation(method = HttpMethod.POST, path = "/test", expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response test(@HostParam("endpoint") String endpoint, @HostParam("version") String version, - @QueryParam("param") String param, @HeaderParam("Content-Type") String contentType, - @HeaderParam("Accept") String accept, @BodyParam("application/json") TestModel body, - RequestContext requestContext); + @Override + public Response test(String endpoint, String version, String param, String contentType, + String accept, TestModel body, RequestContext requestContext) { + UriBuilder uriBuilder + = UriBuilder.parse(endpoint + "/versioning/made-optional/api-version:" + version + "/test"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "param", true, param, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST).setUri(uriBuilder.toString()); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, TestModel.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/versioning/removed/implementation/RemovedClientImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/versioning/removed/implementation/RemovedClientImpl.java index 407394bfeae..300dff6fe66 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/versioning/removed/implementation/RemovedClientImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/versioning/removed/implementation/RemovedClientImpl.java @@ -1,20 +1,23 @@ package versioning.removed.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import versioning.removed.ModelV2; import versioning.removed.ModelV3; import versioning.removed.RemovedServiceVersion; @@ -102,33 +105,104 @@ public RemovedClientImpl(HttpPipeline httpPipeline, Instrumentation instrumentat } /** - * The interface defining all the services for RemovedClient to be used by the proxy service to perform REST calls. + * The HTTP operations for RemovedClient. */ - @ServiceInterface(name = "RemovedClient", host = "{endpoint}/versioning/removed/api-version:{version}") public interface RemovedClientService { static RemovedClientService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("versioning.removed.implementation.RemovedClientServiceImpl"); - return (RemovedClientService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); + return new RemovedClientServiceImpl(pipeline); + } + + Response v2(String endpoint, String version, String contentType, String accept, ModelV2 body, + RequestContext requestContext); + + Response modelV3(String endpoint, String version, String contentType, String accept, ModelV3 body, + RequestContext requestContext); + } + + private static final class RemovedClientServiceImpl implements RemovedClientService { + private static final ClientLogger HTTP_LOG = new ClientLogger(RemovedClientService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + + private RemovedClientServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; + } + + @Override + public Response v2(String endpoint, String version, String contentType, String accept, ModelV2 body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/versioning/removed/api-version:" + version + "/v2"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, ModelV2.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + @Override + public Response modelV3(String endpoint, String version, String contentType, String accept, + ModelV3 body, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/versioning/removed/api-version:" + version + "/v3"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, ModelV3.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); } - @HttpRequestInformation(method = HttpMethod.POST, path = "/v2", expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response v2(@HostParam("endpoint") String endpoint, @HostParam("version") String version, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") ModelV2 body, RequestContext requestContext); - - @HttpRequestInformation(method = HttpMethod.POST, path = "/v3", expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response modelV3(@HostParam("endpoint") String endpoint, @HostParam("version") String version, - @HeaderParam("Content-Type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") ModelV3 body, RequestContext requestContext); + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/versioning/renamedfrom/implementation/NewInterfacesImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/versioning/renamedfrom/implementation/NewInterfacesImpl.java index 4213ae7cd6f..17ce9a1627e 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/versioning/renamedfrom/implementation/NewInterfacesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/versioning/renamedfrom/implementation/NewInterfacesImpl.java @@ -1,20 +1,23 @@ package versioning.renamedfrom.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import versioning.renamedfrom.NewModel; import versioning.renamedfrom.RenamedFromServiceVersion; @@ -58,31 +61,71 @@ public RenamedFromServiceVersion getServiceVersion() { } /** - * The interface defining all the services for RenamedFromClientNewInterfaces to be used by the proxy service to - * perform REST calls. + * The HTTP operations for RenamedFromClientNewInterfaces. */ - @ServiceInterface( - name = "RenamedFromClientNewInterfaces", - host = "{endpoint}/versioning/renamed-from/api-version:{version}") public interface NewInterfacesService { static NewInterfacesService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("versioning.renamedfrom.implementation.NewInterfacesServiceImpl"); - return (NewInterfacesService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new NewInterfacesServiceImpl(pipeline); + } + + Response newOpInNewInterface(String endpoint, String version, String contentType, String accept, + NewModel body, RequestContext requestContext); + } + + private static final class NewInterfacesServiceImpl implements NewInterfacesService { + private static final ClientLogger HTTP_LOG = new ClientLogger(NewInterfacesService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + private NewInterfacesServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation(method = HttpMethod.POST, path = "/interface/test", expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response newOpInNewInterface(@HostParam("endpoint") String endpoint, - @HostParam("version") String version, @HeaderParam("Content-Type") String contentType, - @HeaderParam("Accept") String accept, @BodyParam("application/json") NewModel body, - RequestContext requestContext); + @Override + public Response newOpInNewInterface(String endpoint, String version, String contentType, + String accept, NewModel body, RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/versioning/renamed-from/api-version:" + version + "/interface/test"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, NewModel.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/versioning/renamedfrom/implementation/RenamedFromClientImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/versioning/renamedfrom/implementation/RenamedFromClientImpl.java index b7a0634a42d..40b3a2b8d71 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/versioning/renamedfrom/implementation/RenamedFromClientImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/versioning/renamedfrom/implementation/RenamedFromClientImpl.java @@ -1,21 +1,24 @@ package versioning.renamedfrom.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -import io.clientcore.core.http.annotations.HostParam; -import io.clientcore.core.http.annotations.HttpRequestInformation; -import io.clientcore.core.http.annotations.QueryParam; -import io.clientcore.core.http.annotations.UnexpectedResponseExceptionDetail; +import io.clientcore.core.http.models.HttpHeaderName; 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.SerializationFormat; +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 io.clientcore.core.utils.UriBuilder; +import java.lang.reflect.ParameterizedType; import versioning.renamedfrom.NewModel; import versioning.renamedfrom.RenamedFromServiceVersion; @@ -117,29 +120,73 @@ public RenamedFromClientImpl(HttpPipeline httpPipeline, Instrumentation instrume } /** - * The interface defining all the services for RenamedFromClient to be used by the proxy service to perform REST - * calls. + * The HTTP operations for RenamedFromClient. */ - @ServiceInterface(name = "RenamedFromClient", host = "{endpoint}/versioning/renamed-from/api-version:{version}") public interface RenamedFromClientService { static RenamedFromClientService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class.forName("versioning.renamedfrom.implementation.RenamedFromClientServiceImpl"); - return (RenamedFromClientService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new RenamedFromClientServiceImpl(pipeline); + } + + Response newOp(String endpoint, String version, String newQuery, String contentType, String accept, + NewModel body, RequestContext requestContext); + } + + private static final class RenamedFromClientServiceImpl implements RenamedFromClientService { + private static final ClientLogger HTTP_LOG = new ClientLogger(RenamedFromClientService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + private RenamedFromClientServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation(method = HttpMethod.POST, path = "/test", expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response newOp(@HostParam("endpoint") String endpoint, @HostParam("version") String version, - @QueryParam("newQuery") String newQuery, @HeaderParam("Content-Type") String contentType, - @HeaderParam("Accept") String accept, @BodyParam("application/json") NewModel body, - RequestContext requestContext); + @Override + public Response newOp(String endpoint, String version, String newQuery, String contentType, + String accept, NewModel body, RequestContext requestContext) { + UriBuilder uriBuilder + = UriBuilder.parse(endpoint + "/versioning/renamed-from/api-version:" + version + "/test"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "newQuery", true, newQuery, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST).setUri(uriBuilder.toString()); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, NewModel.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/versioning/returntypechangedfrom/implementation/ReturnTypeChangedFromClientImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/versioning/returntypechangedfrom/implementation/ReturnTypeChangedFromClientImpl.java index b8f2ecdb53c..c40b6e42976 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/versioning/returntypechangedfrom/implementation/ReturnTypeChangedFromClientImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/versioning/returntypechangedfrom/implementation/ReturnTypeChangedFromClientImpl.java @@ -1,20 +1,23 @@ package versioning.returntypechangedfrom.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -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.HttpHeaderName; 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.SerializationFormat; +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.lang.reflect.ParameterizedType; import versioning.returntypechangedfrom.ReturnTypeChangedFromServiceVersion; /** @@ -100,31 +103,71 @@ public ReturnTypeChangedFromClientImpl(HttpPipeline httpPipeline, Instrumentatio } /** - * The interface defining all the services for ReturnTypeChangedFromClient to be used by the proxy service to - * perform REST calls. + * The HTTP operations for ReturnTypeChangedFromClient. */ - @ServiceInterface( - name = "ReturnTypeChangedFromClient", - host = "{endpoint}/versioning/return-type-changed-from/api-version:{version}") public interface ReturnTypeChangedFromClientService { static ReturnTypeChangedFromClientService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz = Class - .forName("versioning.returntypechangedfrom.implementation.ReturnTypeChangedFromClientServiceImpl"); - return (ReturnTypeChangedFromClientService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new ReturnTypeChangedFromClientServiceImpl(pipeline); + } + + Response test(String endpoint, String version, String contentType, String accept, String body, + RequestContext requestContext); + } + + private static final class ReturnTypeChangedFromClientServiceImpl implements ReturnTypeChangedFromClientService { + private static final ClientLogger HTTP_LOG = new ClientLogger(ReturnTypeChangedFromClientService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + private ReturnTypeChangedFromClientServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation(method = HttpMethod.POST, path = "/test", expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response test(@HostParam("endpoint") String endpoint, @HostParam("version") String version, - @HeaderParam("content-type") String contentType, @HeaderParam("Accept") String accept, - @BodyParam("application/json") String body, RequestContext requestContext); + @Override + public Response test(String endpoint, String version, String contentType, String accept, String body, + RequestContext requestContext) { + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST) + .setUri(endpoint + "/versioning/return-type-changed-from/api-version:" + version + "/test"); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, String.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/versioning/typechangedfrom/implementation/TypeChangedFromClientImpl.java b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/versioning/typechangedfrom/implementation/TypeChangedFromClientImpl.java index a8636a459c4..64d5a93b947 100644 --- a/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/versioning/typechangedfrom/implementation/TypeChangedFromClientImpl.java +++ b/packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/versioning/typechangedfrom/implementation/TypeChangedFromClientImpl.java @@ -1,21 +1,24 @@ package versioning.typechangedfrom.implementation; import io.clientcore.core.annotations.ReturnType; -import io.clientcore.core.annotations.ServiceInterface; import io.clientcore.core.annotations.ServiceMethod; -import io.clientcore.core.http.annotations.BodyParam; -import io.clientcore.core.http.annotations.HeaderParam; -import io.clientcore.core.http.annotations.HostParam; -import io.clientcore.core.http.annotations.HttpRequestInformation; -import io.clientcore.core.http.annotations.QueryParam; -import io.clientcore.core.http.annotations.UnexpectedResponseExceptionDetail; +import io.clientcore.core.http.models.HttpHeaderName; 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.SerializationFormat; +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 io.clientcore.core.utils.UriBuilder; +import java.lang.reflect.ParameterizedType; import versioning.typechangedfrom.TestModel; import versioning.typechangedfrom.TypeChangedFromServiceVersion; @@ -102,32 +105,73 @@ public TypeChangedFromClientImpl(HttpPipeline httpPipeline, Instrumentation inst } /** - * The interface defining all the services for TypeChangedFromClient to be used by the proxy service to perform REST - * calls. + * The HTTP operations for TypeChangedFromClient. */ - @ServiceInterface( - name = "TypeChangedFromClient", - host = "{endpoint}/versioning/type-changed-from/api-version:{version}") public interface TypeChangedFromClientService { static TypeChangedFromClientService getNewInstance(HttpPipeline pipeline) { - try { - Class clazz - = Class.forName("versioning.typechangedfrom.implementation.TypeChangedFromClientServiceImpl"); - return (TypeChangedFromClientService) clazz.getMethod("getNewInstance", HttpPipeline.class) - .invoke(null, pipeline); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return new TypeChangedFromClientServiceImpl(pipeline); + } + + Response test(String endpoint, String version, String param, String contentType, String accept, + TestModel body, RequestContext requestContext); + } + + private static final class TypeChangedFromClientServiceImpl implements TypeChangedFromClientService { + private static final ClientLogger HTTP_LOG = new ClientLogger(TypeChangedFromClientService.class); + + private final HttpPipeline httpPipeline; + + private final JsonSerializer jsonSerializer = JsonSerializer.getInstance(); + + private final XmlSerializer xmlSerializer = XmlSerializer.getInstance(); + private TypeChangedFromClientServiceImpl(HttpPipeline httpPipeline) { + this.httpPipeline = httpPipeline; } - @HttpRequestInformation(method = HttpMethod.POST, path = "/test", expectedStatusCodes = { 200 }) - @UnexpectedResponseExceptionDetail - Response test(@HostParam("endpoint") String endpoint, @HostParam("version") String version, - @QueryParam("param") String param, @HeaderParam("Content-Type") String contentType, - @HeaderParam("Accept") String accept, @BodyParam("application/json") TestModel body, - RequestContext requestContext); + @Override + public Response test(String endpoint, String version, String param, String contentType, + String accept, TestModel body, RequestContext requestContext) { + UriBuilder uriBuilder + = UriBuilder.parse(endpoint + "/versioning/type-changed-from/api-version:" + version + "/test"); + GeneratedCodeUtils.addQueryParameter(uriBuilder, "param", true, param, true); + HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.POST).setUri(uriBuilder.toString()); + if (accept != null) { + httpRequest.getHeaders().add(HttpHeaderName.fromString("Accept"), accept); + } + if (body != null) { + httpRequest.getHeaders().set(HttpHeaderName.CONTENT_TYPE, contentType); + SerializationFormat requestFormat + = CoreUtils.serializationFormatFromContentType(httpRequest.getHeaders()); + httpRequest.setBody(BinaryData.fromObject(body, + this.xmlSerializer.supportsFormat(requestFormat) ? this.xmlSerializer : this.jsonSerializer)); + } + httpRequest.setContext(requestContext); + httpRequest.getContext().getRequestCallback().accept(httpRequest); + Response networkResponse = this.httpPipeline.send(httpRequest); + int responseCode = networkResponse.getStatusCode(); + boolean expectedResponse = responseCode == 200; + if (!expectedResponse) { + GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, this.jsonSerializer, + this.xmlSerializer, null, null, HTTP_LOG); + } + return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), + this.decodeResponse(networkResponse.getValue(), + CoreUtils.createParameterizedType(Response.class, TestModel.class), + CoreUtils.serializationFormatFromContentType(networkResponse.getHeaders()))); + } + + private T decodeResponse(BinaryData value, ParameterizedType type, SerializationFormat format) { + if (this.jsonSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type); + } + if (this.xmlSerializer.supportsFormat(format)) { + return CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type); + } + throw HTTP_LOG.throwableAtError() + .addKeyValue("serializationFormat", format.name()) + .log("None of the provided serializers support the format.", UnsupportedOperationException::new); + } } /** diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/extension/plugin/JavaSettings.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/extension/plugin/JavaSettings.java index 5477c5ef271..25bb806d9eb 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/extension/plugin/JavaSettings.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/extension/plugin/JavaSettings.java @@ -441,6 +441,10 @@ public boolean useRestProxy() { return this.useRestProxy; } + public boolean generateClientCoreHttpOperations() { + return !isAzureV1() && !useRestProxy(); + } + private final String keyCredentialHeaderName; /** diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/MethodGroupClient.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/MethodGroupClient.java index be344014e53..d803d11948e 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/MethodGroupClient.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/MethodGroupClient.java @@ -166,7 +166,9 @@ public final void addImportsTo(Set imports, boolean includeImplementatio if (includeImplementationImports) { // ClassType proxyType = settings.isAzureOrFluent() ? ClassType.AzureProxy : ClassType.RestProxy; - imports.add(ClassType.REST_PROXY.getFullName()); + if (!settings.generateClientCoreHttpOperations()) { + imports.add(ClassType.REST_PROXY.getFullName()); + } if (settings.isGenerateClientInterfaces()) { String interfacePackage = ClientModelUtil.getServiceClientInterfacePackageName(); diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/Proxy.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/Proxy.java index 3ba4a4c9c46..f6f7cdbaa88 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/Proxy.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/Proxy.java @@ -67,9 +67,12 @@ public final List getMethods() { * implementations. */ public void addImportsTo(Set imports, boolean includeImplementationImports, JavaSettings settings) { - if (includeImplementationImports) { + if (includeImplementationImports && !settings.generateClientCoreHttpOperations()) { Annotation.HOST.addImportsTo(imports); Annotation.SERVICE_INTERFACE.addImportsTo(imports); + if (!settings.isAzureV1()) { + ClassType.REST_PROXY.addImportsTo(imports, false); + } } for (ProxyMethod method : getMethods()) { diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/ProxyMethod.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/ProxyMethod.java index 317df1b3be1..e29d1648a67 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/ProxyMethod.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/ProxyMethod.java @@ -438,9 +438,18 @@ private IType mapToSyncType(IType type) { * implementations. */ public void addImportsTo(Set imports, boolean includeImplementationImports, JavaSettings settings) { - Annotation.HTTP_REQUEST_INFORMATION.addImportsTo(imports); - Annotation.UNEXPECTED_RESPONSE_EXCEPTION_INFORMATION.addImportsTo(imports); + boolean direct = settings.generateClientCoreHttpOperations(); + if (!direct) { + Annotation.HTTP_REQUEST_INFORMATION.addImportsTo(imports); + Annotation.UNEXPECTED_RESPONSE_EXCEPTION_INFORMATION.addImportsTo(imports); + } ClassType.HTTP_RESPONSE_EXCEPTION.addImportsTo(imports, false); + if (includeImplementationImports && direct) { + returnType.getClientType().addImportsTo(imports, true); + parameters.forEach(parameter -> parameter.getWireType().addImportsTo(imports, true)); + allParameters.forEach(parameter -> parameter.addImportsTo(imports, true, settings)); + return; + } if (includeImplementationImports) { if (getUnexpectedResponseExceptionType() != null) { Annotation.UNEXPECTED_RESPONSE_EXCEPTION_TYPE.addImportsTo(imports); diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/ProxyMethodParameter.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/ProxyMethodParameter.java index 5f72ed66918..c5d2820cca9 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/ProxyMethodParameter.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/ProxyMethodParameter.java @@ -235,8 +235,8 @@ public final String convertFromClientType(String source, String target, boolean * implementations. */ public void addImportsTo(Set imports, boolean includeImplementationImports, JavaSettings settings) { - if (getRequestParameterLocation() - != RequestParameterLocation.NONE/* && getRequestParameterLocation() != RequestParameterLocation.FormData */) { + if (!settings.generateClientCoreHttpOperations() + && getRequestParameterLocation() != RequestParameterLocation.NONE) { if (settings.isAzureV1()) { imports.add(String.format("%1$s.annotation.%2$sParam", ExternalPackage.CORE.getPackageName(), CodeNamer.toPascalCase(getRequestParameterLocation().toString()))); @@ -262,7 +262,7 @@ public void addImportsTo(Set imports, boolean includeImplementationImpor // imports.add(String.format("com.azure.core.annotation.FormParam")); // } - if (!settings.isAzureV1()) { + if (!settings.isAzureV1() && !settings.generateClientCoreHttpOperations()) { imports.add("io.clientcore.core.http.models.HttpMethod"); } diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/ServiceClient.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/ServiceClient.java index b481085095e..d20b04ed2d7 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/ServiceClient.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/ServiceClient.java @@ -312,7 +312,7 @@ public final void addImportsTo(Set imports, boolean includeImplementatio if (settings.isFluentPremium()) { imports.add("com.azure.resourcemanager.resources.fluentcore.AzureServiceClient"); } - if (!getClientMethods().isEmpty()) { + if (!getClientMethods().isEmpty() && !settings.generateClientCoreHttpOperations()) { ClassType.REST_PROXY.addImportsTo(imports, false); } diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/ClientCoreHttpOperationTemplate.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/ClientCoreHttpOperationTemplate.java new file mode 100644 index 00000000000..be4a3da5098 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/ClientCoreHttpOperationTemplate.java @@ -0,0 +1,563 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.typespec.http.client.generator.core.template; + +import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.RequestParameterLocation; +import com.microsoft.typespec.http.client.generator.core.extension.plugin.JavaSettings; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ArrayType; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClassType; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClientModel; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.GenericType; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.IType; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ListType; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.PrimitiveType; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.Proxy; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ProxyMethod; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ProxyMethodParameter; +import com.microsoft.typespec.http.client.generator.core.model.javamodel.JavaBlock; +import com.microsoft.typespec.http.client.generator.core.model.javamodel.JavaClass; +import com.microsoft.typespec.http.client.generator.core.util.ClientModelUtil; +import com.microsoft.typespec.http.client.generator.core.util.TemplateUtil; +import io.clientcore.core.http.models.HttpHeaderName; +import io.clientcore.core.http.models.HttpMethod; +import io.clientcore.core.http.models.HttpRequest; +import io.clientcore.core.http.models.Response; +import io.clientcore.core.implementation.utils.UriEscapers; +import io.clientcore.core.instrumentation.logging.ClientLogger; +import io.clientcore.core.models.binarydata.BinaryData; +import io.clientcore.core.serialization.SerializationFormat; +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 io.clientcore.core.utils.UriBuilder; +import java.lang.reflect.ParameterizedType; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.function.Consumer; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + +/** + * Generates ClientCore HTTP operations directly from the proxy model, replacing the ClientCore annotation processor. + * + *

By default, {@link ProxyTemplate} uses this template for the {@code generic} and {@code azurev2} flavors. + * The proxy template emits the service interface and its factory; this template emits the private nested class + * that the factory instantiates. Methods with an existing default implementation are inherited from the interface. + * Enabling {@code use-rest-proxy} bypasses this template and retains the annotated interface for runtime reflection. + * + *

{@link ProxyMethod} describes the HTTP verb, route, and response types, while {@link ProxyMethodParameter} + * describes how each wire value is bound to the request. The emitted methods use that metadata to build requests, + * send them through the client's existing HTTP pipeline, and handle success and error responses. + */ +final class ClientCoreHttpOperationTemplate { + private static final Pattern PATH_PLACEHOLDER = Pattern.compile("\\{([^{}]+)}"); + private static final String RESPONSE_DECODER + = " T decodeResponse(BinaryData value, " + "ParameterizedType type, SerializationFormat format)"; + + static String implementationName(Proxy proxy) { + return proxy.getName() + "Impl"; + } + + static List parameterDeclarations(ProxyMethod method) { + return method.getParameters() + .stream() + .map(parameter -> parameter.getWireType() + " " + parameter.getName()) + .collect(Collectors.toList()); + } + + static void addImportsTo(Proxy proxy, Set imports, JavaSettings settings) { + if (proxy == null || !settings.generateClientCoreHttpOperations()) { + return; + } + + for (ProxyMethod method : proxy.getMethods()) { + if (method.getImplementation() != null) { + continue; + } + imports.add(HttpMethod.class.getName()); + imports.add(HttpRequest.class.getName()); + imports.add(Response.class.getName()); + imports.add(BinaryData.class.getName()); + imports.add(GeneratedCodeUtils.class.getName()); + imports.add(ClientLogger.class.getName()); + imports.add(JsonSerializer.class.getName()); + imports.add(XmlSerializer.class.getName()); + if (!bindings(method, RequestParameterLocation.QUERY).isEmpty()) { + imports.add(UriBuilder.class.getName()); + } + for (ProxyMethodParameter parameter : method.getParameters()) { + if (parameter.getRequestParameterLocation() == RequestParameterLocation.PATH + && !parameter.getAlreadyEncoded() + && ClassType.STRING.equals(parameter.getWireType())) { + imports.add(UriEscapers.class.getName()); + } else if (parameter.getRequestParameterLocation() == RequestParameterLocation.HEADER + && isOffsetDateTime(parameter.getWireType())) { + imports.add(DateTimeFormatter.class.getName()); + } + } + ProxyMethodParameter body = bodyParameter(method); + if (body != null || !bindings(method, RequestParameterLocation.HEADER).isEmpty()) { + imports.add(HttpHeaderName.class.getName()); + } + if ((body != null && serializesBody(method, body)) || decodesResponse(method)) { + imports.add(CoreUtils.class.getName()); + imports.add(SerializationFormat.class.getName()); + } + if (decodesResponse(method)) { + imports.add(ParameterizedType.class.getName()); + } + if (ArrayType.BYTE_ARRAY.equals(entityType(method)) + && ClassType.BASE_64_URL.equals(method.getReturnValueWireType())) { + ClassType.BASE_64_URL.addImportsTo(imports, false); + } + if (!statusErrorTypes(method, settings).isEmpty()) { + imports.add(CoreUtils.class.getName()); + imports.add(Map.class.getName()); + } + if (!"null".equals(defaultErrorType(method, settings))) { + imports.add(CoreUtils.class.getName()); + } + } + } + + static void write(Proxy proxy, JavaClass owner, JavaSettings settings) { + List operations = proxy.getMethods() + .stream() + .filter(method -> method.getImplementation() == null) + .collect(Collectors.toList()); + String impl = implementationName(proxy); + owner.privateStaticFinalClass(impl + " implements " + proxy.getName(), service -> { + if (!operations.isEmpty()) { + service.privateStaticFinalVariable( + "ClientLogger HTTP_LOG = new ClientLogger(" + proxy.getName() + ".class)"); + service.privateFinalMemberVariable("HttpPipeline", "httpPipeline"); + service.privateFinalMemberVariable("JsonSerializer", "jsonSerializer", "JsonSerializer.getInstance()"); + service.privateFinalMemberVariable("XmlSerializer", "xmlSerializer", "XmlSerializer.getInstance()"); + } + service.privateConstructor(impl + "(HttpPipeline httpPipeline)", constructor -> { + if (!operations.isEmpty()) { + constructor.line("this.httpPipeline = httpPipeline;"); + } + }); + for (ProxyMethod method : operations) { + service.annotation("Override"); + service.publicMethod( + method.getReturnType().getClientType() + " " + method.getName() + "(" + + String.join(", ", parameterDeclarations(method)) + ")", + body -> writeOperation(proxy, method, body, settings)); + } + if (operations.stream().anyMatch(ClientCoreHttpOperationTemplate::decodesResponse)) { + service.privateMethod(RESPONSE_DECODER, body -> { + body.ifBlock("this.jsonSerializer.supportsFormat(format)", + json -> json.methodReturn("CoreUtils.decodeNetworkResponse(value, this.jsonSerializer, type)")); + body.ifBlock("this.xmlSerializer.supportsFormat(format)", + xml -> xml.methodReturn("CoreUtils.decodeNetworkResponse(value, this.xmlSerializer, type)")); + body.line("throw HTTP_LOG.throwableAtError().addKeyValue(\"serializationFormat\", format.name())" + + ".log(\"None of the provided serializers support the format.\", " + + "UnsupportedOperationException::new);"); + }); + } + }); + } + + private static void writeOperation(Proxy proxy, ProxyMethod method, JavaBlock block, JavaSettings settings) { + // Reserve parameter names so generated locals like httpRequest cannot collide with them. + Set names + = method.getParameters().stream().map(ProxyMethodParameter::getName).collect(Collectors.toSet()); + String request = localName(names, "httpRequest"); + String uri = uriExpression(proxy, method); + Map query = bindings(method, RequestParameterLocation.QUERY); + if (!query.isEmpty()) { + String builder = localName(names, "uriBuilder"); + block.line("UriBuilder %s = UriBuilder.parse(%s);", builder, uri); + // The mapper supplies lists for exploded queries and delimited strings for non-exploded queries. + query.forEach((name, parameter) -> block.line("GeneratedCodeUtils.addQueryParameter(%s, %s, true, %s, %s);", + builder, literal(name), parameter.getName(), !parameter.getAlreadyEncoded())); + uri = builder + ".toString()"; + } + block.line("HttpRequest %s = new HttpRequest().setMethod(HttpMethod.%s).setUri(%s);", request, + method.getHttpMethod(), uri); + writeHeaders(method, block, request); + String requestFormat = writeRequestBody(method, block, request, names); + // Run the callback after setting headers and body so callers can inspect or modify the complete request. + method.getParameters() + .stream() + .filter(parameter -> ClassType.REQUEST_CONTEXT.equals(parameter.getWireType())) + .findFirst() + .ifPresent(context -> { + block.line("%s.setContext(%s);", request, context.getName()); + block.line("%s.getContext().getRequestCallback().accept(%s);", request, request); + }); + + String response = localName(names, "networkResponse"); + String responseDeclaration = "Response " + response + " = this.httpPipeline.send(" + request + ")"; + IType entity = entityType(method); + // Match the processor's close behavior. Raw BinaryData and InputStream results must remain usable by the + // caller. + boolean closeResponse = isVoid(entity) + || isBooleanHead(method) + || ArrayType.BYTE_ARRAY.equals(entity) + || (entity instanceof ListType && ClassType.BINARY_DATA.equals(((ListType) entity).getElementType())); + Consumer handleResponse + = responseBlock -> writeResponse(method, responseBlock, response, requestFormat, names, settings); + if (closeResponse) { + block.tryBlock(responseDeclaration, handleResponse); + } else { + block.line(responseDeclaration + ";"); + handleResponse.accept(block); + } + } + + /** + * Builds a Java expression for the request URI before adding query parameters. Absolute URLs bypass the + * service host. Placeholders are matched by wire name, and only unencoded string path values are escaped. + * + * @param proxy the proxy that supplies the service host + * @param method the operation that supplies the path and parameter bindings + * @return the Java expression for the request URI + */ + private static String uriExpression(Proxy proxy, ProxyMethod method) { + String host = proxy.getBaseURL(); + String path = method.getUrlPath(); + if (path == null) { + path = ""; + } + boolean hasHost = !CoreUtils.isNullOrEmpty(host); + if (hasHost && !"/".equals(host) && !path.contains("://")) { + path = host + (path.startsWith("/") ? path : "/" + path); + } + + Map substitutions = new LinkedHashMap<>(); + for (ProxyMethodParameter parameter : method.getParameters()) { + if (parameter.getRequestParameterLocation() == RequestParameterLocation.URI + || parameter.getRequestParameterLocation() == RequestParameterLocation.PATH) { + substitutions.put(parameter.getRequestParameterName(), parameter); + } + } + if (path.contains("{nextLink}") && substitutions.containsKey("nextLink")) { + // A continuation link already contains its endpoint, path, and encoding. + return substitutions.get("nextLink").getName(); + } + + List parts = new ArrayList<>(); + if (!hasHost) { + // Without a host template, URI-bound parameters supply the base endpoint in declaration order. + method.getParameters() + .stream() + .filter(parameter -> parameter.getRequestParameterLocation() == RequestParameterLocation.URI) + .forEach(parameter -> parts.add(stringValue(parameter))); + if (!parts.isEmpty()) { + parts.add("\"/\""); + } + } + Matcher placeholder = PATH_PLACEHOLDER.matcher(path); + int start = 0; + while (placeholder.find()) { + if (start < placeholder.start()) { + parts.add(literal(path.substring(start, placeholder.start()))); + } + ProxyMethodParameter parameter = substitutions.get(placeholder.group(1)); + if (parameter == null) { + throw new IllegalArgumentException("Unbound URL placeholder " + placeholder.group() + " in " + + proxy.getName() + "." + method.getName()); + } + if (parameter.getRequestParameterLocation() == RequestParameterLocation.PATH + && !parameter.getAlreadyEncoded() + && ClassType.STRING.equals(parameter.getWireType())) { + parts.add("UriEscapers.PATH_ESCAPER.escape(" + parameter.getName() + ")"); + } else { + parts.add(stringValue(parameter)); + } + start = placeholder.end(); + } + if (start < path.length() || parts.isEmpty()) { + parts.add(literal(path.substring(start))); + } + return String.join(" + ", parts); + } + + private static Map bindings(ProxyMethod method, RequestParameterLocation location) { + Map bindings = new LinkedHashMap<>(); + for (ProxyMethodParameter parameter : method.getParameters()) { + if (parameter.getRequestParameterLocation() != location) { + continue; + } + String name = parameter.getRequestParameterName(); + if (location == RequestParameterLocation.HEADER + && !CoreUtils.isNullOrEmpty(parameter.getHeaderCollectionPrefix())) { + name = parameter.getHeaderCollectionPrefix(); + } + if (!CoreUtils.isNullOrEmpty(name)) { + // Match the processor: the first header or query parameter for a wire name wins. + bindings.putIfAbsent(name, parameter); + } + } + return bindings; + } + + private static void writeHeaders(ProxyMethod method, JavaBlock block, String request) { + bindings(method, RequestParameterLocation.HEADER).forEach((name, parameter) -> { + if ("Content-Type".equalsIgnoreCase(name)) { + return; // As in the annotated implementation, the body determines Content-Type. + } + String value = isOffsetDateTime(parameter.getWireType()) + ? parameter.getName() + ".format(DateTimeFormatter.ISO_INSTANT)" + : stringValue(parameter); + String statement + = request + ".getHeaders().add(HttpHeaderName.fromString(" + literal(name) + "), " + value + ");"; + if (parameter.getWireType() instanceof PrimitiveType) { + block.line(statement); + } else { + block.ifBlock(parameter.getName() + " != null", header -> header.line(statement)); + } + }); + } + + private static ProxyMethodParameter bodyParameter(ProxyMethod method) { + // The processor did not treat @FormParam as a request body binding. + if ("application/x-www-form-urlencoded".equals(method.getRequestContentType())) { + return null; + } + ProxyMethodParameter body = null; + for (ProxyMethodParameter parameter : method.getParameters()) { + if (parameter.getRequestParameterLocation() == RequestParameterLocation.BODY) { + body = parameter; + } + } + return body; + } + + /** + * Emits the body and its Content-Type together. To match the processor, typed response decoding reuses the + * serialization format computed for a primitive request body. For reference types, any format variable is scoped + * to the body null check, so response decoding uses the response Content-Type instead. + * + * @param method the operation whose body binding and media type are used + * @param block the generated method body + * @param request the generated request variable name + * @param names the names already in use in the generated method + * @return the serialization format variable if available to response decoding, or {@code null} + */ + private static String writeRequestBody(ProxyMethod method, JavaBlock block, String request, Set names) { + ProxyMethodParameter body = bodyParameter(method); + if (body == null) { + return null; + } + // The processor let a parameter named contentType override the declared media type. + String mediaType = method.getParameters() + .stream() + .filter(parameter -> "contentType".equals(parameter.getName())) + .findFirst() + .map(ClientCoreHttpOperationTemplate::stringValue) + .orElseGet(() -> literal( + method.getRequestContentType() == null ? "application/json" : method.getRequestContentType())); + boolean serialize = serializesBody(method, body); + String format = serialize ? localName(names, "requestFormat") : null; + if (body.getWireType() instanceof PrimitiveType) { + writeNonNullBody(block, request, body, mediaType, format); + return format; + } + block.ifBlock(body.getName() + " != null", + present -> writeNonNullBody(present, request, body, mediaType, format)); + return null; + } + + private static boolean serializesBody(ProxyMethod method, ProxyMethodParameter body) { + String contentType = method.getRequestContentType(); + IType type = body.getWireType(); + // A declared JSON body is serialized even for String. For other media types, raw text and bytes pass through. + return (contentType != null && "application/json".equalsIgnoreCase(contentType.trim())) + || !(ClassType.BINARY_DATA.equals(type) + || ClassType.STRING.equals(type) + || ClassType.BYTE_BUFFER.equals(type) + || ArrayType.BYTE_ARRAY.equals(type)); + } + + private static void writeNonNullBody(JavaBlock block, String request, ProxyMethodParameter body, String mediaType, + String format) { + block.line("%s.getHeaders().set(HttpHeaderName.CONTENT_TYPE, %s);", request, mediaType); + String value = body.getName(); + IType type = body.getWireType(); + if (format != null) { + block.line("SerializationFormat %s = CoreUtils.serializationFormatFromContentType(%s.getHeaders());", + format, request); + block.line("%s.setBody(BinaryData.fromObject(%s, this.xmlSerializer.supportsFormat(%s)" + + " ? this.xmlSerializer : this.jsonSerializer));", request, value, format); + } else if (ClassType.BINARY_DATA.equals(type)) { + // The processor did not attach raw bodies of unknown length. For known lengths, setBody sets + // Content-Length. + block.ifBlock(value + ".getLength() != null", known -> known.line("%s.setBody(%s);", request, value)); + } else if (ClassType.STRING.equals(type)) { + block.line("%s.setBody(BinaryData.fromString(%s));", request, value); + } else { + block.line("%s.setBody(BinaryData.fromBytes(%s));", request, + ClassType.BYTE_BUFFER.equals(type) ? value + ".array()" : value); + } + } + + private static void writeResponse(ProxyMethod method, JavaBlock block, String response, String requestFormat, + Set names, JavaSettings settings) { + String status = localName(names, "responseCode"); + block.line("int %s = %s.getStatusCode();", status, response); + List statuses = method.getResponseExpectedStatusCodes(); + // Only an empty list accepts any 2xx. Even a boolean HEAD operation must pass this check before returning. + String success = CoreUtils.isNullOrEmpty(statuses) + ? status + " >= 200 && " + status + " < 300" + : statuses.stream().map(code -> status + " == " + code).collect(Collectors.joining(" || ")); + String expected = localName(names, "expectedResponse"); + block.line("boolean %s = %s;", expected, success); + block.ifBlock("!" + expected, unexpected -> { + // The runtime prefers a status-specific error body type over the default and closes the failed response. + String defaultType = defaultErrorType(method, settings); + Map errorTypes = statusErrorTypes(method, settings); + String specificTypes = errorTypes.isEmpty() + ? "null" + : errorTypes.entrySet() + .stream() + .map(entry -> "Map.entry(" + entry.getKey() + ", " + entry.getValue() + ")") + .collect(Collectors.joining(", ", "Map.ofEntries(", ")")); + unexpected.line("GeneratedCodeUtils.handleUnexpectedResponse(%s, %s, this.jsonSerializer, " + + "this.xmlSerializer, %s, %s, HTTP_LOG);", status, response, defaultType, specificTypes); + }); + + IType type = entityType(method); + boolean wrapped = isResponse(method.getReturnType().getClientType()); + String value; + if (type == PrimitiveType.VOID && !wrapped) { + return; + } else if (isVoid(type)) { + value = "null"; + } else if (isBooleanHead(method)) { + value = expected; + } else if (ClassType.BINARY_DATA.equals(type)) { + block.methodReturn(wrapped ? response : response + ".getValue()"); + return; + } else if (ClassType.INPUT_STREAM.equals(type)) { + value = response + ".getValue().toStream()"; + } else if (ArrayType.BYTE_ARRAY.equals(type)) { + String bytes = localName(names, "responseBody"); + block.line("BinaryData %s = %s.getValue();", bytes, response); + String decode = bytes + ".toBytes()"; + // Both raw and base64url responses return byte[]; the wire type determines whether to decode. + if (ClassType.BASE_64_URL.equals(method.getReturnValueWireType())) { + decode = "new " + ClassType.BASE_64_URL.getName() + "(" + decode + ").decodedBytes()"; + } + value = bytes + " == null ? null : " + decode; + } else { + // CoreUtils unwraps a Response token and passes its entity Type to the serializer. + String token = "CoreUtils.createParameterizedType(Response.class, " + typeToken(type) + ")"; + String format = requestFormat == null + ? "CoreUtils.serializationFormatFromContentType(" + response + ".getHeaders())" + : requestFormat; + value = "this.decodeResponse(" + response + ".getValue(), " + token + ", " + format + ")"; + } + block.methodReturn(wrapped + ? "new Response<>(" + response + ".getRequest(), " + status + ", " + response + ".getHeaders(), " + value + + ")" + : value); + } + + private static boolean isResponse(IType type) { + return ClassType.RESPONSE.equals(type) + || (type instanceof GenericType + && "Response".equals(((GenericType) type).getName()) + && ClassType.RESPONSE.getPackage().equals(((GenericType) type).getPackage())); + } + + private static IType entityType(ProxyMethod method) { + IType type = method.getReturnType().getClientType(); + if (isResponse(type)) { + return type instanceof GenericType + ? ((GenericType) type).getTypeArguments()[0].asNullable() + : ClassType.BINARY_DATA; + } + return type; + } + + private static boolean isBooleanHead(ProxyMethod method) { + return HttpMethod.HEAD == method.getHttpMethod() && ClassType.BOOLEAN.equals(entityType(method).asNullable()); + } + + private static boolean isVoid(IType type) { + return ClassType.VOID.equals(type.asNullable()); + } + + private static boolean decodesResponse(ProxyMethod method) { + IType type = entityType(method); + return !isVoid(type) + && !isBooleanHead(method) + && !ClassType.BINARY_DATA.equals(type) + && !ClassType.INPUT_STREAM.equals(type) + && !ArrayType.BYTE_ARRAY.equals(type); + } + + private static String typeToken(IType type) { + if (type instanceof GenericType) { + GenericType generic = (GenericType) type; + // Preserve nested type arguments for deserialization; a raw class literal would erase them. + return Arrays.stream(generic.getTypeArguments()) + .map(argument -> typeToken(argument.asNullable())) + .collect(Collectors.joining(", ", "CoreUtils.createParameterizedType(" + generic.getName() + ".class, ", + ")")); + } + return type + ".class"; + } + + private static String defaultErrorType(ProxyMethod method, JavaSettings settings) { + ClientModel model = errorModel(method.getUnexpectedResponseExceptionType(), settings); + return model == null ? "null" : "CoreUtils.createParameterizedType(" + model.getFullName() + ".class)"; + } + + private static Map statusErrorTypes(ProxyMethod method, JavaSettings settings) { + Map types = new LinkedHashMap<>(); + if (method.getUnexpectedResponseExceptionTypes() != null) { + method.getUnexpectedResponseExceptionTypes().forEach((exception, codes) -> { + ClientModel model = errorModel(exception, settings); + // An explicit status without a model still needs an Object entry to override the default error model. + String type = "CoreUtils.createParameterizedType(" + (model == null ? "Object" : model.getFullName()) + + ".class)"; + codes.forEach(code -> types.put(code, type)); + }); + } + return types; + } + + private static ClientModel errorModel(ClassType exception, JavaSettings settings) { + return exception == null || settings.isUseDefaultHttpStatusCodeToExceptionTypeMapping() + ? null + : ClientModelUtil.getErrorModelFromException(exception); + } + + private static boolean isOffsetDateTime(IType type) { + return type instanceof ClassType && "java.time.OffsetDateTime".equals(((ClassType) type).getFullName()); + } + + private static String stringValue(ProxyMethodParameter parameter) { + return ClassType.STRING.equals(parameter.getWireType()) + ? parameter.getName() + : "String.valueOf(" + parameter.getName() + ")"; + } + + private static String literal(String value) { + return "\"" + TemplateUtil.escapeString(value) + "\""; + } + + private static String localName(Set names, String preferred) { + String name = preferred; + for (int i = 1; !names.add(name); i++) { + name = preferred + i; + } + return name; + } + + private ClientCoreHttpOperationTemplate() { + } +} diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/MethodGroupTemplate.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/MethodGroupTemplate.java index 1384b3190cd..cf796453efd 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/MethodGroupTemplate.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/MethodGroupTemplate.java @@ -17,7 +17,6 @@ import com.microsoft.typespec.http.client.generator.core.util.TemplateUtil; import io.clientcore.core.serialization.ObjectSerializer; import io.clientcore.core.utils.CoreUtils; -import java.lang.reflect.InvocationTargetException; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; @@ -44,11 +43,11 @@ public final void write(MethodGroupClient methodGroupClient, JavaFile javaFile) } methodGroupClient.addImportsTo(imports, true, settings); + ClientCoreHttpOperationTemplate.addImportsTo(methodGroupClient.getProxy(), imports, settings); String serviceClientPackageName = ClientModelUtil.getServiceClientPackageName(methodGroupClient.getServiceClientName()); imports.add(String.format("%1$s.%2$s", serviceClientPackageName, methodGroupClient.getServiceClientName())); - imports.add(InvocationTargetException.class.getName()); imports.add(ObjectSerializer.class.getName()); ClassType.INSTRUMENTATION.addImportsTo(imports, false); ClassType.HTTP_PIPELINE.addImportsTo(imports, false); diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/ProxyTemplate.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/ProxyTemplate.java index b518098c4a9..36143c4dca8 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/ProxyTemplate.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/ProxyTemplate.java @@ -37,13 +37,15 @@ public static ProxyTemplate getInstance() { public final void write(Proxy restAPI, JavaClass classBlock) { JavaSettings settings = JavaSettings.getInstance(); if (restAPI != null) { - classBlock.javadocComment(comment -> comment.description(String.format( - "The interface defining all the services for %1$s to be used by the proxy service to perform REST calls.", + boolean direct = settings.generateClientCoreHttpOperations(); + classBlock.javadocComment(comment -> comment.description(String.format(direct + ? "The HTTP operations for %1$s." + : "The interface defining all the services for %1$s to be used by the proxy service to perform REST calls.", restAPI.getClientTypeName()))); if (settings.isAzureV1()) { classBlock.annotation("Host(\"" + restAPI.getBaseURL() + "\")"); classBlock.annotation("ServiceInterface(name = \"" + restAPI.getClientTypeName() + "\")"); - } else { + } else if (!direct) { classBlock.annotation(String.format("ServiceInterface(name = \"%1$s\", host = \"%2$s\")", restAPI.getClientTypeName(), restAPI.getBaseURL())); } @@ -54,22 +56,21 @@ public final void write(Proxy restAPI, JavaClass classBlock) { } classBlock.interfaceBlock(visibility, restAPI.getName(), interfaceBlock -> { - if (settings.isAzureV2() || !settings.isAzureV1()) { + if (!settings.isAzureV1()) { interfaceBlock.staticMethod(JavaVisibility.PackagePrivate, restAPI.getName() + " getNewInstance(HttpPipeline pipeline)", - javaBlock -> javaBlock.tryBlock(tryBlock -> { - tryBlock - .line("Class clazz = Class.forName(" + "\"" + JavaSettings.getInstance().getPackage() - + ".implementation." + restAPI.getName() + "Impl" + "\");"); - tryBlock.line("return (" + restAPI.getName() + ") clazz.getMethod(\"getNewInstance\", " - + "HttpPipeline.class).invoke(null, pipeline);"); - }) - .catchBlock( - "ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e", - catchBlock -> catchBlock.line("throw new RuntimeException(e);"))); + javaBlock -> javaBlock.methodReturn(direct + ? "new " + ClientCoreHttpOperationTemplate.implementationName(restAPI) + "(pipeline)" + : "RestProxy.create(" + restAPI.getName() + ".class, pipeline)")); } for (ProxyMethod restAPIMethod : restAPI.getMethods()) { + if (direct) { + writeProxyMethodSignature(ClientCoreHttpOperationTemplate.parameterDeclarations(restAPIMethod), + restAPIMethod, interfaceBlock); + continue; + } + if (restAPIMethod.getRequestContentType().equals("multipart/form-data") || restAPIMethod.getRequestContentType().equals("application/x-www-form-urlencoded")) { interfaceBlock.lineComment("@Multipart not supported by " + ClassType.REST_PROXY.getName()); @@ -133,6 +134,9 @@ public final void write(Proxy restAPI, JavaClass classBlock) { writeProxyMethodSignature(parameterDeclarationList, restAPIMethod, interfaceBlock); } }); + if (direct) { + ClientCoreHttpOperationTemplate.write(restAPI, classBlock, settings); + } } } @@ -260,8 +264,8 @@ protected void writeSingleUnexpectedException(ProxyMethod restAPIMethod, JavaInt } } - protected void writeProxyMethodSignature(java.util.ArrayList parameterDeclarationList, - ProxyMethod restAPIMethod, JavaInterface interfaceBlock) { + protected void writeProxyMethodSignature(List parameterDeclarationList, ProxyMethod restAPIMethod, + JavaInterface interfaceBlock) { if (restAPIMethod.getImplementation() != null) { String parameterDeclarations = String.join(", ", parameterDeclarationList); IType restAPIMethodReturnValueClientType = restAPIMethod.getReturnType().getClientType(); diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/ServiceClientTemplate.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/ServiceClientTemplate.java index 6ea3ce5b240..a4e8cda0e36 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/ServiceClientTemplate.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/ServiceClientTemplate.java @@ -24,7 +24,6 @@ import com.microsoft.typespec.http.client.generator.core.util.ModelNamer; import com.microsoft.typespec.http.client.generator.core.util.TemplateUtil; import io.clientcore.core.serialization.ObjectSerializer; -import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedHashSet; @@ -76,11 +75,11 @@ public final void write(ServiceClient serviceClient, JavaFile javaFile) { imports.add(ClassType.JACKSON_ADAPTER.getFullName()); } - imports.add(InvocationTargetException.class.getName()); imports.add(ObjectSerializer.class.getName()); ClassType.HTTP_PIPELINE.addImportsTo(imports, false); serviceClient.addImportsTo(imports, true, false, settings); + ClientCoreHttpOperationTemplate.addImportsTo(serviceClient.getProxy(), imports, settings); additionalMethods.forEach(method -> method.addImportsTo(imports)); javaFile.declareImport(imports); diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/azurevnext/AzureVNextPomTemplate.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/azurevnext/AzureVNextPomTemplate.java index 1fb1d6fabcf..3d239f9d529 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/azurevnext/AzureVNextPomTemplate.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/azurevnext/AzureVNextPomTemplate.java @@ -159,8 +159,7 @@ protected void writeSpotless(XmlBlock propertiesBlock) { */ protected void writeBuildBlock(XmlBlock projectBlock, Pom pom) { if (pom.isRequireCompilerPlugins()) { - projectBlock.block("build", - buildBlock -> buildBlock.block("plugins", pluginsBlock -> writePlugins(projectBlock))); + projectBlock.block("build", buildBlock -> buildBlock.block("plugins", this::writePlugins)); } } @@ -175,45 +174,6 @@ private void writePlugins(XmlBlock pluginsBlock) { pluginBlock.tag("groupId", "org.apache.maven.plugins"); pluginBlock.tag("artifactId", "maven-compiler-plugin"); pluginBlock.tag("version", "3.13.0"); - - pluginBlock.block("executions", executionsBlock -> executionsBlock.block("execution", executionBlock -> { - executionBlock.tag("id", "run-annotation-processing"); - executionBlock.tag("phase", "generate-sources"); - executionBlock.block("goals", goalsBlock -> goalsBlock.tag("goal", "compile")); - - executionBlock.block("configuration", configurationBlock -> { - configurationBlock.tag("source", "1.8"); - configurationBlock.tag("target", "1.8"); - configurationBlock.tag("release", "8"); - configurationBlock.tag("proc", "only"); - configurationBlock.tag("generatedSourcesDirectory", - "${project.build.directory}/generated-sources/"); - configurationBlock.block("annotationProcessorPaths", - annotationProcessorPathsBlock -> annotationProcessorPathsBlock.block("annotationProcessorPath", - pathBlock -> { - pathBlock.tag("groupId", "io.clientcore"); - pathBlock.tag("artifactId", "annotation-processor"); - pathBlock.tagWithInlineComment("version", "1.0.0-beta.4", - "{x-version-update;io.clientcore:annotation-processor;dependency}"); - })); - configurationBlock.block("annotationProcessors", - annotationProcessorsBlock -> annotationProcessorsBlock.tag("annotationProcessor", - "io.clientcore.annotation.processor.AnnotationProcessor")); - configurationBlock.block("compilerArgs", - compilerArgsBlock -> compilerArgsBlock.tag("arg", "-Xlint:-options")); - configurationBlock.block("excludes", - excludesBlock -> excludesBlock.tag("exclude", "module-info.java")); - }); - - })); - - pluginsBlock.block("dependencies", - dependenciesBlock -> dependenciesBlock.block("dependency", dependencyBlock -> { - dependencyBlock.tag("groupId", "io.clientcore"); - dependencyBlock.tag("artifactId", "annotation-processor"); - dependencyBlock.tagWithInlineComment("version", "1.0.0-beta.3", - "{x-version-update;io.clientcore:annotation-processor;dependency}"); - })); }); } } diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/clientcore/ClientCorePomTemplate.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/clientcore/ClientCorePomTemplate.java index 06c340d100f..e11bd18fab4 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/clientcore/ClientCorePomTemplate.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/clientcore/ClientCorePomTemplate.java @@ -152,8 +152,7 @@ protected void writeSpotless(XmlBlock propertiesBlock) { */ protected void writeBuildBlock(XmlBlock projectBlock, Pom pom) { if (pom.isRequireCompilerPlugins()) { - projectBlock.block("build", - buildBlock -> buildBlock.block("plugins", pluginsBlock -> writePlugins(projectBlock))); + projectBlock.block("build", buildBlock -> buildBlock.block("plugins", this::writePlugins)); } } @@ -169,47 +168,6 @@ private void writePlugins(XmlBlock pluginsBlock) { pluginBlock.tag("artifactId", "maven-compiler-plugin"); pluginBlock.tag("version", "3.13.0"); pluginBlock.block("configuration", configurationBlock -> configurationBlock.tag("release", "11")); - - // Generate annotation processor configuration if not using Rest Proxy - if (!JavaSettings.getInstance().useRestProxy()) { - pluginBlock.block("executions", - executionsBlock -> executionsBlock.block("execution", executionBlock -> { - executionBlock.tag("id", "run-annotation-processing"); - executionBlock.tag("phase", "generate-sources"); - executionBlock.block("goals", goalsBlock -> goalsBlock.tag("goal", "compile")); - - executionBlock.block("configuration", configurationBlock -> { - configurationBlock.tag("source", "1.8"); - configurationBlock.tag("target", "1.8"); - configurationBlock.tag("release", "8"); - configurationBlock.tag("proc", "only"); - configurationBlock.tag("generatedSourcesDirectory", - "${project.build.directory}/generated-sources/"); - configurationBlock.block("annotationProcessorPaths", - annotationProcessorPathsBlock -> annotationProcessorPathsBlock - .block("annotationProcessorPath", pathBlock -> { - pathBlock.tag("groupId", "io.clientcore"); - pathBlock.tag("artifactId", "annotation-processor"); - pathBlock.tag("version", "1.0.0-beta.3"); - })); - configurationBlock.block("annotationProcessors", - annotationProcessorsBlock -> annotationProcessorsBlock.tag("annotationProcessor", - "io.clientcore.annotation.processor.AnnotationProcessor")); - configurationBlock.block("compilerArgs", - compilerArgsBlock -> compilerArgsBlock.tag("arg", "-Xlint:-options")); - configurationBlock.block("excludes", - excludesBlock -> excludesBlock.tag("exclude", "module-info.java")); - }); - })); - - pluginBlock.block("dependencies", - dependenciesBlock -> dependenciesBlock.block("dependency", dependencyBlock -> { - dependencyBlock.tag("groupId", "io.clientcore"); - dependencyBlock.tag("artifactId", "annotation-processor"); - dependencyBlock.tag("version", "1.0.0-beta.3"); - })); - } - }); } } diff --git a/packages/http-client-java/generator/http-client-generator/src/test/java/com/microsoft/typespec/http/client/generator/ClientCoreHttpOperationTests.java b/packages/http-client-java/generator/http-client-generator/src/test/java/com/microsoft/typespec/http/client/generator/ClientCoreHttpOperationTests.java new file mode 100644 index 00000000000..adc7587b7d0 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator/src/test/java/com/microsoft/typespec/http/client/generator/ClientCoreHttpOperationTests.java @@ -0,0 +1,337 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.typespec.http.client.generator; + +import com.google.googlejavaformat.java.RemoveUnusedImports; +import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.CodeModel; +import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.Info; +import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.Language; +import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.Languages; +import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.Operation; +import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.OperationGroup; +import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.Parameter; +import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.Protocol; +import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.Protocols; +import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.Request; +import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.RequestParameterLocation; +import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.Schema; +import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.Schemas; +import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.StringSchema; +import com.microsoft.typespec.http.client.generator.core.extension.plugin.JavaSettings; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.AsyncSyncClient; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.Client; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClientBuilder; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.Pom; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.PrimitiveType; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.Proxy; +import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ProxyMethod; +import com.microsoft.typespec.http.client.generator.core.model.javamodel.JavaFile; +import com.microsoft.typespec.http.client.generator.core.model.javamodel.JavaPackage; +import com.microsoft.typespec.http.client.generator.core.template.Templates; +import com.microsoft.typespec.http.client.generator.model.EmitterOptions; +import io.clientcore.core.http.client.HttpClient; +import io.clientcore.core.http.models.HttpHeaderName; +import io.clientcore.core.http.models.HttpHeaders; +import io.clientcore.core.http.models.HttpMethod; +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.models.binarydata.BinaryData; +import io.clientcore.core.serialization.json.JsonReader; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.net.URL; +import java.net.URLClassLoader; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; +import javax.tools.ToolProvider; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.ValueSource; + +public class ClientCoreHttpOperationTests { + private static final String NAMESPACE = "example.client"; + private static final HttpHeaderName MARKER = HttpHeaderName.fromString("x-marker"); + + @ParameterizedTest + @ValueSource(strings = { "generic", "azurev2" }) + public void invokesFreshClientsWithoutAnnotationProcessing(String flavor, @TempDir Path directory) + throws Throwable { + verifyInNewClassLoader("verifyGeneratedClients", new Class[] { String.class, Path.class }, flavor, + directory); + } + + private static void verifyInNewClassLoader(String method, Class[] types, Object... args) throws Throwable { + // The generator initializes shared type constants for one flavor per class loader. + List classpath = new ArrayList<>(); + for (String entry : System.getProperty("java.class.path").split(File.pathSeparator)) { + classpath.add(Path.of(entry).toUri().toURL()); + } + try (URLClassLoader isolated + = new URLClassLoader(classpath.toArray(new URL[0]), ClassLoader.getPlatformClassLoader())) { + Class checks = isolated.loadClass(ClientCoreHttpOperationTests.class.getName()); + try { + checks.getMethod(method, types).invoke(null, args); + } catch (InvocationTargetException e) { + throw e.getCause(); + } + } + } + + public static void verifyGeneratedClients(String flavor, Path directory) throws Exception { + TypeSpecPlugin plugin = plugin(flavor); + CodeModel codeModel = codeModel(); + Client client = plugin.processClient(codeModel); + JavaPackage javaPackage = plugin.processTemplates(codeModel, client, JavaSettings.getInstance()); + javaPackage.addPom("pom.xml", + new Pom().setGroupId(NAMESPACE) + .setArtifactId("direct-client") + .setVersion("1.0.0") + .setServiceName("Direct Client") + .setServiceDescription("A client for exercising generated HTTP operations.") + .setRequireCompilerPlugins(true)); + String pom = javaPackage.getXmlFiles().get(javaPackage.getXmlFiles().size() - 1).getContents().toString(); + Assertions.assertTrue(pom.contains("maven-compiler-plugin"), pom); + Assertions.assertFalse(pom.contains("annotation-processor"), pom); + + AtomicInteger calls = new AtomicInteger(); + HttpClient transport = request -> { + HttpHeaders headers = new HttpHeaders().set(HttpHeaderName.CONTENT_TYPE, "application/json"); + switch (calls.incrementAndGet()) { + case 1: + Assertions.assertEquals(HttpMethod.PUT, request.getHttpMethod()); + Assertions.assertEquals("https://example.test/items/a%2Fb?search=a%20b", + request.getUri().toString()); + Assertions.assertEquals("callback", request.getHeaders().getValue(MARKER)); + return new Response<>(request, 201, headers, BinaryData.fromString("\"accepted\"")); + + case 2: + Assertions.assertEquals("https://other.example.test/lookup", request.getUri().toString()); + Assertions.assertNull(request.getHeaders().getValue(MARKER)); + return new Response<>(request, 200, headers, BinaryData.fromString("\"found\"")); + + default: + // This is a 2xx, but not the explicitly declared success status for PUT. + return new Response<>(request, 200, headers, BinaryData.fromString("{\"message\":\"rejected\"}")); + } + }; + RequestContext context = RequestContext.builder().addRequestCallback(request -> { + Assertions.assertEquals("original", request.getHeaders().getValue(MARKER)); + Assertions.assertEquals("application/json", request.getHeaders().getValue(HttpHeaderName.CONTENT_TYPE)); + Assertions.assertEquals("\"payload\"", request.getBody().toString()); + request.getHeaders().set(MARKER, "callback"); + }).build(); + + try (URLClassLoader loader = compile(directory, javaPackage.getJavaFiles())) { + ClientBuilder builderModel = client.getClientBuilders().get(0); + Class builderType = loader.loadClass(builderModel.getPackageName() + "." + builderModel.getClassName()); + Object builder = builderType.getConstructor().newInstance(); + builderType.getMethod("httpClient", HttpClient.class).invoke(builder, transport); + Object root = buildClient(builder, builderModel, false); + Object group = buildClient(builder, builderModel, true); + + var put = root.getClass() + .getMethod("putItemWithResponse", String.class, String.class, String.class, String.class, + RequestContext.class); + Response result = (Response) put.invoke(root, "a/b", "payload", "a b", "original", context); + Assertions.assertEquals(201, result.getStatusCode()); + Assertions.assertEquals("accepted", result.getValue()); + Response lookup = (Response) group.getClass() + .getMethod("lookupWithResponse", RequestContext.class) + .invoke(group, RequestContext.none()); + Assertions.assertEquals("found", lookup.getValue()); + InvocationTargetException failure = Assertions.assertThrows(InvocationTargetException.class, + () -> put.invoke(root, "a/b", "payload", "a b", null, RequestContext.none())); + HttpResponseException error = Assertions.assertInstanceOf(HttpResponseException.class, failure.getCause()); + Assertions.assertEquals(200, error.getResponse().getStatusCode()); + Assertions.assertEquals(Map.of("message", "rejected"), error.getValue()); + Assertions.assertEquals(3, calls.get()); + } + } + + @ParameterizedTest + @CsvSource({ + "azure, false, @Put", + "generic, true, @HttpRequestInformation", + "azurev2, true, @HttpRequestInformation" }) + public void retainsAnnotationsForRuntimeProxies(String flavor, boolean restProxy, String annotation) + throws Throwable { + verifyInNewClassLoader("verifyAnnotatedProxy", new Class[] { String.class, boolean.class, String.class }, + flavor, restProxy, annotation); + } + + public static void verifyAnnotatedProxy(String flavor, boolean restProxy, String annotation) { + JavaSettingsTestBase + .configureJavaSettings(Map.of("flavor", flavor, "use-rest-proxy", Boolean.toString(restProxy))); + Assertions.assertFalse(JavaSettings.getInstance().generateClientCoreHttpOperations()); + // The same switch controls service declarations and import collection, not public SDK metadata. + Proxy proxy = new Proxy.Builder().name("Api") + .clientTypeName("Example") + .baseURL("https://example.test") + .methods(List.of(new ProxyMethod.Builder().name("put") + .urlPath("/items") + .httpMethod(HttpMethod.PUT) + .requestContentType("application/json") + .returnType(PrimitiveType.VOID) + .responseExpectedStatusCodes(List.of(201)) + .parameters(List.of()) + .allParameters(List.of()) + .build())) + .build(); + JavaFile file = new JavaFile("Example.java"); + file.publicFinalClass("Example", block -> Templates.getProxyTemplate().write(proxy, block)); + String source = file.getContents().toString(); + Assertions.assertTrue(source.contains("@ServiceInterface"), source); + Assertions.assertTrue(source.contains(annotation), source); + Assertions.assertFalse(source.contains("implements Api"), source); + if (restProxy) { + Assertions.assertTrue(source.contains("RestProxy.create(Api.class, pipeline)"), source); + } + } + + private static TypeSpecPlugin plugin(String flavor) throws Exception { + try (JsonReader reader = JsonReader.fromString("{\"namespace\":\"" + NAMESPACE + "\",\"flavor\":\"" + flavor + + "\",\"stream-style-serialization\":true,\"service-versions\":[\"2025-01-01\"]}")) { + return new TypeSpecPlugin(EmitterOptions.fromJson(reader), false); + } + } + + private static Object buildClient(Object builder, ClientBuilder model, boolean methodGroup) throws Exception { + AsyncSyncClient syncClient = model.getSyncClients() + .stream() + .filter(client -> (client.getMethodGroupClient() != null) == methodGroup) + .findFirst() + .orElseThrow(); + return builder.getClass().getMethod(model.getBuilderMethodNameForSyncClient(syncClient)).invoke(builder); + } + + private static URLClassLoader compile(Path directory, List files) throws Exception { + Path classes = Files.createDirectory(directory.resolve("classes")); + List arguments = new ArrayList<>(List.of("--release", "11", "-proc:none", "-classpath", + System.getProperty("java.class.path"), "-d", classes.toString())); + for (JavaFile file : files) { + Path path = directory.resolve(file.getFilePath()); + // Compile on the classpath to share the fake transport with the test. + if (path.getFileName().toString().equals("module-info.java")) { + continue; + } + Files.createDirectories(path.getParent()); + String source = file.getContents().toString(); + Assertions.assertFalse(source.contains("io.clientcore.core.http.annotations."), source); + Assertions.assertFalse(source.contains("@ServiceInterface"), source); + Assertions.assertFalse(source.contains("Class.forName("), source); + Files.writeString(path, RemoveUnusedImports.removeUnusedImports(source), StandardCharsets.UTF_8); + arguments.add(path.toString()); + } + ByteArrayOutputStream output = new ByteArrayOutputStream(); + Assertions.assertEquals(0, + ToolProvider.getSystemJavaCompiler().run(null, output, output, arguments.toArray(new String[0])), + output.toString(StandardCharsets.UTF_8)); + return new URLClassLoader(new URL[] { classes.toUri().toURL() }, + ClientCoreHttpOperationTests.class.getClassLoader()); + } + + private static CodeModel codeModel() { + CodeModel model = new CodeModel(); + model.setLanguage(languages("DirectClient", "DirectClient")); + Info info = new Info(); + info.setTitle("Direct Client"); + info.setDescription("A client for exercising generated HTTP operations."); + model.setInfo(info); + model.setSchemas(new Schemas()); + Operation put = operation("putItem", "PUT", "/items/{itemId}", "201", + List.of(parameter("itemId", "itemId", RequestParameterLocation.PATH, true), + parameter("search", "search", RequestParameterLocation.QUERY, false), + parameter("marker", "x-marker", RequestParameterLocation.HEADER, false), + parameter("payload", "payload", RequestParameterLocation.BODY, true))); + Operation lookup = operation("lookup", "GET", "https://other.example.test/lookup", "200", List.of()); + model.setOperationGroups(List.of(group(model, "", put), group(model, "items", lookup))); + return model; + } + + private static OperationGroup group(CodeModel model, String name, Operation operation) { + OperationGroup group = new OperationGroup(); + group.set$key(name); + group.setLanguage(languages(name, name)); + group.setCodeModel(model); + group.setOperations(new ArrayList<>(List.of(operation))); + operation.setOperationGroup(group); + return group; + } + + private static Operation operation(String name, String method, String path, String status, + List parameters) { + Operation operation = new Operation(); + operation.setLanguage(languages(name, name)); + operation.setOperationId(name); + operation.setDescription(name + "."); + Request request = new Request(); + request.setLanguage(operation.getLanguage()); + request.setParameters(new ArrayList<>(parameters)); + request.setSignatureParameters(new ArrayList<>(parameters)); + Protocol requestHttp = new Protocol(); + requestHttp.setMethod(method); + requestHttp.setUri("https://example.test"); + requestHttp.setPath(path); + requestHttp.setMediaTypes(List.of("application/json")); + request.setProtocol(http(requestHttp)); + operation.setRequests(new ArrayList<>(List.of(request))); + + var response = new com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.Response(); + response.setSchema(stringSchema()); + Protocol responseHttp = new Protocol(); + responseHttp.setStatusCodes(List.of(status)); + responseHttp.setMediaTypes(List.of("application/json")); + response.setProtocol(http(responseHttp)); + operation.setResponses(List.of(response)); + return operation; + } + + private static Parameter parameter(String name, String wireName, RequestParameterLocation location, + boolean required) { + Parameter parameter = new Parameter(); + parameter.setLanguage(languages(name, wireName)); + parameter.setDescription(name + "."); + parameter.setRequired(required); + parameter.setImplementation(Parameter.ImplementationLocation.METHOD); + parameter.setSchema(stringSchema()); + Protocol http = new Protocol(); + http.setIn(location); + parameter.setProtocol(http(http)); + return parameter; + } + + private static Protocols http(Protocol http) { + Protocols protocols = new Protocols(); + protocols.setHttp(http); + return protocols; + } + + private static StringSchema stringSchema() { + StringSchema string = new StringSchema(); + string.setType(Schema.AllSchemaTypes.STRING); + return string; + } + + private static Languages languages(String name, String wireName) { + Language language = new Language(); + language.setName(name); + language.setNamespace(NAMESPACE); + language.setSerializedName(wireName); + language.setDescription(name + "."); + language.setCrossLanguageDefinitionId(NAMESPACE + "." + name); + Languages languages = new Languages(); + languages.setDefault(language); + languages.setJava(language); + return languages; + } +}