29
29
*/
30
30
public class WhatsappApiServiceGenerator {
31
31
32
- static OkHttpClient sharedClient ;
32
+ static OkHttpClient sharedClient ;
33
33
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 )
40
40
);
41
41
42
42
@ 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 );
44
45
45
46
private WhatsappApiServiceGenerator () {
46
- throw new IllegalStateException ("Cannot instantiate WhatsappApiServiceGenerator is an utility class!" );
47
+ throw new IllegalStateException ( "Cannot instantiate WhatsappApiServiceGenerator is an utility class!" );
47
48
}
48
49
49
50
static {
@@ -52,9 +53,9 @@ private WhatsappApiServiceGenerator() {
52
53
53
54
public static OkHttpClient createDefaultHttpClient () {
54
55
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 ();
58
59
}
59
60
60
61
/**
@@ -75,34 +76,42 @@ public static OkHttpClient createDefaultHttpClient() {
75
76
* @param port the port
76
77
* @param username the username
77
78
* @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>
80
84
*/
81
85
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 );
84
88
85
89
sharedClient = sharedClient .newBuilder ()
86
- .proxySelector (proxySelector )
87
- .build ();
90
+ .proxySelector ( proxySelector )
91
+ .build ();
88
92
89
93
if (username == null || pwd == null ) {
90
94
//Without authentication
91
95
return ;
92
96
}
93
97
94
- CustomProxyAuthenticator proxyAuthenticator = new CustomProxyAuthenticator (username , pwd );
98
+ CustomProxyAuthenticator proxyAuthenticator = new CustomProxyAuthenticator ( username , pwd );
95
99
96
100
sharedClient = sharedClient .newBuilder ()
97
- .proxyAuthenticator (proxyAuthenticator )
98
- .build ();
101
+ .proxyAuthenticator ( proxyAuthenticator )
102
+ .build ();
99
103
}
100
104
101
- public static void setTimeout (final Duration duration ) {
105
+ public static void setTimeouts (final Duration callTimout , final Duration connectTimeout ,
106
+ final Duration readTimeout ) {
102
107
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" );
104
111
sharedClient = sharedClient .newBuilder ()
105
- .callTimeout ( duration )
112
+ .callTimeout ( callTimout )
113
+ .connectTimeout ( connectTimeout )
114
+ .readTimeout ( readTimeout )
106
115
.build ();
107
116
}
108
117
@@ -117,22 +126,22 @@ public static void setTimeout(final Duration duration) {
117
126
*/
118
127
public static <S > S createService (Class <S > serviceClass , String token , String baseUrl ) {
119
128
Retrofit .Builder retrofitBuilder = new Retrofit .Builder ()
120
- .baseUrl (baseUrl )
121
- .addConverterFactory (converterFactory );
129
+ .baseUrl ( baseUrl )
130
+ .addConverterFactory ( converterFactory );
122
131
123
132
if (token == null ) {
124
- retrofitBuilder .client (sharedClient );
133
+ retrofitBuilder .client ( sharedClient );
125
134
} else {
126
135
127
- AuthenticationInterceptor interceptor = new AuthenticationInterceptor (token );
136
+ AuthenticationInterceptor interceptor = new AuthenticationInterceptor ( token );
128
137
OkHttpClient adaptedClient = sharedClient .newBuilder ()
129
- .addInterceptor (interceptor )
130
- .build ();
131
- retrofitBuilder .client (adaptedClient );
138
+ .addInterceptor ( interceptor )
139
+ .build ();
140
+ retrofitBuilder .client ( adaptedClient );
132
141
}
133
142
134
143
Retrofit retrofit = retrofitBuilder .build ();
135
- return retrofit .create (serviceClass );
144
+ return retrofit .create ( serviceClass );
136
145
}
137
146
138
147
/**
@@ -146,7 +155,7 @@ public static <S> S createService(Class<S> serviceClass, String token, String ba
146
155
public static <S > S createService (Class <S > serviceClass , String token ) {
147
156
148
157
var baseUrl = WhatsappApiConfig .getBaseDomain ();
149
- return createService (serviceClass , token , baseUrl );
158
+ return createService ( serviceClass , token , baseUrl );
150
159
151
160
}
152
161
@@ -164,11 +173,11 @@ public static <T> T executeSync(Call<T> call) {
164
173
165
174
return response .body ();
166
175
} else {
167
- WhatsappApiError apiError = getWhatsappApiError (response );
168
- throw new WhatsappApiException (apiError );
176
+ WhatsappApiError apiError = getWhatsappApiError ( response );
177
+ throw new WhatsappApiException ( apiError );
169
178
}
170
179
} catch (IOException e ) {
171
- throw new WhatsappApiException (e );
180
+ throw new WhatsappApiException ( e );
172
181
}
173
182
}
174
183
@@ -184,21 +193,22 @@ public static <T> MediaFile executeDownloadSync(Call<T> call) {
184
193
Response <T > response = call .execute ();
185
194
if (response .isSuccessful ()) {
186
195
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 ];
188
198
ResponseBody body = (ResponseBody ) response .body ();
189
199
190
200
assert body != null ;
191
- return new MediaFile (fileName , body .bytes ());
201
+ return new MediaFile ( fileName , body .bytes () );
192
202
} else {
193
203
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 ) );
196
206
}
197
- WhatsappApiError apiError = getWhatsappApiError (response );
198
- throw new WhatsappApiException (apiError );
207
+ WhatsappApiError apiError = getWhatsappApiError ( response );
208
+ throw new WhatsappApiException ( apiError );
199
209
}
200
210
} catch (IOException e ) {
201
- throw new WhatsappApiException (e );
211
+ throw new WhatsappApiException ( e );
202
212
}
203
213
}
204
214
@@ -211,10 +221,10 @@ public static <T> MediaFile executeDownloadSync(Call<T> call) {
211
221
* @throws IOException the io exception
212
222
*/
213
223
public static WhatsappApiError getWhatsappApiError (Response <?> response ) throws WhatsappApiException , IOException {
214
- Objects .requireNonNull (errorBodyConverter );
224
+ Objects .requireNonNull ( errorBodyConverter );
215
225
ResponseBody responseBody = response .errorBody ();
216
- Objects .requireNonNull (responseBody );
217
- return errorBodyConverter .convert (responseBody );
226
+ Objects .requireNonNull ( responseBody );
227
+ return errorBodyConverter .convert ( responseBody );
218
228
219
229
}
220
230
0 commit comments