1
1
package com .algolia ;
2
2
3
+ import com .algolia .utils .Requester ;
3
4
import java .io .IOException ;
4
5
import java .io .UnsupportedEncodingException ;
5
6
import java .lang .reflect .Type ;
9
10
import java .time .OffsetDateTime ;
10
11
import java .util .*;
11
12
import java .util .Map .Entry ;
12
- import java .util .concurrent .TimeUnit ;
13
13
import okhttp3 .*;
14
14
import okhttp3 .internal .http .HttpMethod ;
15
- import okhttp3 .logging .HttpLoggingInterceptor ;
16
- import okhttp3 .logging .HttpLoggingInterceptor .Level ;
17
15
18
16
public class ApiClient {
19
17
@@ -25,36 +23,21 @@ public class ApiClient {
25
23
26
24
private DateFormat dateFormat ;
27
25
28
- private OkHttpClient httpClient ;
29
26
private JSON json ;
30
27
31
- private HttpLoggingInterceptor loggingInterceptor ;
28
+ private Requester requester ;
32
29
33
30
/*
34
- * Basic constructor for ApiClient
31
+ * Constructor for ApiClient with custom Requester
35
32
*/
36
- public ApiClient (String appId , String apiKey ) {
33
+ public ApiClient (String appId , String apiKey , Requester requester ) {
37
34
json = new JSON ();
38
35
setUserAgent ("OpenAPI-Generator/0.1.0/java" );
39
- initHttpClient ();
40
36
41
37
this .basePath = "https://" + appId + "-1.algolianet.com" ;
42
38
this .appId = appId ;
43
39
this .apiKey = apiKey ;
44
- }
45
-
46
- private void initHttpClient () {
47
- initHttpClient (Collections .<Interceptor >emptyList ());
48
- }
49
-
50
- private void initHttpClient (List <Interceptor > interceptors ) {
51
- OkHttpClient .Builder builder = new OkHttpClient .Builder ();
52
- builder .addNetworkInterceptor (getProgressInterceptor ());
53
- for (Interceptor interceptor : interceptors ) {
54
- builder .addInterceptor (interceptor );
55
- }
56
-
57
- httpClient = builder .build ();
40
+ this .requester = requester ;
58
41
}
59
42
60
43
/**
@@ -130,20 +113,7 @@ public boolean isDebugging() {
130
113
* @return ApiClient
131
114
*/
132
115
public ApiClient setDebugging (boolean debugging ) {
133
- if (debugging != this .debugging ) {
134
- if (debugging ) {
135
- loggingInterceptor = new HttpLoggingInterceptor ();
136
- loggingInterceptor .setLevel (Level .BODY );
137
- httpClient =
138
- httpClient .newBuilder ().addInterceptor (loggingInterceptor ).build ();
139
- } else {
140
- final OkHttpClient .Builder builder = httpClient .newBuilder ();
141
- builder .interceptors ().remove (loggingInterceptor );
142
- httpClient = builder .build ();
143
- loggingInterceptor = null ;
144
- }
145
- }
146
- this .debugging = debugging ;
116
+ requester .setDebugging (debugging );
147
117
return this ;
148
118
}
149
119
@@ -153,7 +123,7 @@ public ApiClient setDebugging(boolean debugging) {
153
123
* @return Timeout in milliseconds
154
124
*/
155
125
public int getConnectTimeout () {
156
- return httpClient . connectTimeoutMillis ();
126
+ return requester . getConnectTimeout ();
157
127
}
158
128
159
129
/**
@@ -164,11 +134,7 @@ public int getConnectTimeout() {
164
134
* @return Api client
165
135
*/
166
136
public ApiClient setConnectTimeout (int connectionTimeout ) {
167
- httpClient =
168
- httpClient
169
- .newBuilder ()
170
- .connectTimeout (connectionTimeout , TimeUnit .MILLISECONDS )
171
- .build ();
137
+ requester .setConnectTimeout (connectionTimeout );
172
138
return this ;
173
139
}
174
140
@@ -178,7 +144,7 @@ public ApiClient setConnectTimeout(int connectionTimeout) {
178
144
* @return Timeout in milliseconds
179
145
*/
180
146
public int getReadTimeout () {
181
- return httpClient . readTimeoutMillis ();
147
+ return requester . getReadTimeout ();
182
148
}
183
149
184
150
/**
@@ -189,11 +155,7 @@ public int getReadTimeout() {
189
155
* @return Api client
190
156
*/
191
157
public ApiClient setReadTimeout (int readTimeout ) {
192
- httpClient =
193
- httpClient
194
- .newBuilder ()
195
- .readTimeout (readTimeout , TimeUnit .MILLISECONDS )
196
- .build ();
158
+ requester .setReadTimeout (readTimeout );
197
159
return this ;
198
160
}
199
161
@@ -203,7 +165,7 @@ public ApiClient setReadTimeout(int readTimeout) {
203
165
* @return Timeout in milliseconds
204
166
*/
205
167
public int getWriteTimeout () {
206
- return httpClient . writeTimeoutMillis ();
168
+ return requester . getWriteTimeout ();
207
169
}
208
170
209
171
/**
@@ -214,11 +176,7 @@ public int getWriteTimeout() {
214
176
* @return Api client
215
177
*/
216
178
public ApiClient setWriteTimeout (int writeTimeout ) {
217
- httpClient =
218
- httpClient
219
- .newBuilder ()
220
- .writeTimeout (writeTimeout , TimeUnit .MILLISECONDS )
221
- .build ();
179
+ requester .setWriteTimeout (writeTimeout );
222
180
return this ;
223
181
}
224
182
@@ -672,7 +630,7 @@ public Call buildCall(
672
630
callback
673
631
);
674
632
675
- return httpClient .newCall (request );
633
+ return requester .newCall (request );
676
634
}
677
635
678
636
/**
@@ -815,26 +773,4 @@ public RequestBody buildRequestBodyFormEncoding(
815
773
}
816
774
return formBuilder .build ();
817
775
}
818
-
819
- /**
820
- * Get network interceptor to add it to the httpClient to track download progress for async
821
- * requests.
822
- */
823
- private Interceptor getProgressInterceptor () {
824
- return new Interceptor () {
825
- @ Override
826
- public Response intercept (Interceptor .Chain chain ) throws IOException {
827
- final Request request = chain .request ();
828
- final Response originalResponse = chain .proceed (request );
829
- if (request .tag () instanceof ApiCallback ) {
830
- final ApiCallback callback = (ApiCallback ) request .tag ();
831
- return originalResponse
832
- .newBuilder ()
833
- .body (new ProgressResponseBody (originalResponse .body (), callback ))
834
- .build ();
835
- }
836
- return originalResponse ;
837
- }
838
- };
839
- }
840
776
}
0 commit comments