forked from loafle/openapi-generator-original
Merge pull request #1866 from xhh/java-okhttp-gson-debugging
[Java okhttp-gson] Implement the "debugging" option of ApiClient
This commit is contained in:
commit
24980ea1c6
@ -11,6 +11,8 @@ import com.squareup.okhttp.MultipartBuilder;
|
|||||||
import com.squareup.okhttp.MediaType;
|
import com.squareup.okhttp.MediaType;
|
||||||
import com.squareup.okhttp.Headers;
|
import com.squareup.okhttp.Headers;
|
||||||
import com.squareup.okhttp.internal.http.HttpMethod;
|
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;
|
import java.lang.reflect.Type;
|
||||||
|
|
||||||
@ -116,6 +118,8 @@ public class ApiClient {
|
|||||||
private OkHttpClient httpClient;
|
private OkHttpClient httpClient;
|
||||||
private JSON json;
|
private JSON json;
|
||||||
|
|
||||||
|
private HttpLoggingInterceptor loggingInterceptor;
|
||||||
|
|
||||||
public ApiClient() {
|
public ApiClient() {
|
||||||
httpClient = new OkHttpClient();
|
httpClient = new OkHttpClient();
|
||||||
|
|
||||||
@ -452,6 +456,16 @@ public class ApiClient {
|
|||||||
* @param debugging To enable (true) or disable (false) debugging
|
* @param debugging To enable (true) or disable (false) debugging
|
||||||
*/
|
*/
|
||||||
public ApiClient setDebugging(boolean 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;
|
this.debugging = debugging;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
@ -117,6 +117,11 @@
|
|||||||
<artifactId>okhttp</artifactId>
|
<artifactId>okhttp</artifactId>
|
||||||
<version>${okhttp-version}</version>
|
<version>${okhttp-version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.squareup.okhttp</groupId>
|
||||||
|
<artifactId>logging-interceptor</artifactId>
|
||||||
|
<version>${okhttp-version}</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.google.code.gson</groupId>
|
<groupId>com.google.code.gson</groupId>
|
||||||
<artifactId>gson</artifactId>
|
<artifactId>gson</artifactId>
|
||||||
@ -133,7 +138,7 @@
|
|||||||
</dependencies>
|
</dependencies>
|
||||||
<properties>
|
<properties>
|
||||||
<swagger-annotations-version>1.5.0</swagger-annotations-version>
|
<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>
|
<gson-version>2.3.1</gson-version>
|
||||||
<maven-plugin-version>1.0.0</maven-plugin-version>
|
<maven-plugin-version>1.0.0</maven-plugin-version>
|
||||||
<junit-version>4.8.1</junit-version>
|
<junit-version>4.8.1</junit-version>
|
||||||
|
@ -117,6 +117,11 @@
|
|||||||
<artifactId>okhttp</artifactId>
|
<artifactId>okhttp</artifactId>
|
||||||
<version>${okhttp-version}</version>
|
<version>${okhttp-version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.squareup.okhttp</groupId>
|
||||||
|
<artifactId>logging-interceptor</artifactId>
|
||||||
|
<version>${okhttp-version}</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.google.code.gson</groupId>
|
<groupId>com.google.code.gson</groupId>
|
||||||
<artifactId>gson</artifactId>
|
<artifactId>gson</artifactId>
|
||||||
@ -133,7 +138,7 @@
|
|||||||
</dependencies>
|
</dependencies>
|
||||||
<properties>
|
<properties>
|
||||||
<swagger-annotations-version>1.5.0</swagger-annotations-version>
|
<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>
|
<gson-version>2.3.1</gson-version>
|
||||||
<maven-plugin-version>1.0.0</maven-plugin-version>
|
<maven-plugin-version>1.0.0</maven-plugin-version>
|
||||||
<junit-version>4.8.1</junit-version>
|
<junit-version>4.8.1</junit-version>
|
||||||
|
@ -11,6 +11,8 @@ import com.squareup.okhttp.MultipartBuilder;
|
|||||||
import com.squareup.okhttp.MediaType;
|
import com.squareup.okhttp.MediaType;
|
||||||
import com.squareup.okhttp.Headers;
|
import com.squareup.okhttp.Headers;
|
||||||
import com.squareup.okhttp.internal.http.HttpMethod;
|
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;
|
import java.lang.reflect.Type;
|
||||||
|
|
||||||
@ -116,6 +118,8 @@ public class ApiClient {
|
|||||||
private OkHttpClient httpClient;
|
private OkHttpClient httpClient;
|
||||||
private JSON json;
|
private JSON json;
|
||||||
|
|
||||||
|
private HttpLoggingInterceptor loggingInterceptor;
|
||||||
|
|
||||||
public ApiClient() {
|
public ApiClient() {
|
||||||
httpClient = new OkHttpClient();
|
httpClient = new OkHttpClient();
|
||||||
|
|
||||||
@ -141,8 +145,8 @@ public class ApiClient {
|
|||||||
|
|
||||||
// Setup authentications (key: authentication name, value: authentication).
|
// Setup authentications (key: authentication name, value: authentication).
|
||||||
authentications = new HashMap<String, Authentication>();
|
authentications = new HashMap<String, Authentication>();
|
||||||
authentications.put("api_key", new ApiKeyAuth("header", "api_key"));
|
|
||||||
authentications.put("petstore_auth", new OAuth());
|
authentications.put("petstore_auth", new OAuth());
|
||||||
|
authentications.put("api_key", new ApiKeyAuth("header", "api_key"));
|
||||||
// Prevent the authentications from being modified.
|
// Prevent the authentications from being modified.
|
||||||
authentications = Collections.unmodifiableMap(authentications);
|
authentications = Collections.unmodifiableMap(authentications);
|
||||||
}
|
}
|
||||||
@ -451,6 +455,16 @@ public class ApiClient {
|
|||||||
* @param debugging To enable (true) or disable (false) debugging
|
* @param debugging To enable (true) or disable (false) debugging
|
||||||
*/
|
*/
|
||||||
public ApiClient setDebugging(boolean 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;
|
this.debugging = debugging;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
@ -186,14 +186,15 @@ public class ApiClientTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetAndSetConnectTimeout() {
|
public void testGetAndSetConnectTimeout() {
|
||||||
assertEquals(0, apiClient.getConnectTimeout());
|
// connect timeout defaults to 10 seconds
|
||||||
assertEquals(0, apiClient.getHttpClient().getConnectTimeout());
|
|
||||||
|
|
||||||
apiClient.setConnectTimeout(10000);
|
|
||||||
assertEquals(10000, apiClient.getConnectTimeout());
|
assertEquals(10000, apiClient.getConnectTimeout());
|
||||||
assertEquals(10000, apiClient.getHttpClient().getConnectTimeout());
|
assertEquals(10000, apiClient.getHttpClient().getConnectTimeout());
|
||||||
|
|
||||||
apiClient.setConnectTimeout(0);
|
apiClient.setConnectTimeout(0);
|
||||||
|
assertEquals(0, apiClient.getConnectTimeout());
|
||||||
|
assertEquals(0, apiClient.getHttpClient().getConnectTimeout());
|
||||||
|
|
||||||
|
apiClient.setConnectTimeout(10000);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
x
Reference in New Issue
Block a user