Skip to main content

Trigger Update

note

This action only works for API keys that have a test prefix

To make integration easy, Complero provides an endpoint to create an Update for an existing Contact. The event contains the exact data transmitted, so they can fit to the exact test scenarios.

In the following example we will upload a contact, trigger an update for that contact and then get the latest events.

src/main/java/com/complero/examples/CheckForEvents.java
package com.complero.examples;

import com.complero.api.ContactsApi;
import com.complero.api.EventsApi;
import com.complero.api.OperationsApi;
import com.complero.api.models.*;
import com.complero.invoker.ApiClient;
import com.complero.invoker.ApiException;
import com.complero.invoker.ApiResponse;
import com.complero.invoker.auth.ApiKeyAuth;
import com.complero.invoker.auth.Authentication;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class CheckForEvents {
public static void main(String[] args) {
ApiClient apiClient = createApiClient(System.getenv("API_KEY"));

// this is the part where you query your own database Contacts
List<ContactUpload> contacts = collectContactsToUpload();

try {
String operationId = uploadContacts(contacts, apiClient);
Operation operation = waitForOperation(operationId, apiClient);

// in Production, you would now be waiting for people to fill out the webform,
// which would then trigger update/confirm events
// in Development you can simulate such update/confirm events via the
// triggerSandboxEvent endpoint
EventsApi eventsApi = new EventsApi(apiClient);
ApiResponse<WebResponse> response = triggerSandboxEvent(eventsApi);
// Sandbox Event was triggered, let's look at the response message
System.out.println(response.getData().getMessage());

List<ContactEvent> event = getLatestEvent(eventsApi);
// let's have a look at the returned event
System.out.println("Event Type: " + event.get(0).getType());
System.out.println(event.get(0).toString());
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}

// We will demonstrate a failing upload because a validation error is returned
List<ContactUpload> contactsThatFailValidation = collectFailingValidationContactsToUpload();

try {
uploadContacts(contactsThatFailValidation, apiClient);
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}

static ApiResponse<WebResponse> triggerSandboxEvent(EventsApi eventsApi) throws ApiException {
TriggerPublicEvent triggerPublicEvent = new TriggerPublicEvent();

// Here we'll simulate the Contact updating their contact information
Contact contact1 = new Contact();
ContactSection contactSection = new ContactSection();
Address address = new Address();
List<String> emails = new ArrayList<>();
contact1.setFirstName("Petra");
contact1.setLastName("Bauer");
contact1.setId("api-sandbox-1");
contact1.setEmployeeId("1");
address.setZipCode("53111");
address.setCountry("DE");
address.setCity("Bonn");
address.setStreet("Friedrichsstraße");

contactSection.addAddressesItem(address);
emails.add("edcba@gmail.com");
emails.add("google@gmail.com");
contactSection.setEmails(emails);
contact1.setSectionPrivate(contactSection);
triggerPublicEvent.setContact(contact1);

// Set type of the event (update,confirm)
// update if data was changed
// confirm if no data was changed but the Contact simply confirmed that the current data is correct.
triggerPublicEvent.setType(TriggerPublicEvent.TypeEnum.UPDATE);

// call API to trigger Sandbox Event
ApiResponse<WebResponse> response = eventsApi.triggerSandboxEventWithHttpInfo(triggerPublicEvent);
return response;
}

static List<ContactEvent> getLatestEvent(EventsApi eventsApi) throws ApiException {
// call API to retrieve the latest events
return eventsApi.getLatestEvents("update", "1");
}

static ApiClient createApiClient(String apiKey) {
ApiClient apiClient = new ApiClient();

// set auth
Authentication auth = apiClient.getAuthentications().get("ApiKeyAuth");
((ApiKeyAuth) auth).setApiKey("Bearer ".concat(apiKey));
return apiClient;
}

static List<ContactUpload> collectFailingValidationContactsToUpload() {
List<ContactUpload> uploadContacts = new ArrayList<>();

ContactUpload contact1 = new ContactUpload();
Section section = new Section();
List<Email> emails = new ArrayList<Email>();
contact1.setId("1-failing-validation");
contact1.setFirstName("Petra");
contact1.setLastName("Bauer");
contact1.setEmployeeId("1");
emails.add(new Email().email("@gmail.com")); // <-- Invalid email
section.setEmails(emails);
contact1.setSectionPrivate(section);

uploadContacts.add(contact1);

return uploadContacts;
}

static List<ContactUpload> collectContactsToUpload() {
List<ContactUpload> uploadContacts = new ArrayList<>();

ContactUpload contact1 = new ContactUpload();
Section section = new Section();
Address address = new Address();
List<Email> emails = new ArrayList<>();
contact1.setFirstName("Petra");
contact1.setLastName("Bauer");
contact1.setId("api-sandbox-1");
contact1.setEmployeeId("1");
address.setZipCode("53111");
address.setCountry("DE");
address.setCity("Bonn");
address.setStreet("Friedensplatz");

section.addAddressesItem(address);
emails.add(new Email().email("abcde@gmail.com"));
emails.add(new Email().email("gmail@gmail.com"));
section.setEmails(emails);
contact1.setSectionPrivate(section);

uploadContacts.add(contact1);

return uploadContacts;
}

static String uploadContacts(List<ContactUpload> contacts, ApiClient apiClient) {
ContactsApi contactsApi = new ContactsApi(apiClient);
// Uploading contacts
ApiResponse<Void> apiResponse;
try {
apiResponse = contactsApi.bulkUploadContactsWithHttpInfo(contacts);
} catch (ApiException e) {
throw new RuntimeException("Error when uploading contacts", e);
}

System.out.println("HTTP POST request succeeded! Status Code: " + apiResponse.getStatusCode());
System.out.println("Headers: " + apiResponse.getHeaders());

Map<String, List<String>> headers = apiResponse.getHeaders();
// retrieve operation-id from header, so we can check in on the ongoing
// operation
String operationID = headers.get("operation-id").get(0);

return operationID;
}

static Operation waitForOperation(String operationID, ApiClient apiClient) {
OperationsApi operationsApi = new OperationsApi(apiClient);
Operation operation;

System.out.println("OperationID: " + operationID);

boolean statusChecking = true;
int tries = 0;
do {
try {
// inspect the progress of the batch operation
operation = operationsApi.getOperation(operationID);
} catch (ApiException ex) {
throw new RuntimeException("Error during HTTP GET request: " + ex.getMessage());
}
System.out.println("HTTP GET request succeeded! Status Code: " + operation.getStatus());

switch (Objects.requireNonNull(operation.getStatus())) {
case "pending":
System.out.println("Status is 'pending'. Attempt number: " + ++tries);
sleep(3000);
break;

case "success":
System.out.println("Status is 'success'. Ending loop.");
statusChecking = false;
break;

case "error":
// api returned that an error occurred for the upload
throw new RuntimeException("Status is 'error'. Ending loop.");

default:
throw new RuntimeException("Unknown status: " + operation.getStatus());
}
} while (statusChecking);

return operation;
}

static void sleep(int ms) {
System.out.println("Repeating after " + ms + "ms...");
try {
Thread.sleep(ms);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
mvn clean install exec:java -Dexec.mainClass="com.complero.examples.CheckForEvents"