Skip to content

Commit

Permalink
Add stage for base url
Browse files Browse the repository at this point in the history
  • Loading branch information
jianghaolu committed Jun 16, 2016
1 parent 4a7b25e commit 1773d49
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public Builder() {
*/
public Builder(OkHttpClient.Builder httpClientBuilder, Retrofit.Builder retrofitBuilder) {
super(httpClientBuilder, retrofitBuilder);
buildable = new Buildable();
}

/**
Expand All @@ -61,34 +62,37 @@ public Builder(OkHttpClient.Builder httpClientBuilder, Retrofit.Builder retrofit
* @param environment the environment the application is running in
* @return the builder itself for chaining
*/
public Builder withDefaultBaseUrl(AzureEnvironment environment) {
public RestClient.Builder.Buildable withDefaultBaseUrl(AzureEnvironment environment) {
withBaseUrl(environment.getBaseUrl());
return this;
return buildable;
}

/**
* Build an AzureRestClient with all the current configurations.
*
* @return an {@link AzureRestClient}.
*/
public AzureRestClient build() {
AzureJacksonMapperAdapter mapperAdapter = new AzureJacksonMapperAdapter();
OkHttpClient httpClient = httpClientBuilder
.addInterceptor(baseUrlHandler)
.addInterceptor(customHeadersInterceptor)
.addInterceptor(new RetryHandler())
.build();
return new AzureRestClient(httpClient,
retrofitBuilder
.baseUrl(baseUrl)
.client(httpClient)
.addConverterFactory(mapperAdapter.getConverterFactory())
.build(),
credentials,
customHeadersInterceptor,
userAgentInterceptor,
baseUrlHandler,
mapperAdapter);
public class Buildable extends RestClient.Builder.Buildable {
/**
* Build an AzureRestClient with all the current configurations.
*
* @return an {@link AzureRestClient}.
*/
@Override
public AzureRestClient build() {
AzureJacksonMapperAdapter mapperAdapter = new AzureJacksonMapperAdapter();
OkHttpClient httpClient = httpClientBuilder
.addInterceptor(baseUrlHandler)
.addInterceptor(customHeadersInterceptor)
.addInterceptor(new RetryHandler())
.build();
return new AzureRestClient(httpClient,
retrofitBuilder
.baseUrl(baseUrl)
.client(httpClient)
.addConverterFactory(mapperAdapter.getConverterFactory())
.build(),
credentials,
customHeadersInterceptor,
userAgentInterceptor,
baseUrlHandler,
mapperAdapter);
}
}
}
}
143 changes: 76 additions & 67 deletions client-runtime/src/main/java/com/microsoft/rest/RestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ public static class Builder {
protected BaseUrlHandler baseUrlHandler;
/** The interceptor to set 'User-Agent' header. */
protected UserAgentInterceptor userAgentInterceptor;
/** The inner Builder instance. */
protected Buildable buildable;

/**
* Creates an instance of the builder with a base URL to the service.
Expand Down Expand Up @@ -161,6 +163,7 @@ public Builder(OkHttpClient.Builder httpClientBuilder, Retrofit.Builder retrofit
.cookieJar(new JavaNetCookieJar(cookieManager))
.addInterceptor(userAgentInterceptor);
this.retrofitBuilder = retrofitBuilder;
this.buildable = new Buildable();
}

/**
Expand All @@ -169,9 +172,9 @@ public Builder(OkHttpClient.Builder httpClientBuilder, Retrofit.Builder retrofit
* @param baseUrl the base URL to use.
* @return the builder itself for chaining.
*/
public Builder withBaseUrl(String baseUrl) {
public Buildable withBaseUrl(String baseUrl) {
this.baseUrl = baseUrl;
return this;
return buildable;
}

/**
Expand All @@ -180,7 +183,7 @@ public Builder withBaseUrl(String baseUrl) {
* @param serviceClientClass the service client class containing a default base URL.
* @return the builder itself for chaining.
*/
public Builder withDefaultBaseUrl(Class<?> serviceClientClass) {
public Buildable withDefaultBaseUrl(Class<?> serviceClientClass) {
try {
Field field = serviceClientClass.getDeclaredField("DEFAULT_BASE_URL");
field.setAccessible(true);
Expand All @@ -190,79 +193,85 @@ public Builder withDefaultBaseUrl(Class<?> serviceClientClass) {
} catch (IllegalAccessException e) {
throw new UnsupportedOperationException("Cannot read static field DEFAULT_BASE_URL", e);
}
return this;
return buildable;
}

/**
* Sets the user agent header.
*
* @param userAgent the user agent header.
* @return the builder itself for chaining.
* The inner class from which a Rest Client can be built.
*/
public Builder withUserAgent(String userAgent) {
this.userAgentInterceptor.setUserAgent(userAgent);
return this;
}
public class Buildable {
/**
* Sets the user agent header.
*
* @param userAgent the user agent header.
* @return the builder itself for chaining.
*/
public Buildable withUserAgent(String userAgent) {
userAgentInterceptor.setUserAgent(userAgent);
return this;
}

/**
* Sets the credentials.
*
* @param credentials the credentials object.
* @return the builder itself for chaining.
*/
public Builder withCredentials(ServiceClientCredentials credentials) {
this.credentials = credentials;
if (credentials != null) {
credentials.applyCredentialsFilter(httpClientBuilder);
/**
* Sets the credentials.
*
* @param credentials the credentials object.
* @return the builder itself for chaining.
*/
public Buildable withCredentials(ServiceClientCredentials credentials) {
Builder.this.credentials = credentials;
if (credentials != null) {
credentials.applyCredentialsFilter(httpClientBuilder);
}
return this;
}
return this;
}

/**
* Sets the log level.
*
* @param logLevel the {@link okhttp3.logging.HttpLoggingInterceptor.Level} enum.
* @return the builder itself for chaining.
*/
public Builder withLogLevel(HttpLoggingInterceptor.Level logLevel) {
this.httpClientBuilder.addInterceptor(new HttpLoggingInterceptor().setLevel(logLevel));
return this;
}
/**
* Sets the log level.
*
* @param logLevel the {@link okhttp3.logging.HttpLoggingInterceptor.Level} enum.
* @return the builder itself for chaining.
*/
public Buildable withLogLevel(HttpLoggingInterceptor.Level logLevel) {
httpClientBuilder.addInterceptor(new HttpLoggingInterceptor().setLevel(logLevel));
return this;
}

/**
* Add an interceptor the Http client pipeline.
*
* @param interceptor the interceptor to add.
* @return the builder itself for chaining.
*/
public Builder withInterceptor(Interceptor interceptor) {
this.httpClientBuilder.addInterceptor(interceptor);
return this;
}
/**
* Add an interceptor the Http client pipeline.
*
* @param interceptor the interceptor to add.
* @return the builder itself for chaining.
*/
public Buildable withInterceptor(Interceptor interceptor) {
httpClientBuilder.addInterceptor(interceptor);
return this;
}

/**
* Build a RestClient with all the current configurations.
*
* @return a {@link RestClient}.
*/
public RestClient build() {
JacksonMapperAdapter mapperAdapter = new JacksonMapperAdapter();
OkHttpClient httpClient = httpClientBuilder
.addInterceptor(baseUrlHandler)
.addInterceptor(customHeadersInterceptor)
.addInterceptor(new RetryHandler())
.build();
return new RestClient(httpClient,
retrofitBuilder
.baseUrl(baseUrl)
.client(httpClient)
.addConverterFactory(mapperAdapter.getConverterFactory())
.build(),
credentials,
customHeadersInterceptor,
userAgentInterceptor,
baseUrlHandler,
mapperAdapter);
}

/**
* Build a RestClient with all the current configurations.
*
* @return a {@link RestClient}.
*/
public RestClient build() {
JacksonMapperAdapter mapperAdapter = new JacksonMapperAdapter();
OkHttpClient httpClient = httpClientBuilder
.addInterceptor(baseUrlHandler)
.addInterceptor(customHeadersInterceptor)
.addInterceptor(new RetryHandler())
.build();
return new RestClient(httpClient,
retrofitBuilder
.baseUrl(baseUrl)
.client(httpClient)
.addConverterFactory(mapperAdapter.getConverterFactory())
.build(),
credentials,
customHeadersInterceptor,
userAgentInterceptor,
baseUrlHandler,
mapperAdapter);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class CredentialsTests {
@Test
public void basicCredentialsTest() throws Exception {
BasicAuthenticationCredentials credentials = new BasicAuthenticationCredentials("user", "pass");
RestClient.Builder restBuilder = new RestClient.Builder()
RestClient restClient = new RestClient.Builder()
.withBaseUrl("http://localhost")
.withCredentials(credentials)
.withInterceptor(new Interceptor() {
Expand All @@ -41,16 +41,16 @@ public Response intercept(Chain chain) throws IOException {
.protocol(Protocol.HTTP_1_1)
.build();
}
});
ServiceClient serviceClient = new ServiceClient(restBuilder.build()) { };
}).build();
ServiceClient serviceClient = new ServiceClient(restClient) { };
Response response = serviceClient.restClient().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.Builder restBuilder = new RestClient.Builder()
RestClient restClient = new RestClient.Builder()
.withBaseUrl("http://localhost")
.withCredentials(credentials)
.withInterceptor(new Interceptor() {
Expand All @@ -64,8 +64,8 @@ public Response intercept(Chain chain) throws IOException {
.protocol(Protocol.HTTP_1_1)
.build();
}
});
ServiceClient serviceClient = new ServiceClient(restBuilder.build()) { };
}).build();
ServiceClient serviceClient = new ServiceClient(restClient) { };
Response response = serviceClient.restClient().httpClient().newCall(new Request.Builder().url("http://localhost").build()).execute();
Assert.assertEquals(200, response.code());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ public Response intercept(Chain chain) throws IOException {
.build();
}
});
RestClient.Builder restBuilder = new RestClient.Builder( clientBuilder, retrofitBuilder)
.withBaseUrl("http://localhost");
ServiceClient serviceClient = new ServiceClient(restBuilder.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").get().build()).execute();
Assert.assertEquals(501, response.code());
Expand All @@ -69,9 +69,9 @@ public Response intercept(Chain chain) throws IOException {
.build();
}
});
RestClient.Builder restBuilder = new RestClient.Builder(clientBuilder, retrofitBuilder)
.withBaseUrl("http://localhost");
ServiceClient serviceClient = new ServiceClient(restBuilder.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").get().build()).execute();
Assert.assertEquals(500, response.code());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public Response intercept(Chain chain) throws IOException {
.build();
}
});
RestClient.Builder restBuilder = new RestClient.Builder(clientBuilder, retrofitBuilder)
.withBaseUrl("http://localhost");
ServiceClient serviceClient = new ServiceClient(restBuilder.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();
Assert.assertEquals(200, response.code());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

package com.microsoft.rest;

import com.microsoft.rest.serializer.JacksonMapperAdapter;

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

Expand All @@ -22,7 +20,7 @@
public class UserAgentTests {
@Test
public void defaultUserAgentTests() throws Exception {
RestClient.Builder restBuilder = new RestClient.Builder()
RestClient restClient = new RestClient.Builder()
.withBaseUrl("http://localhost")
.withInterceptor(new Interceptor() {
@Override
Expand All @@ -35,8 +33,8 @@ public Response intercept(Chain chain) throws IOException {
.protocol(Protocol.HTTP_1_1)
.build();
}
});
ServiceClient serviceClient = new ServiceClient(restBuilder.build()) { };
}).build();
ServiceClient serviceClient = new ServiceClient(restClient) { };
Response response = serviceClient.restClient().httpClient()
.newCall(new Request.Builder().get().url("http://localhost").build()).execute();
Assert.assertEquals(200, response.code());
Expand All @@ -45,7 +43,7 @@ public Response intercept(Chain chain) throws IOException {
@Test
public void customUserAgentTests() throws Exception {

RestClient.Builder restBuilder = new RestClient.Builder()
RestClient restClient = new RestClient.Builder()
.withBaseUrl("http://localhost")
.withUserAgent("Awesome")
.withInterceptor(new Interceptor() {
Expand All @@ -59,8 +57,8 @@ public Response intercept(Chain chain) throws IOException {
.protocol(Protocol.HTTP_1_1)
.build();
}
});
ServiceClient serviceClient = new ServiceClient(restBuilder.build()) { };
}).build();
ServiceClient serviceClient = new ServiceClient(restClient) { };
Response response = serviceClient.restClient().httpClient()
.newCall(new Request.Builder().get().url("http://localhost").build()).execute();
Assert.assertEquals(200, response.code());
Expand Down

0 comments on commit 1773d49

Please sign in to comment.