Skip to content

Commit

Permalink
Split templates into two entry classes
Browse files Browse the repository at this point in the history
  • Loading branch information
harveymmaunders committed Feb 21, 2025
1 parent a2d9fe8 commit 99e9ce7
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 97 deletions.
25 changes: 21 additions & 4 deletions client-samples/java/rest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,27 @@ Using retrofit requires more code changes, so it is recommended to initially cal

To configure this application for a different API, the following changes need to be made (there are more details on retrofit below):

| Method | Function in ExampleApplication | What needs to be changed? |
|----------------|---------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| OkHttp Request | `callHelloWorldApi` | The `apiEndpoint` variable in Example Application. |
| Retrofit | `callHelloWorldApiWithRetrofit` | Create a response type in the [`responseTypes`](./src/main/java/com/ms/infra/example/application/responseTypes/) directory. </br> Create a service in the [`services`](./src/main/java/com/ms/infra/example/application/services/) directory. |
| Method | Class | What needs to be changed? |
|----------------|------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| OkHttp Request | `ExampleApplication` | The `apiEndpoint` variable in Example Application. |
| Retrofit | `RetrofitExampleApplication` | Create a response type in the [`responseTypes`](./src/main/java/com/ms/infra/example/application/responseTypes/) directory. </br> Create a service in the [`services`](./src/main/java/com/ms/infra/example/application/services/) directory. |

**Once you have chosen a final template, you can remove the following:**

_If you are using the OkHttp template_
- Files
- [`RetrofitExampleApplication.java`](./src/main/java/com/ms/infra/example/application/RetrofitExampleApplication.java)
- [`MsRetrofitWrapper.java`](./src/main/java/com/ms/infra/example/application/morganStanleyServices/MsRetrofitWrapper.java)
- Test files
- [`TestHelloWorldRestServiceShould.java`](src/test/java/com/ms/infra/example/application/TestHelloWorldRestServiceShould.java)
- Folders
- [`responseTypes`](./src/main/java/com/ms/infra/example/application/responseTypes/)
- [`services`](./src/main/java/com/ms/infra/example/application/services/)
- Libraries (from [build.gradle.kts](./build.gradle.kts))
- Both libraries listed under `// retrofit`

_If you are using the Retrofit template_
- [`ExampleApplication.java`](./src/main/java/com/ms/infra/example/application/ExampleApplication.java)


