This project is no longer maintained and should be used for historical purposes only.
The SDK is broken up into two projects:
- The
wsimportedJava models, that live incom.bronto.api.model - The SDK wrapper providing a convenient API for various apps
- Asynchronous web service calls
- Retry on common network errors
- Automatic pagination with the
readAllobject operations - Chainable request building
- Component driven object operations
The project is built SBT because it is a Superior Build Tool.
> sbt
> project bronto-api
> publishLocal
<dependency>
<groupId>com.bronto.api</groupId>
<artifactId>bronto-api-sdk</artifactId>
<version>1.0.11</version>
</dependency>
import com.bronto.api.*;
import com.bronto.api.model.*;
import static com.bronto.api.model.ObjectBuilder.newObject;
String apiToken = "<Your API token>";
BrontoApi client = new BrontoClient(apiToken);
String sessionId = client.login();ContactOperations contactOps = new ContactOperations(client);
ContactObject contact = contactOps.newObject()
.set("email", "user@example.com")
.set("status", ContactStatus.ONBOARDING)
.add("listIds", listId)
.add("fields", newObject(ContactField.class)
.set("fieldId", fieldId)
.set("content", value).get())
.get();
try {
WriteResult result = contactOps.add(contact);
} catch (Exception e) {
// Handle exception
}contactOps.addOrUpdate(contacts);contactOps.delete(contact);Calendar createdThreshold = Calendar.getInstance();
createdThreshold.add(Calendar.DATE, -7);
final ContactReadRequest readContacts = new ContactReadRequest()
.withStatus(ContactStatus.TRANSACTIONAL)
.withCreated(FilterOperator.AFTER, createdThreshold.getTime())
.withListId(listId);
List<ContactObject> contacts = null;
while(contacts = contactOps.read(readContacts); !contacts.isEmpty()) {
for (ContactObject contact: contacts) {
System.out.println(contact.getEmail());
}
readContacts.next();
}for (ContactObject contact : contactOps.readAll(readContacts)) {
System.out.println(contact.getEmail());
}MailListOperations listOps = new MailListOperations(client);
MailListObject list = listOps.get(new MailListReadRequest().withName("My Example List"));listOps.clear(list);FieldObject field = newObject(FieldObject.class)
.set("name", "API Field")
.set("label", "API Field Label")
.set("visibility", FieldVisibility.PRIVATE)
.set("type", FieldType.TEXT).get();
client.transport(FieldObject.class).add(field);ObjectOperations<ContentTagObject> contentTagOps = client.transport(ContentTagObject.class);
ContentTagObject tag = contentTagOps.get(new ContentTagReadRequest().withId("123"));MessageOperations messageOps = new MessageOperations(client);
MessageObject message = messageOps.get(new MessageReadRequest().withId("123"));
/*
* Content from Bronto's new message editor is a JSON object rather than an HTML
* string. Use the new MessageContentDetector class to determine what type of
* content you're dealing with (MessageOperations will automatically store the
* content as the correct type of object - this is just a helper method for
* altering message content via the API).
*
* Also be aware that the message content is stored in both the JSON's "body"
* element _and_ the "ui" element. The "ui" element is what's used to re-create the
* message in the message editor. Failing to change the content in the "ui" element
* will cause your changes to be lost when the message is re-opened and saved in
* Bronto.
*/
boolean isJson = MessageContentDetector.hasJsonContent(message);ObjectOperations<DeliveryObject> deliveryOps = client.transport(DeliveryObject.class);
DeliveryRecipientObject recipient = new DeliveryRecipientObject();
recipient.setDeliveryType(DeliveryRecipientSelection.SELECTED.getApiValue());
recipient.setType(DeliveryRecipientType.CONTACT.getApiValue());
recipient.setId(contact.getId());
DeliveryObject delivery = deliveryOps.newObject(new Date());
delivery.setType(DeliveryType.TRANSACTIONAL.getApiValue());
delivery.setMessageId(message.getId());
delivery.setFromEmail("user@example.com");
delivery.setFromName("Example Sender");
delivery.getRecipients().add(recipient);
deliveryOps.add(delivery);deliveryOps.read(new DeliveryReadRequest().setId(delivery.getId())).get();ObjectOperations<DeliveryRecipientStatObject> deliveryStats = client.transport(DeliveryRecipientStatObject.class);
DeliveryRecipientReadRequest readDelivery = new DeliveryRecipientReadRequest().setId(delivery.getId());
for (DeliveryRecipientStatObject stat : deliveryStats.readAll(readDelivery)) {
// Do something with stat
}All calls, with the exception of login and readAll, can be made
asynchronously. This means there's two deritives of an asynchronous API call:
- The call itself, with expected arguments, returning a
Future. - The call with the expected arguments and an
AsyncHandler(see below)
Asynchronous operations are suffixed with Async, in the class name. The
request building should be transferrable.
For read calls, the AsyncReadPager exists as a convenience for paging.
For write calls, the AsyncWriteHandler exists as a convenience for handling
success and failure cases.
In either class, you can override the onError method to handle exception
cases.
BrontoApiAsync ClientAsync client = new BrontoClientAsync(apiToken, executor);
ContactOperationsAsync contactOps = new ContactOperationsAsync(client);
Calendar createdThreshold = Calendar.getInstance();
createdThreshold.add(Calendar.DATE, -7);
final ContactReadRequest readContacts = new ContactReadRequest()
.withStatus(ContactStatus.TRANSACTIONAL)
.withCreated(FilterOperator.AFTER, createdThreshold.getTime())
.withListId(listId);
contactOps.read(readContacts, new AsyncReadPager<ContactObject>() {
@Override
public void readObjects(List<ContactObject> contacts) {
for (ContactObject contact : contacts) {
System.out.println("Contact with email: " + contact.getEmail());
}
}
});contactOps.addOrUpdate(Arrays.asList(contact), new AsyncWriteHandler() {
@Override
public void onSuccessItems(List<ResultItem> results) {
for (ResultItem result : results) {
System.out.println("Added/Updated contact with id: " + result.getId());
}
}
@Override
public void onErrorItems(List<ResultItem> results) {
for (ResultItem result : results) {
System.err.println(result.getErrorString());
}
}
});import com.fasterxml.jackson.databind.ObjectMapper;
ObjectMapper mapper = new ObjectMapper();
Future<String> json = fieldOps.get(read, new AsyncHandler<FieldObject, String>() {
@Override
public String onSuccess(FieldObject field) {
return mapper.writeValue(field);
}
@Override
public void onError(Exception e) {
System.err.println(e.getMessage());
}
});
System.out.println(json.get());ObjectOperationsAsync<ConversionObject> conversionOps =
client.transportAsync(ConversionObject.class);
ConversionReadRequest conversions = new ConversionReadRequest();
for (ConversionObject conversion : conversionOps.readAll(conversions)) {
System.out.println(String.format("Conversion id: %s (email: %s) "
+ "- item = %s", conversion.getId(), conversion.getEmail(),
conversion.getItem()));
}