Skip to content

Commit

Permalink
Move .build() from collection calls to initialize()
Browse files Browse the repository at this point in the history
  • Loading branch information
jianghaolu committed Apr 25, 2016
1 parent b7dc878 commit dfba322
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 47 deletions.
21 changes: 10 additions & 11 deletions client-runtime/src/main/java/com/microsoft/rest/ServiceClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ public abstract class ServiceClient {
*/
protected Retrofit.Builder retrofitBuilder;

/**
* The OkHttp client.
*/
protected OkHttpClient httpClient;

/**
* The rest adapter.
*/
protected Retrofit retrofit;

/**
* The adapter for {@link com.fasterxml.jackson.databind.ObjectMapper} for serialization
* and deserialization operations.
Expand Down Expand Up @@ -74,17 +84,6 @@ public List<Interceptor> getClientInterceptors() {
return this.clientBuilder.interceptors();
}

/**
* Sets the logging level for OkHttp client.
*
* @param logLevel the logging level enum
*/
public void setLogLevel(Level logLevel) {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(logLevel);
this.getClientInterceptors().add(loggingInterceptor);
}

/**
* Gets the adapter for {@link com.fasterxml.jackson.databind.ObjectMapper} for serialization
* and deserialization operations..
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
import com.microsoft.rest.credentials.BasicAuthenticationCredentials;
import com.microsoft.rest.credentials.TokenCredentials;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Protocol;
import okhttp3.Response;
import retrofit2.Retrofit;

import org.junit.Assert;
import org.junit.Test;

Expand All @@ -20,29 +23,30 @@
public class CredentialsTests {
@Test
public void basicCredentialsTest() throws Exception {
ServiceClient serviceClient = new ServiceClient() { };
BasicAuthenticationCredentials credentials = new BasicAuthenticationCredentials("user", "pass");
credentials.applyCredentialsFilter(serviceClient.clientBuilder);
serviceClient.getClientInterceptors().add(new Interceptor() {
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
Retrofit.Builder retrofitBuilder = new Retrofit.Builder();
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();
return new Response.Builder()
.request(chain.request())
.code(200)
.protocol(Protocol.HTTP_1_1)
.build();
}
});
ServiceClient serviceClient = new ServiceClient(clientBuilder, retrofitBuilder) { };
BasicAuthenticationCredentials credentials = new BasicAuthenticationCredentials("user", "pass");
credentials.applyCredentialsFilter(serviceClient.clientBuilder);
}

@Test
public void tokenCredentialsTest() throws Exception {
ServiceClient serviceClient = new ServiceClient() { };
TokenCredentials credentials = new TokenCredentials(null, "this_is_a_token");
credentials.applyCredentialsFilter(serviceClient.clientBuilder);
serviceClient.getClientInterceptors().add(new Interceptor() {
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
Retrofit.Builder retrofitBuilder = new Retrofit.Builder();
clientBuilder.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
String header = chain.request().header("Authorization");
Expand All @@ -54,5 +58,8 @@ public Response intercept(Chain chain) throws IOException {
.build();
}
});
ServiceClient serviceClient = new ServiceClient(clientBuilder, retrofitBuilder) { };
TokenCredentials credentials = new TokenCredentials(null, "this_is_a_token");
credentials.applyCredentialsFilter(serviceClient.clientBuilder);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
import com.microsoft.rest.retry.RetryHandler;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Protocol;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;

import org.junit.Assert;
import org.junit.Test;

Expand All @@ -21,9 +24,10 @@
public class RetryHandlerTests {
@Test
public void exponentialRetryEndOn501() throws Exception {
ServiceClient serviceClient = new ServiceClient() { };
serviceClient.getClientInterceptors().add(new RetryHandler());
serviceClient.getClientInterceptors().add(new Interceptor() {
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
Retrofit.Builder retrofitBuilder = new Retrofit.Builder();
clientBuilder.addInterceptor(new RetryHandler());
clientBuilder.addInterceptor(new Interceptor() {
// Send 408, 500, 502, all retried, with a 501 ending
private int[] codes = new int[]{408, 500, 502, 501};
private int count = 0;
Expand All @@ -37,18 +41,21 @@ public Response intercept(Chain chain) throws IOException {
.build();
}
});
ServiceClient serviceClient = new ServiceClient(clientBuilder, retrofitBuilder) { };
Response response = serviceClient.clientBuilder.build().newCall(
new Request.Builder().url("http://localhost").get().build()).execute();
Assert.assertEquals(501, response.code());
}

@Test
public void exponentialRetryMax() throws Exception {
ServiceClient serviceClient = new ServiceClient() { };
serviceClient.getClientInterceptors().add(new RetryHandler());
serviceClient.getClientInterceptors().add(new Interceptor() {
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
Retrofit.Builder retrofitBuilder = new Retrofit.Builder();
clientBuilder.addInterceptor(new RetryHandler());
clientBuilder.addInterceptor(new Interceptor() {
// Send 500 until max retry is hit
private int count = 0;

@Override
public Response intercept(Chain chain) throws IOException {
Assert.assertTrue(count++ < 5);
Expand All @@ -59,6 +66,7 @@ public Response intercept(Chain chain) throws IOException {
.build();
}
});
ServiceClient serviceClient = new ServiceClient(clientBuilder, retrofitBuilder) { };
Response response = serviceClient.clientBuilder.build().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 @@ -8,7 +8,10 @@
package com.microsoft.rest;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Response;
import retrofit2.Retrofit;

import org.junit.Assert;
import org.junit.Test;

Expand All @@ -17,17 +20,19 @@
public class ServiceClientTests {
@Test
public void filterTests() throws Exception {
ServiceClient serviceClient = new ServiceClient() { };
serviceClient.getClientInterceptors().add(0, new FirstFilter());
serviceClient.getClientInterceptors().add(1, new SecondFilter());
serviceClient.getClientInterceptors().add(new Interceptor() {
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
Retrofit.Builder retrofitBuilder = new Retrofit.Builder();
clientBuilder.interceptors().add(0, new FirstFilter());
clientBuilder.interceptors().add(1, new SecondFilter());
clientBuilder.interceptors().add(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Assert.assertEquals("1", chain.request().header("filter1"));
Assert.assertEquals("2", chain.request().header("filter2"));
return chain.proceed(chain.request());
}
});
ServiceClient serviceClient = new ServiceClient(clientBuilder, retrofitBuilder) { };
}

public class FirstFilter implements Interceptor {
Expand Down
33 changes: 20 additions & 13 deletions client-runtime/src/test/java/com/microsoft/rest/UserAgentTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
package com.microsoft.rest;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Protocol;
import okhttp3.Response;
import retrofit2.Retrofit;

import org.junit.Assert;
import org.junit.Test;

Expand All @@ -18,36 +21,40 @@
public class UserAgentTests {
@Test
public void defaultUserAgentTests() throws Exception {
ServiceClient serviceClient = new ServiceClient() { };
serviceClient.getClientInterceptors().add(new Interceptor() {
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
Retrofit.Builder retrofitBuilder = new Retrofit.Builder();
clientBuilder.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
String header = chain.request().header("User-Agent");
Assert.assertEquals("AutoRest-Java", header);
return new Response.Builder()
.request(chain.request())
.code(200)
.protocol(Protocol.HTTP_1_1)
.build();
.request(chain.request())
.code(200)
.protocol(Protocol.HTTP_1_1)
.build();
}
});
ServiceClient serviceClient = new ServiceClient(clientBuilder, retrofitBuilder) { };
}

@Test
public void customUserAgentTests() throws Exception {
ServiceClient serviceClient = new ServiceClient() { };
serviceClient.getClientInterceptors().add(new UserAgentInterceptor("Awesome"));
serviceClient.getClientInterceptors().add(new Interceptor() {
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
Retrofit.Builder retrofitBuilder = new Retrofit.Builder();
clientBuilder.addInterceptor(new UserAgentInterceptor("Awesome"));
clientBuilder.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
String header = chain.request().header("User-Agent");
Assert.assertEquals("Awesome", header);
return new Response.Builder()
.request(chain.request())
.code(200)
.protocol(Protocol.HTTP_1_1)
.build();
.request(chain.request())
.code(200)
.protocol(Protocol.HTTP_1_1)
.build();
}
});
ServiceClient serviceClient = new ServiceClient(clientBuilder, retrofitBuilder) { };
}
}

0 comments on commit dfba322

Please sign in to comment.