Skip to content

Commit

Permalink
Remove rest client from generic codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
jianghaolu committed Jun 16, 2016
1 parent 1773d49 commit 05105b4
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -25,7 +27,7 @@ protected AzureServiceClient(String baseUrl) {
* @param restClient the REST client
*/
protected AzureServiceClient(RestClient restClient) {
super(restClient);
this.restClient = restClient;
}

/**
Expand Down
63 changes: 47 additions & 16 deletions client-runtime/src/main/java/com/microsoft/rest/ServiceClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,72 @@

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.
*
* @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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
28 changes: 14 additions & 14 deletions client-runtime/src/test/java/com/microsoft/rest/UserAgentTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");
Expand All @@ -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());
}
Expand Down

0 comments on commit 05105b4

Please sign in to comment.