Skip to content

Commit 067d8de

Browse files
committed
Set all timeouts
1 parent 250274f commit 067d8de

File tree

2 files changed

+63
-49
lines changed

2 files changed

+63
-49
lines changed

src/main/java/com/whatsapp/api/WhatsappApiServiceGenerator.java

Lines changed: 56 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,22 @@
2929
*/
3030
public class WhatsappApiServiceGenerator {
3131

32-
static OkHttpClient sharedClient;
32+
static OkHttpClient sharedClient;
3333
private static final Converter.Factory converterFactory = JacksonConverterFactory.create(
34-
new ObjectMapper()
35-
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
36-
.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false)
37-
.configure(DeserializationFeature.FAIL_ON_UNRESOLVED_OBJECT_IDS, false)
38-
.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, false)
39-
.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE, true)
34+
new ObjectMapper()
35+
.configure( DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false )
36+
.configure( DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false )
37+
.configure( DeserializationFeature.FAIL_ON_UNRESOLVED_OBJECT_IDS, false )
38+
.configure( DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, false )
39+
.configure( DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE, true )
4040
);
4141

4242
@SuppressWarnings("unchecked")
43-
private static final Converter<ResponseBody, WhatsappApiError> errorBodyConverter = (Converter<ResponseBody, WhatsappApiError>) converterFactory.responseBodyConverter(WhatsappApiError.class, new Annotation[0], null);
43+
private static final Converter<ResponseBody, WhatsappApiError> errorBodyConverter = (Converter<ResponseBody, WhatsappApiError>) converterFactory.responseBodyConverter(
44+
WhatsappApiError.class, new Annotation[0], null );
4445

4546
private WhatsappApiServiceGenerator() {
46-
throw new IllegalStateException("Cannot instantiate WhatsappApiServiceGenerator is an utility class!");
47+
throw new IllegalStateException( "Cannot instantiate WhatsappApiServiceGenerator is an utility class!" );
4748
}
4849

