# Configure an API Client
source: https://developer.mastercard.com/commercial-event-notifications/documentation/tutorial/tutorial-2/index.md

### Step 1: Create a new Gradle project by using any Java IDE {#step-1-create-a-new-gradle-project-by-using-any-java-ide}

### Step 2: Update your Gradle file with the required libraries {#step-2-update-your-gradle-file-with-the-required-libraries}

Refer to the [reference application](https://developer.mastercard.com/commercial-event-notifications/documentation/reference_application_tutorial/index.md) page for the Gradle file.

```json
openApiGenerate {
    generatorName = "java"
    !--inputSpec = "$rootDir/src/test/resources/"
    outputDir = "$buildDir"
    apiPackage = "com.mastercard.commercial.api"
    modelPackage = "com.mastercard.commercial.model"
    generateApiTests = false
    generateModelTests = false
    configOptions.set([
            dateLibrary         : "java8",
            interfaceOnly       : "true",
            skipDefaultInterface: "true",
            useBeanValidation   : "true"
    ])}
```

```sh
dependencies {
    implementation "org.springframework.boot:spring-boot-starter:${springBootStarterVersion}"
    implementation "javax.ws.rs:javax.ws.rs-api:${rsapiVersion}"
    implementation "org.projectlombok:lombok:${lombokVersion}"
    implementation "com.mastercard.developer:oauth1-signer:${oauthSignerVersion}"
    implementation "org.apache.oltu.oauth2:org.apache.oltu.oauth2.client:${oauthClientVersion}"
    implementation "io.swagger:swagger-annotations:${swaggerAnnotationVersion}"
    implementation "com.google.code.gson:gson:${gsonVersion}"
    implementation "io.gsonfire:gson-fire:${gsonFireVersion}"
    implementation "com.squareup.okhttp3:okhttp:${okhttpVersion}"
    implementation "com.squareup.okhttp3:logging-interceptor:${okhttpVersion}"
    implementation "org.junit.platform:junit-platform-suite-engine:${junitPlatformVersion}"
    implementation "org.junit.platform:junit-platform-suite-api:${junitPlatformVersion}"
    implementation "org.junit.platform:junit-platform-commons:${junitPlatformVersion}"
    implementation "org.junit.platform:junit-platform-launcher:${junitPlatformVersion}"
    implementation "org.assertj:assertj-core:${assertJVersion}"
    implementation "com.google.code.findbugs:jsr305:${findBugsVersion}"
    testImplementation "org.springframework.boot:spring-boot-starter-test:${springBootStarterVersion}"
    testImplementation "org.junit.jupiter:junit-jupiter-engine:${jupiterVersion}"
    testImplementation "org.junit.jupiter:junit-jupiter-api:${jupiterVersion}"
    testImplementation "org.junit.platform:junit-platform-suite-engine:${junitPlatformVersion}"
    testImplementation "org.junit.platform:junit-platform-engine:${junitPlatformVersion}"
    compileOnly "javax:javaee-web-api:${webApiVersion}"

    annotationProcessor "org.mapstruct:mapstruct-processor:${mapstructProcessorVersion}", "org.projectlombok:lombok:${lombokVersion}", "org.projectlombok:lombok-mapstruct-binding:${mapstructBindingVersion}"

    // Test Dependencies
    testImplementation("org.projectlombok:lombok:${lombokVersion}")
    testAnnotationProcessor("org.projectlombok:lombok:${lombokVersion}")
}
```

### Step 3: Generate API sources from the yaml file using the Gradle plugin {#step-3-generate-api-sources-from-the-yaml-file-using-the-gradle-plugin}

### Step 4: Configure the API client {#step-4-configure-the-api-client}

```bash
  client:
    consumer-key: # INSERT YOUR CONSUMER KEY HERE
    signing-key-alias: # INSERT YOUR KEY ALIAS HERE
    signing-key-password: # INSERT YOUR KEYSTORE PASSWORD HERE
    signing-key-pkcs12FilePath: src/test/resources/REPLACE_WITH_YOUR_CERTIFICATE.p12
    # copy the downloaded .p12 file to /src/test/resources location.
```

### Step 5: Initialize the API client {#step-5-initialize-the-api-client}

```js
  PrivateKey signingKey = AuthenticationUtils.loadSigningKey(
        signingKeyPkcs12FilePath,
        signingKeyAlias,
        signingKeyPassword
  );

  OkHttp2OAuth1Interceptor authInterceptor = new OkHttp2OAuth1Interceptor(
        consumerKey, signingKey);

  ApiClient client = new ApiClient();
  client.setBasePath("https://sbx.stage.api.mastercard.com/in-control/protected");
  client.getHttpClient().networkInterceptors().add(new ForceJsonResponseInterceptor()); // Without this, the gateway would return an XML response
  client.getHttpClient().networkInterceptors().add(authInterceptor);

  /**
  * Add "Format=JSON" to the request for the service/gateway to return a JSON response.
  */
  private class ForceJsonResponseInterceptor implements Interceptor {

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request originalRequest = chain.request();
        String withJsonFormatUrl = withJsonFormat(originalRequest.uri().toString());
        Request newRequest = originalRequest.newBuilder().url(withJsonFormatUrl).build();

        return chain.proceed(newRequest);
    }

    private String withJsonFormat(String uri) {
        return String.format("%s%s%s",
                uri,
                uri.contains("?") ? "&" : "?",
                "Format=JSON"
        );
    }
  }
```

### Step 6: Update the Commercial Event Notifications properties depending on the environment {#step-6-update-the-commercial-event-notifications-properties-depending-on-the-environment}

```sh
  in-control:
    bank-identification-number: 556950 # Stage environment BIN range
    corp-guid: SENTTY13379505AC33CDA796BCBE850EADAF625FAD6B071E8D100CB1ECBEA76AD402DE1519037869 # Stage environment corporate GUID
    base-path: https://mtf.apiedge.mastercard.com/commercial # Commercial Event Notifications URL Base Path.
```

