2
2
3
3
import api_assured .exceptions .FailedCallException ;
4
4
import com .fasterxml .jackson .core .JsonProcessingException ;
5
+ import okhttp3 .ResponseBody ;
5
6
import okio .Buffer ;
6
7
import properties .PropertyUtilities ;
7
8
import retrofit2 .Call ;
8
9
import retrofit2 .Response ;
9
10
import utils .*;
10
- import utils .mapping . MappingUtilities ;
11
+ import utils .reflection . ReflectionUtilities ;
11
12
12
13
import java .io .IOException ;
13
14
import java .nio .charset .StandardCharsets ;
14
15
import java .util .Objects ;
15
16
16
- import static utils .mapping . MappingUtilities .Json .*;
17
+ import static utils .MappingUtilities .Json .*;
17
18
import static utils .reflection .ReflectionUtilities .getPreviousMethodName ;
18
19
import static utils .StringUtilities .Color .*;
20
+ import static utils .reflection .ReflectionUtilities .isOfType ;
19
21
20
22
/**
21
23
* This abstract class represents a caller that performs API calls, logs the results and returns objects in response bodies.
@@ -32,7 +34,7 @@ public abstract class Caller {
32
34
/**
33
35
* A static boolean variable that determines whether logs should be kept for API calls.
34
36
*/
35
- public static boolean keepLogs ;
37
+ protected static boolean keepLogs ;
36
38
37
39
/**
38
40
* A Printer object for logging.
@@ -143,16 +145,18 @@ private static <T> Response<T> getResponse(Call<T> call, boolean printBody) thro
143
145
if (keepLogs ) log .success ("The response code is: " + response .code ());
144
146
if (keepLogs && !response .message ().isEmpty ()) log .info (response .message ());
145
147
if (printBody && printableResponse ) log .info ("The response body is: \n " + getJsonString (body ));
146
- assert body != null ;
147
148
return Response .success (body , response .raw ());
148
149
}
149
150
else {
150
151
log .warning ("The response code is: " + response .code ());
151
- if (!response .message ().isEmpty ()) log .warning (response .message ());
152
- assert response .errorBody () != null ;
153
- if (printBody ) {
154
- Object errorBody = getJsonString (getErrorObject (response , Object .class ));
155
- String errorLog = errorBody .equals ("null" ) ? "The error body is empty." : "The error body is: \n " + errorBody ;
152
+ if (response .message ().length ()>0 ) log .warning (response .message ());
153
+ if (response .errorBody () != null && printBody ) {
154
+ Object errorBody = isOfType (response , Object .class ) ?
155
+ getJsonString (getErrorObject (response , Object .class )) :
156
+ response .raw ();
157
+ String errorLog = errorBody .equals ("null" ) ?
158
+ "The error body is empty." :
159
+ "The error body is: \n " + errorBody ;
156
160
log .warning (errorLog );
157
161
}
158
162
return Response .error (response .errorBody (), response .raw ());
@@ -173,12 +177,18 @@ private static <T> Response<T> getResponse(Call<T> call, boolean printBody) thro
173
177
*
174
178
* @see MappingUtilities.Json#fromJsonString(String, Class)
175
179
*/
180
+ @ SuppressWarnings ("unchecked" )
176
181
private static <ErrorModel > ErrorModel getErrorObject (Response <?> response , Class <ErrorModel > errorModel ) throws JsonProcessingException {
177
182
assert response .errorBody () != null ;
183
+ if (errorModel .isAssignableFrom (ResponseBody .class ))
184
+ return (ErrorModel ) response .errorBody ();
178
185
try (Buffer errorBuffer = response .errorBody ().source ().getBuffer ().clone ()) {
179
186
String bodyString = errorBuffer .readString (StandardCharsets .UTF_8 );
180
- if (!StringUtilities .isBlank (bodyString ))
181
- return fromJsonString (bodyString , errorModel );
187
+ if (!StringUtilities .isBlank (bodyString )) {
188
+ if (errorModel .isAssignableFrom (String .class ))
189
+ return (ErrorModel ) bodyString ;
190
+ else return fromJsonString (bodyString , errorModel );
191
+ }
182
192
else
183
193
return null ;
184
194
}
@@ -249,9 +259,12 @@ private static <ResponseModel> Response<ResponseModel> call(
249
259
*/
250
260
@ SuppressWarnings ("unchecked" )
251
261
private static <ErrorModel > ErrorModel getErrorBody (Response <?> response , Class <?>... errorModels ){
252
- for (Class <?> errorModel :errorModels ){
262
+ for (Class <?> errorClass :errorModels ){
253
263
try {
254
- return (ErrorModel ) fromJsonString (getJsonStringFor (getErrorObject (response , errorModel )), errorModel );
264
+ ErrorModel errorModel = (ErrorModel ) getErrorObject (response , errorClass );
265
+ assert errorModel != null ;
266
+ if (errorClass .isAssignableFrom (errorModel .getClass ()))
267
+ return errorModel ;
255
268
}
256
269
catch (JsonProcessingException ignored ) {}
257
270
}
0 commit comments