Skip to content

Commit

Permalink
Move RestClient into azure runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
jianghaolu committed Jun 16, 2016
1 parent 05105b4 commit fecda5c
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

package com.microsoft.azure;

import com.microsoft.rest.RestClient;

/**
* An instance of this class describes an environment in Azure.
*/
Expand Down Expand Up @@ -86,8 +84,8 @@ public String getBaseUrl() {
*
* @return a builder for the rest client.
*/
public RestClient.Builder newRestClientBuilder() {
return new AzureRestClient.Builder()
public RestClient.Builder.Buildable newRestClientBuilder() {
return new RestClient.Builder()
.withDefaultBaseUrl(this)
.withInterceptor(new RequestIdHeaderInterceptor());
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@

package com.microsoft.azure;

import com.microsoft.rest.RestClient;
import com.microsoft.rest.ServiceClient;

/**
* ServiceClient is the abstraction for accessing REST operations and their payload data types.
*/
public abstract class AzureServiceClient {
RestClient restClient;
/**
* The RestClient instance storing all information needed for making REST calls.
*/
private RestClient restClient;

protected AzureServiceClient(String baseUrl) {
this(new RestClient.Builder().withBaseUrl(baseUrl)
Expand All @@ -38,4 +38,11 @@ protected AzureServiceClient(RestClient restClient) {
public String userAgent() {
return "Azure-SDK-For-Java/" + getClass().getPackage().getImplementationVersion();
}

/**
* @return the {@link RestClient} instance.
*/
public RestClient restClient() {
return restClient;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
*
*/

package com.microsoft.rest;
package com.microsoft.azure;

import com.microsoft.azure.serializer.AzureJacksonMapperAdapter;
import com.microsoft.rest.BaseUrlHandler;
import com.microsoft.rest.CustomHeadersInterceptor;
import com.microsoft.rest.UserAgentInterceptor;
import com.microsoft.rest.credentials.ServiceClientCredentials;
import com.microsoft.rest.retry.RetryHandler;
import com.microsoft.rest.serializer.JacksonMapperAdapter;
Expand Down Expand Up @@ -188,14 +192,23 @@ public Buildable withDefaultBaseUrl(Class<?> serviceClientClass) {
Field field = serviceClientClass.getDeclaredField("DEFAULT_BASE_URL");
field.setAccessible(true);
baseUrl = (String) field.get(null);
} catch (NoSuchFieldException e) {
throw new UnsupportedOperationException("Cannot read static field DEFAULT_BASE_URL", e);
} catch (IllegalAccessException e) {
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new UnsupportedOperationException("Cannot read static field DEFAULT_BASE_URL", e);
}
return buildable;
}

/**
* Sets the base URL with the default from the Azure Environment.
*
* @param environment the environment the application is running in
* @return the builder itself for chaining
*/
public RestClient.Builder.Buildable withDefaultBaseUrl(AzureEnvironment environment) {
withBaseUrl(environment.getBaseUrl());
return buildable;
}

/**
* The inner class from which a Rest Client can be built.
*/
Expand All @@ -207,7 +220,7 @@ public class Buildable {
* @return the builder itself for chaining.
*/
public Buildable withUserAgent(String userAgent) {
userAgentInterceptor.setUserAgent(userAgent);
userAgentInterceptor.withUserAgent(userAgent);
return this;
}

Expand Down Expand Up @@ -253,7 +266,7 @@ public Buildable withInterceptor(Interceptor interceptor) {
* @return a {@link RestClient}.
*/
public RestClient build() {
JacksonMapperAdapter mapperAdapter = new JacksonMapperAdapter();
AzureJacksonMapperAdapter mapperAdapter = new AzureJacksonMapperAdapter();
OkHttpClient httpClient = httpClientBuilder
.addInterceptor(baseUrlHandler)
.addInterceptor(customHeadersInterceptor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

package com.microsoft.azure;

import com.microsoft.rest.RestClient;
import com.microsoft.rest.retry.RetryHandler;

import org.junit.Assert;
Expand All @@ -25,7 +24,7 @@ public class RequestIdHeaderInterceptorTests {

@Test
public void newRequestIdForEachCall() throws Exception {
RestClient restClient = new AzureRestClient.Builder()
RestClient restClient = new RestClient.Builder()
.withBaseUrl("http://localhost")
.withInterceptor(new RequestIdHeaderInterceptor())
.withInterceptor(new Interceptor() {
Expand Down Expand Up @@ -59,7 +58,7 @@ public Response intercept(Chain chain) throws IOException {

@Test
public void sameRequestIdForRetry() throws Exception {
RestClient restClient = new AzureRestClient.Builder()
RestClient restClient = new RestClient.Builder()
.withBaseUrl("http://localhost")
.withInterceptor(new RequestIdHeaderInterceptor())
.withInterceptor(new RetryHandler())
Expand Down
20 changes: 15 additions & 5 deletions client-runtime/src/main/java/com/microsoft/rest/ServiceClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
* ServiceClient is the abstraction for accessing REST operations and their payload data types.
*/
public abstract class ServiceClient {
protected OkHttpClient httpClient;

protected Retrofit retrofit;

protected JacksonMapperAdapter mapperAdapter;
/** The HTTP client. */
private OkHttpClient httpClient;
/** The Retrofit instance. */
private Retrofit retrofit;
/** The adapter to a Jackson {@link com.fasterxml.jackson.databind.ObjectMapper}. */
private JacksonMapperAdapter mapperAdapter;

/**
* Initializes a new instance of the ServiceClient class.
Expand Down Expand Up @@ -64,14 +65,23 @@ protected ServiceClient(String baseUrl, OkHttpClient.Builder clientBuilder, Retr
.build();
}

/**
* @return the Retrofit instance.
*/
public Retrofit retrofit() {
return this.retrofit;
}

/**
* @return the HTTP client.
*/
public OkHttpClient httpClient() {
return this.httpClient;
}

/**
* @return the adapter to a Jackson {@link com.fasterxml.jackson.databind.ObjectMapper}.
*/
public JacksonMapperAdapter mapperAdapter() {
return this.mapperAdapter;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,22 @@ public UserAgentInterceptor() {
* Overwrite the User-Agent header.
*
* @param userAgent the new user agent value.
* @return the user agent interceptor itself
*/
public void setUserAgent(String userAgent) {
public UserAgentInterceptor withUserAgent(String userAgent) {
this.userAgent = userAgent;
return this;
}

/**
* Append a text to the User-Agent header.
*
* @param userAgent the user agent value to append.
* @return the user agent interceptor itself
*/
public void appendUserAgent(String userAgent) {
public UserAgentInterceptor appendUserAgent(String userAgent) {
this.userAgent += " " + userAgent;
return this;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import com.microsoft.rest.credentials.BasicAuthenticationCredentials;
import com.microsoft.rest.credentials.TokenCredentials;
import com.microsoft.rest.serializer.JacksonMapperAdapter;

import org.junit.Assert;
import org.junit.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,18 @@

import com.microsoft.rest.retry.RetryHandler;

import com.microsoft.rest.serializer.JacksonMapperAdapter;
import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;

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;

import java.io.IOException;

public class RetryHandlerTests {
@Test
public void exponentialRetryEndOn501() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@

package com.microsoft.rest;

import com.microsoft.rest.serializer.JacksonMapperAdapter;
import org.junit.Assert;
import org.junit.Test;
import retrofit2.Retrofit;

import java.io.IOException;

Expand All @@ -19,6 +17,7 @@
import okhttp3.Protocol;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;

public class ServiceClientTests {
@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public Response intercept(Chain chain) throws IOException {
@Test
public void customUserAgentTests() throws Exception {
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder()
.addInterceptor(new UserAgentInterceptor())
.addInterceptor(new UserAgentInterceptor().withUserAgent("Awesome"))
.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Expand Down

0 comments on commit fecda5c

Please sign in to comment.