## Create DER Encoded File
Expand Down
41 changes: 16 additions & 25 deletions client-samples/java/rest/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,29 @@ plugins {
group = "example.application"
version = "1.0-SNAPSHOT"

var microsoftAzureMsal4jVersion = "1.17.3";
var squareupOkhttp3Version = "4.11.0";
var squareupRetrofit2Version = "2.11.0";
var slf4jVersion = "2.0.16";
var eclipseMicroprofileVersion = "6.0";
var smallRyeConfigVersion = "3.9.1";
var squareupOkhttp3MockwebserverVersion = "4.12.0";
var junitJupiterVersion = "5.11.3";
var mockitoCoreVersion = "5.14.2";

dependencies {
implementation(group="com.microsoft.azure", name="msal4j", version=microsoftAzureMsal4jVersion)
implementation(group="com.microsoft.azure", name="msal4j", version="1.17.3")

// okhttp
implementation(group="com.squareup.okhttp3", name="logging-interceptor", version="4.11.0")
implementation(group="com.squareup.okhttp3", name="okhttp", version="4.11.0")

// okhttp/retrofit
implementation(group="com.squareup.okhttp3", name="logging-interceptor", version=squareupOkhttp3Version)
implementation(group="com.squareup.okhttp3", name="okhttp", version=squareupOkhttp3Version)
implementation(group="com.squareup.retrofit2", name="converter-jackson", version=squareupRetrofit2Version)
implementation(group="com.squareup.retrofit2", name="retrofit", version=squareupRetrofit2Version)
// retrofit
implementation(group="com.squareup.retrofit2", name="converter-jackson", version="2.11.0")
implementation(group="com.squareup.retrofit2", name="retrofit", version="2.11.0")

// slf4j
implementation(group="org.slf4j", name="slf4j-api", version=slf4jVersion)
implementation(group="org.slf4j", name="slf4j-simple", version=slf4jVersion)
implementation(group="org.slf4j", name="slf4j-api", version="2.0.16")
implementation(group="org.slf4j", name="slf4j-simple", version="2.0.16")

// microprofile
implementation(group="org.eclipse.microprofile", name="microprofile", version=eclipseMicroprofileVersion)
implementation(group="io.smallrye.config", name="smallrye-config", version=smallRyeConfigVersion)
implementation(group="org.eclipse.microprofile", name="microprofile", version="6.0")
implementation(group="io.smallrye.config", name="smallrye-config", version="3.9.1")

// testing
testImplementation(group="com.squareup.okhttp3", name="mockwebserver", version=squareupOkhttp3MockwebserverVersion)
testImplementation(group="org.junit.jupiter", name="junit-jupiter", version=junitJupiterVersion)
testImplementation(group="org.mockito", name="mockito-core", version=mockitoCoreVersion)

testImplementation(group="com.squareup.okhttp3", name="mockwebserver", version="4.12.0")
testImplementation(group="org.junit.jupiter", name="junit-jupiter", version="5.11.3")
testImplementation(group="org.mockito", name="mockito-core", version="5.14.2")
}

repositories {
Expand All @@ -55,4 +46,4 @@ tasks {
gradleVersion = "8.10"
distributionType = Wrapper.DistributionType.ALL
}
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
package com.ms.infra.example.application;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ms.infra.example.application.config.MicroprofileConfigService;
import com.ms.infra.example.application.interceptors.AuthHeaderInterceptor;
import com.ms.infra.example.application.morganStanleyServices.MsClientAuthTokenService;
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.OkHttpClient;
import okhttp3.Request;
import okhttp3.logging.HttpLoggingInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ExampleApplication {
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 {
Expand All @@ -29,9 +20,6 @@ public static void main(String... args) throws Exception {
public static void run() throws Exception {
// Call the endpoint with an okHttpClient request
callHelloWorldApi();

// Call the endpoint with retrofit
callHelloWorldApiWithRetrofit();
}

public static void callHelloWorldApi() throws Exception {
Expand Down Expand Up @@ -62,39 +50,4 @@ public static void callHelloWorldApi() throws Exception {
logger.info("Response code: {}", response.code());
logger.info("Response: {}", response.body().string());
}

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());
}
});
}
}
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());
}
});
}
}
32 changes: 11 additions & 21 deletions client-samples/java/websockets/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,27 @@ plugins {
group = "example.application"
version = "1.0-SNAPSHOT"

var microsoftAzureMsal4jVersion = "1.17.3";
var squareupOkhttp3Version = "4.11.0";
var slf4jVersion = "2.0.16";
var eclipseMicroprofileVersion = "6.0";
var smallRyeConfigVersion = "3.9.1";
var squareupOkhttp3MockwebserverVersion = "4.12.0";
var ioFabric8MockwebserverVersion = "0.1.8";
var junitJupiterVersion = "5.11.3";
var mockitoCoreVersion = "5.14.2";

dependencies {
// msal4j
implementation(group="com.microsoft.azure", name="msal4j", version=microsoftAzureMsal4jVersion)
implementation(group="com.microsoft.azure", name="msal4j", version="1.17.3")

// okhttp/retrofit
implementation(group="com.squareup.okhttp3", name="logging-interceptor", version=squareupOkhttp3Version)
implementation(group="com.squareup.okhttp3", name="okhttp", version=squareupOkhttp3Version)
implementation(group="com.squareup.okhttp3", name="logging-interceptor", version="4.11.0")
implementation(group="com.squareup.okhttp3", name="okhttp", version="4.11.0")

// slf4j
implementation(group="org.slf4j", name="slf4j-api", version=slf4jVersion)
implementation(group="org.slf4j", name="slf4j-simple", version=slf4jVersion)
implementation(group="org.slf4j", name="slf4j-api", version="2.0.16")
implementation(group="org.slf4j", name="slf4j-simple", version="2.0.16")

// microprofile
implementation(group="org.eclipse.microprofile", name="microprofile", version=eclipseMicroprofileVersion)
implementation(group="io.smallrye.config", name="smallrye-config", version=smallRyeConfigVersion)
implementation(group="org.eclipse.microprofile", name="microprofile", version="6.0")
implementation(group="io.smallrye.config", name="smallrye-config", version="3.9.1")

// testing
testImplementation(group="com.squareup.okhttp3", name="mockwebserver", version=squareupOkhttp3MockwebserverVersion)
testImplementation(group="io.fabric8", name="mockwebserver", version=ioFabric8MockwebserverVersion)
testImplementation(group="org.junit.jupiter", name="junit-jupiter", version=junitJupiterVersion)
testImplementation(group="org.mockito", name="mockito-core", version=mockitoCoreVersion)
testImplementation(group="com.squareup.okhttp3", name="mockwebserver", version="4.12.0")
testImplementation(group="io.fabric8", name="mockwebserver", version="0.1.8")
testImplementation(group="org.junit.jupiter", name="junit-jupiter", version="5.11.3")
testImplementation(group="org.mockito", name="mockito-core", version="5.14.2")
}

repositories {
Expand Down

0 comments on commit 99e9ce7

Please sign in to comment.