Skip to content

Commit

Permalink
Merge pull request #1866 from xhh/java-okhttp-gson-debugging
Browse files Browse the repository at this point in the history
[Java okhttp-gson] Implement the "debugging" option of ApiClient
  • Loading branch information
wing328 committed Jan 12, 2016
2 parents 8b430cd + b87d6a0 commit 24980ea
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import com.squareup.okhttp.MultipartBuilder;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.Headers;
import com.squareup.okhttp.internal.http.HttpMethod;
import com.squareup.okhttp.logging.HttpLoggingInterceptor;
import com.squareup.okhttp.logging.HttpLoggingInterceptor.Level;

import java.lang.reflect.Type;

Expand Down Expand Up @@ -116,6 +118,8 @@ public class ApiClient {
private OkHttpClient httpClient;
private JSON json;

private HttpLoggingInterceptor loggingInterceptor;

public ApiClient() {
httpClient = new OkHttpClient();
Expand Down Expand Up @@ -452,6 +456,16 @@ public class ApiClient {
* @param debugging To enable (true) or disable (false) debugging
*/
public ApiClient setDebugging(boolean debugging) {
if (debugging != this.debugging) {
if (debugging) {
loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(Level.BODY);
httpClient.interceptors().add(loggingInterceptor);
} else {
httpClient.interceptors().remove(loggingInterceptor);
loggingInterceptor = null;
}
}
this.debugging = debugging;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@
<artifactId>okhttp</artifactId>
<version>${okhttp-version}</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>logging-interceptor</artifactId>
<version>${okhttp-version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
Expand All @@ -133,7 +138,7 @@
</dependencies>
<properties>
<swagger-annotations-version>1.5.0</swagger-annotations-version>
<okhttp-version>2.4.0</okhttp-version>
<okhttp-version>2.7.2</okhttp-version>
<gson-version>2.3.1</gson-version>
<maven-plugin-version>1.0.0</maven-plugin-version>
<junit-version>4.8.1</junit-version>
Expand Down
7 changes: 6 additions & 1 deletion samples/client/petstore/java/okhttp-gson/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@
<artifactId>okhttp</artifactId>
<version>${okhttp-version}</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>logging-interceptor</artifactId>
<version>${okhttp-version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
Expand All @@ -133,7 +138,7 @@
</dependencies>
<properties>
<swagger-annotations-version>1.5.0</swagger-annotations-version>
<okhttp-version>2.4.0</okhttp-version>
<okhttp-version>2.7.2</okhttp-version>
<gson-version>2.3.1</gson-version>
<maven-plugin-version>1.0.0</maven-plugin-version>
<junit-version>4.8.1</junit-version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.Headers;
import com.squareup.okhttp.internal.http.HttpMethod;
import com.squareup.okhttp.logging.HttpLoggingInterceptor;
import com.squareup.okhttp.logging.HttpLoggingInterceptor.Level;

import java.lang.reflect.Type;

Expand Down Expand Up @@ -116,6 +118,8 @@ public class ApiClient {
private OkHttpClient httpClient;
private JSON json;

private HttpLoggingInterceptor loggingInterceptor;

public ApiClient() {
httpClient = new OkHttpClient();

Expand All @@ -141,8 +145,8 @@ public ApiClient() {

// Setup authentications (key: authentication name, value: authentication).
authentications = new HashMap<String, Authentication>();
authentications.put("api_key", new ApiKeyAuth("header", "api_key"));
authentications.put("petstore_auth", new OAuth());
authentications.put("api_key", new ApiKeyAuth("header", "api_key"));
// Prevent the authentications from being modified.
authentications = Collections.unmodifiableMap(authentications);
}
Expand Down Expand Up @@ -451,6 +455,16 @@ public boolean isDebugging() {
* @param debugging To enable (true) or disable (false) debugging
*/
public ApiClient setDebugging(boolean debugging) {
if (debugging != this.debugging) {
if (debugging) {
loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(Level.BODY);
httpClient.interceptors().add(loggingInterceptor);
} else {
httpClient.interceptors().remove(loggingInterceptor);
loggingInterceptor = null;
}
}
this.debugging = debugging;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,15 @@ public void testSetApiKeyAndPrefix() {

@Test
public void testGetAndSetConnectTimeout() {
assertEquals(0, apiClient.getConnectTimeout());
assertEquals(0, apiClient.getHttpClient().getConnectTimeout());

apiClient.setConnectTimeout(10000);
// connect timeout defaults to 10 seconds
assertEquals(10000, apiClient.getConnectTimeout());
assertEquals(10000, apiClient.getHttpClient().getConnectTimeout());

apiClient.setConnectTimeout(0);
assertEquals(0, apiClient.getConnectTimeout());
assertEquals(0, apiClient.getHttpClient().getConnectTimeout());

apiClient.setConnectTimeout(10000);
}

@Test
Expand Down

0 comments on commit 24980ea

Please sign in to comment.