11package api_assured ;
22
3+ import context .ContextStore ;
34import okhttp3 .Headers ;
45import okhttp3 .OkHttpClient ;
56import okhttp3 .Request ;
3940@ SuppressWarnings ({"unused" , "UnusedReturnValue" })
4041public class ServiceGenerator {
4142
43+ OkHttpClient client ;
44+
4245 /**
4346 * The header object containing the headers to be added to the requests.
4447 */
@@ -47,47 +50,52 @@ public class ServiceGenerator {
4750 /**
4851 * A boolean indicating whether to log the headers in the requests.
4952 */
50- boolean printHeaders = Boolean .parseBoolean (PropertyUtilities . getProperty ("log-headers" , "true" ));
53+ boolean printHeaders = Boolean .parseBoolean (ContextStore . get ("log-headers" , "true" ));
5154
5255 /**
5356 * A boolean indicating whether to log detailed information in the requests.
5457 */
55- boolean detailedLogging = Boolean .parseBoolean (PropertyUtilities . getProperty ("detailed-logging" , "false" ));
58+ boolean detailedLogging = Boolean .parseBoolean (ContextStore . get ("detailed-logging" , "false" ));
5659
5760 /**
5861 * A boolean indicating whether to verify the hostname in the requests.
5962 */
60- boolean hostnameVerification = Boolean .parseBoolean (PropertyUtilities . getProperty ("verify-hostname" , "true" ));
63+ boolean hostnameVerification = Boolean .parseBoolean (ContextStore . get ("verify-hostname" , "true" ));
6164
6265 /**
6366 * A boolean indicating whether to print request body in the outgoing requests.
6467 */
65- boolean printRequestBody = Boolean .parseBoolean (PropertyUtilities . getProperty ("print-request-body" , "false" ));
68+ boolean printRequestBody = Boolean .parseBoolean (ContextStore . get ("print-request-body" , "false" ));
6669
6770 /**
6871 * Connection timeout in seconds.
6972 */
70- int connectionTimeout = Integer .parseInt (PropertyUtilities . getProperty ("connection-timeout" , "60" ));
73+ int connectionTimeout = Integer .parseInt (ContextStore . get ("connection-timeout" , "60" ));
7174
7275 /**
7376 * Read timeout in seconds.
7477 */
75- int readTimeout = Integer .parseInt (PropertyUtilities . getProperty ("connection-read-timeout" , "30" ));
78+ int readTimeout = Integer .parseInt (ContextStore . get ("connection-read-timeout" , "30" ));
7679
7780 /**
7881 * Write timeout in seconds.
7982 */
80- int writeTimeout = Integer .parseInt (PropertyUtilities . getProperty ("connection-write-timeout" , "30" ));
83+ int writeTimeout = Integer .parseInt (ContextStore . get ("connection-write-timeout" , "30" ));
8184
8285 /**
8386 * Proxy host. (default: null)
8487 */
85- String proxyHost = PropertyUtilities . getProperty ("proxy-host" , null );
88+ String proxyHost = ContextStore . get ("proxy-host" , null );
8689
8790 /**
8891 * Proxy port (default: 8888)
8992 */
90- int proxyPort = Integer .parseInt (PropertyUtilities .getProperty ("proxy-port" , "8888" ));
93+ int proxyPort = Integer .parseInt (ContextStore .get ("proxy-port" , "8888" ));
94+
95+ /**
96+ * Follow redirects?
97+ */
98+ boolean followRedirects = Boolean .parseBoolean (ContextStore .get ("request-follows-redirects" , "false" ));
9199
92100 /**
93101 * Use proxy?
@@ -185,20 +193,57 @@ public <S> S generate(Class<S> serviceClass) {
185193
186194 if (BASE_URL .isEmpty ()) BASE_URL = (String ) ReflectionUtilities .getFieldValue ("BASE_URL" , serviceClass );
187195
188- HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor ();
189- HttpLoggingInterceptor headerInterceptor = new HttpLoggingInterceptor ();
196+ client = client == null ? getDefaultHttpClient () : client ;
190197
191- if (detailedLogging ){
192- interceptor .setLevel (HttpLoggingInterceptor .Level .BODY );
193- headerInterceptor .setLevel (HttpLoggingInterceptor .Level .HEADERS );
194- }
198+ assert BASE_URL != null ;
199+ @ SuppressWarnings ("deprecation" )
200+ Retrofit retrofit = new Retrofit .Builder ()
201+ .baseUrl (BASE_URL )
202+ .addConverterFactory (GsonConverterFactory .create ())
203+ .addCallAdapterFactory (RxJavaCallAdapterFactory .create ())
204+ .addConverterFactory (JacksonConverterFactory .create ())
205+ .addConverterFactory (ScalarsConverterFactory .create ())
206+ .addConverterFactory (SimpleXmlConverterFactory .create ()) //Deprecated
207+ .addConverterFactory (MoshiConverterFactory .create ())
208+ .addConverterFactory (WireConverterFactory .create ())
209+ .addConverterFactory (ProtoConverterFactory .create ())
210+ .client (client )
211+ .build ();
212+ return retrofit .create (serviceClass );
213+ }
214+
215+ /**
216+ * Sets the OkHttpClient instance to be used by the ServiceGenerator.
217+ *
218+ * @param client the OkHttpClient instance to be set
219+ * @return the current instance of ServiceGenerator for method chaining
220+ */
221+ public ServiceGenerator setHttpClient (OkHttpClient client ){
222+ this .client = client ;
223+ return this ;
224+ }
195225
196- OkHttpClient client = new OkHttpClient .Builder ()
197- .addInterceptor (interceptor )
198- .addInterceptor (headerInterceptor )
226+ /**
227+ * Creates and returns a default OkHttpClient instance with predefined configurations.
228+ * <p>
229+ * This client includes:
230+ * <ul>
231+ * <li>Logging interceptors for both body and headers.</li>
232+ * <li>Connection, read, and write timeouts.</li>
233+ * <li>Redirect handling.</li>
234+ * <li>A network interceptor for modifying requests before execution.</li>
235+ * </ul>
236+ * The interceptor ensures headers are set, logs the request body if enabled,
237+ * and prints headers when required.
238+ *
239+ * @return a configured OkHttpClient instance
240+ */
241+ public OkHttpClient getDefaultHttpClient (){
242+ OkHttpClient client = new OkHttpClient .Builder ()
199243 .connectTimeout (connectionTimeout , TimeUnit .SECONDS )
200244 .readTimeout (readTimeout , TimeUnit .SECONDS )
201245 .writeTimeout (writeTimeout , TimeUnit .SECONDS )
246+ .followRedirects (followRedirects )
202247 .addNetworkInterceptor (chain -> {
203248 Request request = chain .request ().newBuilder ().build ();
204249 request = request .newBuilder ()
@@ -249,6 +294,12 @@ public <S> S generate(Class<S> serviceClass) {
249294 return chain .proceed (request );
250295 }).build ();
251296
297+ if (detailedLogging )
298+ client = new OkHttpClient .Builder (client )
299+ .addInterceptor (getLogginInterceptor (HttpLoggingInterceptor .Level .BODY ))
300+ .addInterceptor (getLogginInterceptor (HttpLoggingInterceptor .Level .HEADERS ))
301+ .build ();
302+
252303 if (!hostnameVerification )
253304 client = new OkHttpClient .Builder (client ).hostnameVerifier ((hostname , session ) -> true ).build ();
254305
@@ -257,22 +308,22 @@ public <S> S generate(Class<S> serviceClass) {
257308 .proxy (new Proxy (Proxy .Type .HTTP , new InetSocketAddress (proxyHost , proxyPort )))
258309 .build ();
259310
311+ return client ;
312+ }
260313
261- assert BASE_URL != null ;
262- @ SuppressWarnings ("deprecation" )
263- Retrofit retrofit = new Retrofit .Builder ()
264- .baseUrl (BASE_URL )
265- .addConverterFactory (GsonConverterFactory .create ())
266- .addCallAdapterFactory (RxJavaCallAdapterFactory .create ())
267- .addConverterFactory (JacksonConverterFactory .create ())
268- .addConverterFactory (ScalarsConverterFactory .create ())
269- .addConverterFactory (SimpleXmlConverterFactory .create ()) //Deprecated
270- .addConverterFactory (MoshiConverterFactory .create ())
271- .addConverterFactory (WireConverterFactory .create ())
272- .addConverterFactory (ProtoConverterFactory .create ())
273- .client (client )
274- .build ();
275- return retrofit .create (serviceClass );
314+ /**
315+ * Creates and returns an {@link HttpLoggingInterceptor} with the specified logging level.
316+ * <p>
317+ * This interceptor is used to log HTTP request and response details,
318+ * such as headers, body, and metadata, depending on the provided level.
319+ *
320+ * @param level the logging level to set for the interceptor (e.g., BODY, HEADERS, BASIC, NONE)
321+ * @return an {@link HttpLoggingInterceptor} instance configured with the specified level
322+ */
323+ public HttpLoggingInterceptor getLogginInterceptor (HttpLoggingInterceptor .Level level ){
324+ HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor ();
325+ interceptor .setLevel (level );
326+ return interceptor ;
276327 }
277328
278329 /**
0 commit comments