4950
static {
@@ -52,9 +53,9 @@ private WhatsappApiServiceGenerator() {
5253

5354
public static OkHttpClient createDefaultHttpClient() {
5455
return new OkHttpClient.Builder()//
55-
.callTimeout(20, TimeUnit.SECONDS)//
56-
.pingInterval(20, TimeUnit.SECONDS)//
57-
.build();
56+
.callTimeout( 20, TimeUnit.SECONDS )//
57+
.pingInterval( 20, TimeUnit.SECONDS )//
58+
.build();
5859
}
5960

6061
/**
@@ -75,34 +76,42 @@ public static OkHttpClient createDefaultHttpClient() {
7576
* @param port the port
7677
* @param username the username
7778
* @param pwd the pwd
78-
* @see <a href="https://square.github.io/okhttp/4.x/okhttp/okhttp3/-ok-http-client/-builder/proxy-selector/">Proxy Selector</a>
79-
* @see <a href="https://square.github.io/okhttp/4.x/okhttp/okhttp3/-ok-http-client/-builder/proxy-authenticator/">Proxy Authenticator</a>
79+
* @see <a href="https://square.github.io/okhttp/4.x/okhttp/okhttp3/-ok-http-client/-builder/proxy-selector/">Proxy
80+
* Selector</a>
81+
* @see <a
82+
* href="https://square.github.io/okhttp/4.x/okhttp/okhttp3/-ok-http-client/-builder/proxy-authenticator/">Proxy
83+
* Authenticator</a>
8084
*/
8185
public static void setHttpProxy(String host, int port, String username, String pwd) {
82-
Objects.requireNonNull(host, "Host cannot be null");
83-
CustomHttpProxySelector proxySelector = new CustomHttpProxySelector(host, port);
86+
Objects.requireNonNull( host, "Host cannot be null" );
87+
CustomHttpProxySelector proxySelector = new CustomHttpProxySelector( host, port );
8488

8589
sharedClient = sharedClient.newBuilder()
86-
.proxySelector(proxySelector)
87-
.build();
90+
.proxySelector( proxySelector )
91+
.build();
8892

8993
if (username == null || pwd == null) {
9094
//Without authentication
9195
return;
9296
}
9397

94-
CustomProxyAuthenticator proxyAuthenticator = new CustomProxyAuthenticator(username, pwd);
98+
CustomProxyAuthenticator proxyAuthenticator = new CustomProxyAuthenticator( username, pwd );
9599

96100
sharedClient = sharedClient.newBuilder()
97-
.proxyAuthenticator(proxyAuthenticator)
98-
.build();
101+
.proxyAuthenticator( proxyAuthenticator )
102+
.build();
99103
}
100104

101-
public static void setTimeout(final Duration duration) {
105+
public static void setTimeouts(final Duration callTimout, final Duration connectTimeout,
106+
final Duration readTimeout) {
102107

103-
Objects.requireNonNull(duration, "Duration cannot be null");
108+
Objects.requireNonNull( callTimout, "Call duration cannot be null" );
109+
Objects.requireNonNull( connectTimeout, "Connect duration cannot be null" );
110+
Objects.requireNonNull( readTimeout, "Read duration cannot be null" );
104111
sharedClient = sharedClient.newBuilder()
105-
.callTimeout( duration )
112+
.callTimeout( callTimout )
113+
.connectTimeout( connectTimeout )
114+
.readTimeout( readTimeout )
106115
.build();
107116
}
108117

@@ -117,22 +126,22 @@ public static void setTimeout(final Duration duration) {
117126
*/
118127
public static <S> S createService(Class<S> serviceClass, String token, String baseUrl) {
119128
Retrofit.Builder retrofitBuilder = new Retrofit.Builder()
120-
.baseUrl(baseUrl)
121-
.addConverterFactory(converterFactory);
129+
.baseUrl( baseUrl )
130+
.addConverterFactory( converterFactory );
122131

123132
if (token == null) {
124-
retrofitBuilder.client(sharedClient);
133+
retrofitBuilder.client( sharedClient );
125134
} else {
126135

127-
AuthenticationInterceptor interceptor = new AuthenticationInterceptor(token);
136+
AuthenticationInterceptor interceptor = new AuthenticationInterceptor( token );
128137
OkHttpClient adaptedClient = sharedClient.newBuilder()
129-
.addInterceptor(interceptor)
130-
.build();
131-
retrofitBuilder.client(adaptedClient);
138+
.addInterceptor( interceptor )
139+
.build();
140+
retrofitBuilder.client( adaptedClient );
132141
}
133142

134143
Retrofit retrofit = retrofitBuilder.build();
135-
return retrofit.create(serviceClass);
144+
return retrofit.create( serviceClass );
136145
}
137146

138147
/**
@@ -146,7 +155,7 @@ public static <S> S createService(Class<S> serviceClass, String token, String ba
146155
public static <S> S createService(Class<S> serviceClass, String token) {
147156

148157
var baseUrl = WhatsappApiConfig.getBaseDomain();
149-
return createService(serviceClass, token, baseUrl);
158+
return createService( serviceClass, token, baseUrl );
150159

151160
}
152161

@@ -164,11 +173,11 @@ public static <T> T executeSync(Call<T> call) {
164173

165174
return response.body();
166175
} else {
167-
WhatsappApiError apiError = getWhatsappApiError(response);
168-
throw new WhatsappApiException(apiError);
176+
WhatsappApiError apiError = getWhatsappApiError( response );
177+
throw new WhatsappApiException( apiError );
169178
}
170179
} catch (IOException e) {
171-
throw new WhatsappApiException(e);
180+
throw new WhatsappApiException( e );
172181
}
173182
}
174183

@@ -184,21 +193,22 @@ public static <T> MediaFile executeDownloadSync(Call<T> call) {
184193
Response<T> response = call.execute();
185194
if (response.isSuccessful()) {
186195

187-
var fileName = Objects.requireNonNull(response.headers().get("Content-Disposition")).split("=")[1];
196+
var fileName = Objects.requireNonNull( response.headers().get( "Content-Disposition" ) )
197+
.split( "=" )[1];
188198
ResponseBody body = (ResponseBody) response.body();
189199

190200
assert body != null;
191-
return new MediaFile(fileName, body.bytes());
201+
return new MediaFile( fileName, body.bytes() );
192202
} else {
193203
if (response.code() == 404) {
194-
var error = new Error(404, null, 404, null, "Not found", null, null, null, false, null, null);
195-
throw new WhatsappApiException(new WhatsappApiError(error));
204+
var error = new Error( 404, null, 404, null, "Not found", null, null, null, false, null, null );
205+
throw new WhatsappApiException( new WhatsappApiError( error ) );
196206
}
197-
WhatsappApiError apiError = getWhatsappApiError(response);
198-
throw new WhatsappApiException(apiError);
207+
WhatsappApiError apiError = getWhatsappApiError( response );
208+
throw new WhatsappApiException( apiError );
199209
}
200210
} catch (IOException e) {
201-
throw new WhatsappApiException(e);
211+
throw new WhatsappApiException( e );
202212
}
203213
}
204214

@@ -211,10 +221,10 @@ public static <T> MediaFile executeDownloadSync(Call<T> call) {
211221
* @throws IOException the io exception
212222
*/
213223
public static WhatsappApiError getWhatsappApiError(Response<?> response) throws WhatsappApiException, IOException {
214-
Objects.requireNonNull(errorBodyConverter);
224+
Objects.requireNonNull( errorBodyConverter );
215225
ResponseBody responseBody = response.errorBody();
216-
Objects.requireNonNull(responseBody);
217-
return errorBodyConverter.convert(responseBody);
226+
Objects.requireNonNull( responseBody );
227+
return errorBodyConverter.convert( responseBody );
218228

219229
}
220230

src/test/java/com/whatsapp/api/WhatsappApiServiceGeneratorTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,18 +120,22 @@ void testSetHttpProxy_WithAuthentication() {
120120

121121
/**
122122
* Method under test:
123-
* {@link WhatsappApiServiceGenerator#setTimeout(Duration)}
123+
* {@link WhatsappApiServiceGenerator#setTimeouts(Duration, Duration, Duration)}
124124
*/
125125
@Test
126-
void testSetTimeout() {
126+
void testSetTimeouts() {
127127

128128
assertEquals( 20 * 1000, WhatsappApiServiceGenerator.getSharedClient().callTimeoutMillis() );
129129

130130
// Set timeout in shared client
131-
WhatsappApiServiceGenerator.setTimeout( Duration.ofMinutes( 1L ) );
131+
WhatsappApiServiceGenerator.setTimeouts( Duration.ofMinutes( 1L ), Duration.ofMinutes( 2L ), Duration.ofMinutes( 3L ) );
132132

133133
// Check if timeout is set
134134
assertEquals( 60 * 1000, WhatsappApiServiceGenerator.getSharedClient().callTimeoutMillis() );
135+
136+
assertEquals( 2 * 60 * 1000, WhatsappApiServiceGenerator.getSharedClient().connectTimeoutMillis() );
137+
138+
assertEquals( 3 * 60 * 1000, WhatsappApiServiceGenerator.getSharedClient().readTimeoutMillis() );
135139
}
136140

137141
}

0 commit comments

Comments
 (0)