Skip to content

Commit

Permalink
Add user agent to every method
Browse files Browse the repository at this point in the history
  • Loading branch information
jianghaolu committed May 19, 2016
1 parent 9d48136 commit b887e80
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ protected AzureServiceClient(String baseUrl) {
*/
protected AzureServiceClient(RestClient restClient) {
super(restClient);
restClient.userAgent().setUserAgent("Azure-SDK-For-Java", getClass().getPackage().getImplementationVersion(), null);
}
}
34 changes: 20 additions & 14 deletions client-runtime/src/main/java/com/microsoft/rest/RestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,21 @@ public final class RestClient {
private BaseUrlHandler baseUrlHandler;
/** The adapter to a Jackson {@link com.fasterxml.jackson.databind.ObjectMapper}. */
private JacksonMapperAdapter mapperAdapter;
/** The interceptor to set 'User-Agent' header. */
private UserAgentInterceptor userAgentInterceptor;

private RestClient(OkHttpClient httpClient,
Retrofit retrofit,
ServiceClientCredentials credentials,
CustomHeadersInterceptor customHeadersInterceptor,
UserAgentInterceptor userAgentInterceptor,
BaseUrlHandler baseUrlHandler,
JacksonMapperAdapter mapperAdapter) {
this.httpClient = httpClient;
this.retrofit = retrofit;
this.credentials = credentials;
this.customHeadersInterceptor = customHeadersInterceptor;
this.userAgentInterceptor = userAgentInterceptor;
this.baseUrlHandler = baseUrlHandler;
this.mapperAdapter = mapperAdapter;
}
Expand Down Expand Up @@ -86,6 +90,16 @@ public Retrofit retrofit() {
return retrofit;
}

/**
* Get the user agent interceptor where custom user agents can be set or
* appended.
*
* @return the user agent interceptor.
*/
public UserAgentInterceptor userAgent() {
return this.userAgentInterceptor;
}

/**
* Get the base URL currently set. If it's a customizable URL, the updated
* URL instead of the raw one might be returned.
Expand Down Expand Up @@ -134,6 +148,8 @@ public static class Builder {
private BaseUrlHandler baseUrlHandler;
/** The adapter to a Jackson {@link com.fasterxml.jackson.databind.ObjectMapper}. */
private JacksonMapperAdapter mapperAdapter;
/** The interceptor to set 'User-Agent' header. */
private UserAgentInterceptor userAgentInterceptor;

/**
* Creates an instance of the builder with a base URL to the service.
Expand Down Expand Up @@ -165,34 +181,23 @@ public Builder(String baseUrl, OkHttpClient.Builder httpClientBuilder, Retrofit.
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
customHeadersInterceptor = new CustomHeadersInterceptor();
baseUrlHandler = new BaseUrlHandler(baseUrl);
userAgentInterceptor = new UserAgentInterceptor();
// Set up OkHttp client
this.httpClientBuilder = httpClientBuilder
.cookieJar(new JavaNetCookieJar(cookieManager))
.addInterceptor(new UserAgentInterceptor());
.addInterceptor(userAgentInterceptor);
// Set up rest adapter
this.retrofitBuilder = retrofitBuilder.baseUrl(baseUrl);
}

/**
* Sets the base URL.
*
* @param baseUrl the dynamic base URL.
* @return the builder itself for chaining.
*/
public Builder withBaseUrl(String baseUrl) {
this.retrofitBuilder.baseUrl(baseUrl);
this.baseUrlHandler = new BaseUrlHandler(baseUrl);
return this;
}

/**
* Sets the user agent header.
*
* @param userAgent the user agent header.
* @return the builder itself for chaining.
*/
public Builder withUserAgent(String userAgent) {
this.httpClientBuilder.addInterceptor(new UserAgentInterceptor(userAgent));
this.userAgentInterceptor.setUserAgent(userAgent);
return this;
}

Expand Down Expand Up @@ -264,6 +269,7 @@ public RestClient build() {
.build(),
credentials,
customHeadersInterceptor,
userAgentInterceptor,
baseUrlHandler,
mapperAdapter);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.internal.Version;

import java.io.IOException;

Expand All @@ -33,28 +32,44 @@ public class UserAgentInterceptor implements Interceptor {
* 'User-Agent' header.
*/
public UserAgentInterceptor() {
this(DEFAULT_USER_AGENT_HEADER);
setUserAgent(DEFAULT_USER_AGENT_HEADER, null, null);
}

/**
* Initialize an instance of {@link UserAgentInterceptor} class with the specified
* 'User-Agent' header.
*
* @param userAgent the 'User-Agent' header value.
*/
public UserAgentInterceptor(String userAgent) {
this.userAgent = userAgent;
public void setUserAgent(String userAgent) {
setUserAgent(userAgent, null, null);
}

public void setUserAgent(String product, String version, String extras) {
this.userAgent = product;
if (version != null) {
this.userAgent += "/" + version;
}
if (extras != null) {
this.userAgent += String.format(" (%s)", extras);
}
}

public void appendUserAgent(String userAgent) {
appendUserAgent(userAgent, null, null);
}

public void appendUserAgent(String product, String version, String extras) {
this.userAgent += " " + product;
if (version != null) {
this.userAgent += "/" + version;
}
if (extras != null) {
this.userAgent += String.format(" (%s)", extras);
}
}

@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
String header = request.header("User-Agent");
if (header == null || header.equals(Version.userAgent()) || header.equals(DEFAULT_USER_AGENT_HEADER)) {
request = chain.request().newBuilder()
.header("User-Agent", userAgent)
.build();
}
request = chain.request().newBuilder()
.header("User-Agent", userAgent + " " + header)
.build();
return chain.proceed(request);
}
}
71 changes: 37 additions & 34 deletions client-runtime/src/test/java/com/microsoft/rest/UserAgentTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@

package com.microsoft.rest;

import com.microsoft.rest.serializer.JacksonMapperAdapter;
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,42 +20,46 @@
public class UserAgentTests {
@Test
public void defaultUserAgentTests() throws Exception {
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();
}
});
RestClient.Builder restBuilder = new RestClient.Builder("http://localhost", clientBuilder, retrofitBuilder);
RestClient.Builder restBuilder = new RestClient.Builder("http://localhost")
.withInterceptor(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();
}
})
.withMapperAdapter(new JacksonMapperAdapter());
ServiceClient serviceClient = new ServiceClient(restBuilder.build()) { };
Response response = serviceClient.restClient().httpClient()
.newCall(new Request.Builder().get().url("http://localhost").build()).execute();
Assert.assertEquals(200, response.code());
}

@Test
public void customUserAgentTests() throws Exception {
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();
}
});
RestClient.Builder restBuilder = new RestClient.Builder("http://localhost", clientBuilder, retrofitBuilder);
RestClient.Builder restBuilder = new RestClient.Builder("http://localhost")
.withUserAgent("Awesome")
.withInterceptor(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();
}
})
.withMapperAdapter(new JacksonMapperAdapter());
ServiceClient serviceClient = new ServiceClient(restBuilder.build()) { };
Response response = serviceClient.restClient().httpClient()
.newCall(new Request.Builder().get().url("http://localhost").build()).execute();
Assert.assertEquals(200, response.code());
}
}

0 comments on commit b887e80

Please sign in to comment.