-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split templates into two entry classes
- Loading branch information
1 parent
a2d9fe8
commit 99e9ce7
Showing
5 changed files
with
111 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
.../java/rest/src/main/java/com/ms/infra/example/application/RetrofitExampleApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.ms.infra.example.application; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.ms.infra.example.application.morganStanleyServices.MsRetrofitWrapper; | ||
import com.ms.infra.example.application.responseTypes.HelloWorldGetServicesResponse; | ||
import com.ms.infra.example.application.services.HelloWorldRestService; | ||
import okhttp3.logging.HttpLoggingInterceptor; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import retrofit2.Call; | ||
import retrofit2.Callback; | ||
import retrofit2.Response; | ||
|
||
public class RetrofitExampleApplication { | ||
private static final Logger logger = LoggerFactory.getLogger(ExampleApplication.class); | ||
private static final ObjectMapper objectMapper = new ObjectMapper(); | ||
private static final String apiEndpoint = "hello/world/v1/services"; | ||
|
||
public static void main(String... args) throws Exception { | ||
run(); | ||
} | ||
|
||
public static void run() throws Exception { | ||
// Call the endpoint with retrofit | ||
callHelloWorldApiWithRetrofit(); | ||
} | ||
|
||
public static void callHelloWorldApiWithRetrofit() throws Exception { | ||
MsRetrofitWrapper msRetrofitWrapper = new MsRetrofitWrapper(HttpLoggingInterceptor.Level.BODY); | ||
HelloWorldRestService helloWorldRestService = msRetrofitWrapper.createService(HelloWorldRestService.class); | ||
|
||
Call<HelloWorldGetServicesResponse> call = helloWorldRestService.getServices(); | ||
|
||
// this function will make an API call | ||
call.enqueue(new Callback<HelloWorldGetServicesResponse>() { | ||
// this will run if the API call returns any response | ||
// it acts similar to a try catch, and will fail over to the onFailure method is anything errors | ||
@Override | ||
public void onResponse(Call<HelloWorldGetServicesResponse> call, Response<HelloWorldGetServicesResponse> response) { | ||
if (response.isSuccessful()) { | ||
// load the response body into our own custom class | ||
HelloWorldGetServicesResponse data = response.body(); | ||
try { | ||
logger.info("Response code: {}", response.code()); | ||
logger.info(objectMapper.writeValueAsString(data)); | ||
} catch (JsonProcessingException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} else { | ||
// there was a response but was not successful | ||
logger.error("Response was not successful, response code: {}", response.code()); | ||
} | ||
} | ||
|
||
@Override | ||
public void onFailure(Call<HelloWorldGetServicesResponse> call, Throwable t) { | ||
// the trace error from onResponse is passed into this class as Throwable t | ||
logger.error("Failure with response, issue: {}", t.toString()); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters