Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(retrofit2): add a method that returns Response object when retrofit2 client invokes an api #1222

Merged
merged 2 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.netflix.spinnaker.kork.retrofit.exceptions.SpinnakerNetworkException;
import java.io.IOException;
import retrofit2.Call;
import retrofit2.Response;

public class Retrofit2SyncCall<T> {

Expand All @@ -30,8 +31,20 @@ public class Retrofit2SyncCall<T> {
* @param <T> Successful response body type.
*/
public static <T> T execute(Call<T> call) {
return executeCall(call).body();
}

/**
* Handle IOExceptions from {@link Call}.execute method centrally, instead of all places that make
* retrofit2 API calls.
*
* @throws SpinnakerNetworkException if IOException occurs.
* @param call call to be executed
* @return Response<T> response after execution
*/
public static <T> Response<T> executeCall(Call<T> call) {
try {
return call.execute().body();
return call.execute();
} catch (IOException e) {
throw new SpinnakerNetworkException(e, call.request());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.http.GET;

@SpringBootTest(
Expand Down Expand Up @@ -100,6 +101,25 @@ void testRetrofit2Client() {
assertEquals(response.get("message"), "success");
}

@Test
void testRetrofit2ClientWithResponse() {
stubFor(
get(urlEqualTo("/test"))
.willReturn(
aResponse()
.withHeader("Content-Type", "application/json")
.withBody("{\"message\": \"success\", \"code\": 200}")));

ServiceEndpoint serviceEndpoint =
new DefaultServiceEndpoint("retrofit2service", "http://localhost:" + port);
Retrofit2TestService retrofit2TestService =
serviceClientProvider.getService(Retrofit2TestService.class, serviceEndpoint);
Response<Map<String, String>> response =
Retrofit2SyncCall.executeCall(retrofit2TestService.getSomething());

assertEquals(response.body().get("message"), "success");
}

@Test
void testRetrofit2Client_withInterceptor() {

Expand Down
Loading