From 05105b477b12dd6e00aecea0116e6e8b5de1cf45 Mon Sep 17 00:00:00 2001 From: Jianghao Lu Date: Thu, 16 Jun 2016 13:39:32 -0700 Subject: [PATCH] Remove rest client from generic codegen --- .../microsoft/azure/AzureServiceClient.java | 6 +- .../com/microsoft/rest/ServiceClient.java | 63 ++++++++++++++----- .../com/microsoft/rest/CredentialsTests.java | 48 +++++++------- .../com/microsoft/rest/RetryHandlerTests.java | 12 ++-- .../microsoft/rest/ServiceClientTests.java | 6 +- .../com/microsoft/rest/UserAgentTests.java | 28 ++++----- 6 files changed, 95 insertions(+), 68 deletions(-) diff --git a/azure-client-runtime/src/main/java/com/microsoft/azure/AzureServiceClient.java b/azure-client-runtime/src/main/java/com/microsoft/azure/AzureServiceClient.java index 5edc6a662e62c..6c68c9fcd071a 100644 --- a/azure-client-runtime/src/main/java/com/microsoft/azure/AzureServiceClient.java +++ b/azure-client-runtime/src/main/java/com/microsoft/azure/AzureServiceClient.java @@ -13,7 +13,9 @@ /** * ServiceClient is the abstraction for accessing REST operations and their payload data types. */ -public abstract class AzureServiceClient extends ServiceClient { +public abstract class AzureServiceClient { + RestClient restClient; + protected AzureServiceClient(String baseUrl) { this(new RestClient.Builder().withBaseUrl(baseUrl) .withInterceptor(new RequestIdHeaderInterceptor()).build()); @@ -25,7 +27,7 @@ protected AzureServiceClient(String baseUrl) { * @param restClient the REST client */ protected AzureServiceClient(RestClient restClient) { - super(restClient); + this.restClient = restClient; } /** diff --git a/client-runtime/src/main/java/com/microsoft/rest/ServiceClient.java b/client-runtime/src/main/java/com/microsoft/rest/ServiceClient.java index e6d2f4a8d595d..8896f1968f90f 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/ServiceClient.java +++ b/client-runtime/src/main/java/com/microsoft/rest/ServiceClient.java @@ -7,14 +7,25 @@ package com.microsoft.rest; +import com.microsoft.rest.retry.RetryHandler; +import com.microsoft.rest.serializer.JacksonMapperAdapter; + +import java.net.CookieManager; +import java.net.CookiePolicy; + +import okhttp3.JavaNetCookieJar; +import okhttp3.OkHttpClient; +import retrofit2.Retrofit; + /** * ServiceClient is the abstraction for accessing REST operations and their payload data types. */ public abstract class ServiceClient { - /** - * The builder for building the OkHttp client. - */ - private RestClient restClient; + protected OkHttpClient httpClient; + + protected Retrofit retrofit; + + protected JacksonMapperAdapter mapperAdapter; /** * Initializes a new instance of the ServiceClient class. @@ -22,26 +33,46 @@ public abstract class ServiceClient { * @param baseUrl the service endpoint */ protected ServiceClient(String baseUrl) { - this(new RestClient.Builder().withBaseUrl(baseUrl).build()); + this(baseUrl, new OkHttpClient.Builder(), new Retrofit.Builder()); } /** * Initializes a new instance of the ServiceClient class. * - * @param restClient the builder to build up an REST client */ - protected ServiceClient(RestClient restClient) { - if (restClient == null) { - throw new IllegalArgumentException("restClient == null"); + protected ServiceClient(String baseUrl, OkHttpClient.Builder clientBuilder, Retrofit.Builder restBuilder) { + if (clientBuilder == null) { + throw new IllegalArgumentException("clientBuilder == null"); } - this.restClient = restClient; + if (restBuilder == null) { + throw new IllegalArgumentException("restBuilder == null"); + } + this.mapperAdapter = new JacksonMapperAdapter(); + CookieManager cookieManager = new CookieManager(); + cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); + this.httpClient = clientBuilder + .cookieJar(new JavaNetCookieJar(cookieManager)) + .addInterceptor(new UserAgentInterceptor()) + .addInterceptor(new BaseUrlHandler()) + .addInterceptor(new CustomHeadersInterceptor()) + .addInterceptor(new RetryHandler()) + .build(); + this.retrofit = restBuilder + .baseUrl(baseUrl) + .client(httpClient) + .addConverterFactory(mapperAdapter.getConverterFactory()) + .build(); } - /** - * Get the list of interceptors the OkHttp client will execute. - * @return the list of interceptors - */ - public RestClient restClient() { - return this.restClient; + public Retrofit retrofit() { + return this.retrofit; + } + + public OkHttpClient httpClient() { + return this.httpClient; + } + + public JacksonMapperAdapter mapperAdapter() { + return this.mapperAdapter; } } diff --git a/client-runtime/src/test/java/com/microsoft/rest/CredentialsTests.java b/client-runtime/src/test/java/com/microsoft/rest/CredentialsTests.java index fbd3e15f52c9f..31b7daf4b7e25 100644 --- a/client-runtime/src/test/java/com/microsoft/rest/CredentialsTests.java +++ b/client-runtime/src/test/java/com/microsoft/rest/CredentialsTests.java @@ -27,33 +27,33 @@ public class CredentialsTests { @Test public void basicCredentialsTest() throws Exception { BasicAuthenticationCredentials credentials = new BasicAuthenticationCredentials("user", "pass"); - RestClient restClient = new RestClient.Builder() - .withBaseUrl("http://localhost") - .withCredentials(credentials) - .withInterceptor(new Interceptor() { - @Override - public Response intercept(Chain chain) throws IOException { - String header = chain.request().header("Authorization"); - Assert.assertEquals("Basic dXNlcjpwYXNz", header); - return new Response.Builder() - .request(chain.request()) - .code(200) - .protocol(Protocol.HTTP_1_1) - .build(); - } - }).build(); - ServiceClient serviceClient = new ServiceClient(restClient) { }; - Response response = serviceClient.restClient().httpClient().newCall(new Request.Builder().url("http://localhost").build()).execute(); + OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); + credentials.applyCredentialsFilter(clientBuilder); + clientBuilder.addInterceptor( + new Interceptor() { + @Override + public Response intercept(Chain chain) throws IOException { + String header = chain.request().header("Authorization"); + Assert.assertEquals("Basic dXNlcjpwYXNz", header); + return new Response.Builder() + .request(chain.request()) + .code(200) + .protocol(Protocol.HTTP_1_1) + .build(); + } + }); + ServiceClient serviceClient = new ServiceClient("http://localhost", clientBuilder, new Retrofit.Builder()) { }; + Response response = serviceClient.httpClient().newCall(new Request.Builder().url("http://localhost").build()).execute(); Assert.assertEquals(200, response.code()); } @Test public void tokenCredentialsTest() throws Exception { TokenCredentials credentials = new TokenCredentials(null, "this_is_a_token"); - RestClient restClient = new RestClient.Builder() - .withBaseUrl("http://localhost") - .withCredentials(credentials) - .withInterceptor(new Interceptor() { + OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); + credentials.applyCredentialsFilter(clientBuilder); + clientBuilder.addInterceptor( + new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { String header = chain.request().header("Authorization"); @@ -64,9 +64,9 @@ public Response intercept(Chain chain) throws IOException { .protocol(Protocol.HTTP_1_1) .build(); } - }).build(); - ServiceClient serviceClient = new ServiceClient(restClient) { }; - Response response = serviceClient.restClient().httpClient().newCall(new Request.Builder().url("http://localhost").build()).execute(); + }); + ServiceClient serviceClient = new ServiceClient("http://localhost", clientBuilder, new Retrofit.Builder()) { }; + Response response = serviceClient.httpClient().newCall(new Request.Builder().url("http://localhost").build()).execute(); Assert.assertEquals(200, response.code()); } } diff --git a/client-runtime/src/test/java/com/microsoft/rest/RetryHandlerTests.java b/client-runtime/src/test/java/com/microsoft/rest/RetryHandlerTests.java index bceb2835068af..a086eb39f0d3f 100644 --- a/client-runtime/src/test/java/com/microsoft/rest/RetryHandlerTests.java +++ b/client-runtime/src/test/java/com/microsoft/rest/RetryHandlerTests.java @@ -42,10 +42,8 @@ public Response intercept(Chain chain) throws IOException { .build(); } }); - RestClient restClient = new RestClient.Builder( clientBuilder, retrofitBuilder) - .withBaseUrl("http://localhost").build(); - ServiceClient serviceClient = new ServiceClient(restClient) { }; - Response response = serviceClient.restClient().httpClient().newCall( + ServiceClient serviceClient = new ServiceClient("http://localhost", clientBuilder, retrofitBuilder) { }; + Response response = serviceClient.httpClient().newCall( new Request.Builder().url("http://localhost").get().build()).execute(); Assert.assertEquals(501, response.code()); } @@ -69,10 +67,8 @@ public Response intercept(Chain chain) throws IOException { .build(); } }); - RestClient restClient = new RestClient.Builder(clientBuilder, retrofitBuilder) - .withBaseUrl("http://localhost").build(); - ServiceClient serviceClient = new ServiceClient(restClient) { }; - Response response = serviceClient.restClient().httpClient().newCall( + ServiceClient serviceClient = new ServiceClient("http://localhost", clientBuilder, retrofitBuilder) { }; + Response response = serviceClient.httpClient().newCall( new Request.Builder().url("http://localhost").get().build()).execute(); Assert.assertEquals(500, response.code()); } diff --git a/client-runtime/src/test/java/com/microsoft/rest/ServiceClientTests.java b/client-runtime/src/test/java/com/microsoft/rest/ServiceClientTests.java index c4309e729bcf8..a5815aa0ae7dc 100644 --- a/client-runtime/src/test/java/com/microsoft/rest/ServiceClientTests.java +++ b/client-runtime/src/test/java/com/microsoft/rest/ServiceClientTests.java @@ -39,10 +39,8 @@ public Response intercept(Chain chain) throws IOException { .build(); } }); - RestClient restClient = new RestClient.Builder(clientBuilder, retrofitBuilder) - .withBaseUrl("http://localhost").build(); - ServiceClient serviceClient = new ServiceClient(restClient) { }; - Response response = serviceClient.restClient().httpClient().newCall(new Request.Builder().url("http://localhost").build()).execute(); + ServiceClient serviceClient = new ServiceClient("http://localhost", clientBuilder, retrofitBuilder) { }; + Response response = serviceClient.httpClient().newCall(new Request.Builder().url("http://localhost").build()).execute(); Assert.assertEquals(200, response.code()); } diff --git a/client-runtime/src/test/java/com/microsoft/rest/UserAgentTests.java b/client-runtime/src/test/java/com/microsoft/rest/UserAgentTests.java index 245ceaeae8dd6..f6a3f9491ffa3 100644 --- a/client-runtime/src/test/java/com/microsoft/rest/UserAgentTests.java +++ b/client-runtime/src/test/java/com/microsoft/rest/UserAgentTests.java @@ -13,16 +13,18 @@ import java.io.IOException; import okhttp3.Interceptor; +import okhttp3.OkHttpClient; import okhttp3.Protocol; import okhttp3.Request; import okhttp3.Response; +import retrofit2.Retrofit; public class UserAgentTests { @Test public void defaultUserAgentTests() throws Exception { - RestClient restClient = new RestClient.Builder() - .withBaseUrl("http://localhost") - .withInterceptor(new Interceptor() { + OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder() + .addInterceptor(new UserAgentInterceptor()) + .addInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { String header = chain.request().header("User-Agent"); @@ -33,20 +35,18 @@ public Response intercept(Chain chain) throws IOException { .protocol(Protocol.HTTP_1_1) .build(); } - }).build(); - ServiceClient serviceClient = new ServiceClient(restClient) { }; - Response response = serviceClient.restClient().httpClient() + }); + ServiceClient serviceClient = new ServiceClient("http://localhost", clientBuilder, new Retrofit.Builder()) { }; + Response response = serviceClient.httpClient() .newCall(new Request.Builder().get().url("http://localhost").build()).execute(); Assert.assertEquals(200, response.code()); } @Test public void customUserAgentTests() throws Exception { - - RestClient restClient = new RestClient.Builder() - .withBaseUrl("http://localhost") - .withUserAgent("Awesome") - .withInterceptor(new Interceptor() { + OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder() + .addInterceptor(new UserAgentInterceptor()) + .addInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { String header = chain.request().header("User-Agent"); @@ -57,9 +57,9 @@ public Response intercept(Chain chain) throws IOException { .protocol(Protocol.HTTP_1_1) .build(); } - }).build(); - ServiceClient serviceClient = new ServiceClient(restClient) { }; - Response response = serviceClient.restClient().httpClient() + }); + ServiceClient serviceClient = new ServiceClient("http://localhost", clientBuilder, new Retrofit.Builder()) { }; + Response response = serviceClient.httpClient() .newCall(new Request.Builder().get().url("http://localhost").build()).execute(); Assert.assertEquals(200, response.code()); }