Commit 5a5abd1 1 parent 1058c53 commit 5a5abd1 Copy full SHA for 5a5abd1
File tree 2 files changed +34
-1
lines changed
kork-retrofit/src/main/java/com/netflix/spinnaker/kork/retrofit
kork-retrofit2/src/test/java/com/netflix/spinnaker/kork/retrofit
2 files changed +34
-1
lines changed Original file line number Diff line number Diff line change 19
19
import com .netflix .spinnaker .kork .retrofit .exceptions .SpinnakerNetworkException ;
20
20
import java .io .IOException ;
21
21
import retrofit2 .Call ;
22
+ import retrofit2 .Response ;
22
23
23
24
public class Retrofit2SyncCall <T > {
24
25
@@ -30,8 +31,20 @@ public class Retrofit2SyncCall<T> {
30
31
* @param <T> Successful response body type.
31
32
*/
32
33
public static <T > T execute (Call <T > call ) {
34
+ return executeCall (call ).body ();
35
+ }
36
+
37
+ /**
38
+ * Handle IOExceptions from {@link Call}.execute method centrally, instead of all places that make
39
+ * retrofit2 API calls.
40
+ *
41
+ * @throws SpinnakerNetworkException if IOException occurs.
42
+ * @param call call to be executed
43
+ * @return Response<T> response after execution
44
+ */
45
+ public static <T > Response <T > executeCall (Call <T > call ) {
33
46
try {
34
- return call .execute (). body () ;
47
+ return call .execute ();
35
48
} catch (IOException e ) {
36
49
throw new SpinnakerNetworkException (e , call .request ());
37
50
}
Original file line number Diff line number Diff line change 50
50
import org .springframework .context .annotation .Bean ;
51
51
import org .springframework .context .annotation .Configuration ;
52
52
import retrofit2 .Call ;
53
+ import retrofit2 .Response ;
53
54
import retrofit2 .http .GET ;
54
55
55
56
@ SpringBootTest (
@@ -100,6 +101,25 @@ void testRetrofit2Client() {
100
101
assertEquals (response .get ("message" ), "success" );
101
102
}
102
103
104
+ @ Test
105
+ void testRetrofit2ClientWithResponse () {
106
+ stubFor (
107
+ get (urlEqualTo ("/test" ))
108
+ .willReturn (
109
+ aResponse ()
110
+ .withHeader ("Content-Type" , "application/json" )
111
+ .withBody ("{\" message\" : \" success\" , \" code\" : 200}" )));
112
+
113
+ ServiceEndpoint serviceEndpoint =
114
+ new DefaultServiceEndpoint ("retrofit2service" , "http://localhost:" + port );
115
+ Retrofit2TestService retrofit2TestService =
116
+ serviceClientProvider .getService (Retrofit2TestService .class , serviceEndpoint );
117
+ Response <Map <String , String >> response =
118
+ Retrofit2SyncCall .executeCall (retrofit2TestService .getSomething ());
119
+
120
+ assertEquals (response .body ().get ("message" ), "success" );
121
+ }
122
+
103
123
@ Test
104
124
void testRetrofit2Client_withInterceptor () {
105
125
You can’t perform that action at this time.
0 commit comments