Skip to content

Commit 5a5abd1

Browse files
feat(retrofit2): add a method that returns Response object when retrofit2 client invokes an api (#1222)
* feat(retrofit2): add executeCall() to return Response object instead of response type object * test(retrofit2): add a test to verify executeCall()
1 parent 1058c53 commit 5a5abd1

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

kork-retrofit/src/main/java/com/netflix/spinnaker/kork/retrofit/Retrofit2SyncCall.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.netflix.spinnaker.kork.retrofit.exceptions.SpinnakerNetworkException;
2020
import java.io.IOException;
2121
import retrofit2.Call;
22+
import retrofit2.Response;
2223

2324
public class Retrofit2SyncCall<T> {
2425

@@ -30,8 +31,20 @@ public class Retrofit2SyncCall<T> {
3031
* @param <T> Successful response body type.
3132
*/
3233
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) {
3346
try {
34-
return call.execute().body();
47+
return call.execute();
3548
} catch (IOException e) {
3649
throw new SpinnakerNetworkException(e, call.request());
3750
}

kork-retrofit2/src/test/java/com/netflix/spinnaker/kork/retrofit/Retrofit2ServiceFactoryTest.java

+20
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.springframework.context.annotation.Bean;
5151
import org.springframework.context.annotation.Configuration;
5252
import retrofit2.Call;
53+
import retrofit2.Response;
5354
import retrofit2.http.GET;
5455

5556
@SpringBootTest(
@@ -100,6 +101,25 @@ void testRetrofit2Client() {
100101
assertEquals(response.get("message"), "success");
101102
}
102103

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+
103123
@Test
104124
void testRetrofit2Client_withInterceptor() {
105125

0 commit comments

Comments
 (0)