|
| 1 | +package software.xdev; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import java.util.Optional; |
| 5 | +import java.util.Scanner; |
| 6 | +import java.util.function.Function; |
| 7 | + |
| 8 | +import org.apache.hc.core5.http.HttpStatus; |
| 9 | +import org.slf4j.Logger; |
| 10 | +import org.slf4j.LoggerFactory; |
| 11 | + |
| 12 | +import software.xdev.brevo.api.ContactsApi; |
| 13 | +import software.xdev.brevo.client.ApiClient; |
| 14 | +import software.xdev.brevo.client.ApiException; |
| 15 | +import software.xdev.brevo.model.CreateContact; |
| 16 | +import software.xdev.brevo.model.CreateUpdateContactModel; |
| 17 | +import software.xdev.brevo.model.GetContactInfoIdentifierParameter; |
| 18 | +import software.xdev.brevo.model.GetExtendedContactDetails; |
| 19 | +import software.xdev.brevo.model.UpdateContact; |
| 20 | + |
| 21 | + |
| 22 | +public final class Application |
| 23 | +{ |
| 24 | + private static final Logger LOG = LoggerFactory.getLogger(Application.class); |
| 25 | + |
| 26 | + static Scanner scanner = new Scanner(System.in); |
| 27 | + |
| 28 | + // Tries to add an email/contact to a list |
| 29 | + public static void main(final String[] args) |
| 30 | + { |
| 31 | + final ApiClient apiClient = new ApiClient(); |
| 32 | + apiClient.setApiKey(getProperty("API-KEY", Function.identity())); |
| 33 | + apiClient.setUserAgent("Brevo Java Client"); |
| 34 | + |
| 35 | + final long listId = getProperty("LIST-ID", Long::parseLong); |
| 36 | + final String email = getProperty("EMAIL-FOR-LIST", Function.identity()); |
| 37 | + |
| 38 | + final ContactsApi contactsApi = new ContactsApi(apiClient); |
| 39 | + try |
| 40 | + { |
| 41 | + final GetContactInfoIdentifierParameter identifier = |
| 42 | + new GetContactInfoIdentifierStringParameter(email); |
| 43 | + final GetExtendedContactDetails contactInfo = contactsApi.getContactInfo( |
| 44 | + identifier, |
| 45 | + null, |
| 46 | + null); |
| 47 | + LOG.info("Got contact[email={},listIds={}]", contactInfo.getEmail(), contactInfo.getListIds()); |
| 48 | + if(!contactInfo.getListIds().contains(listId)) |
| 49 | + { |
| 50 | + contactsApi.updateContact(identifier, new UpdateContact().listIds(List.of(listId))); |
| 51 | + LOG.info("Updated contact to include listId={}", listId); |
| 52 | + } |
| 53 | + } |
| 54 | + catch(final ApiException ex) |
| 55 | + { |
| 56 | + if(ex.getCode() == HttpStatus.SC_NOT_FOUND) |
| 57 | + { |
| 58 | + final CreateUpdateContactModel createdContact = contactsApi.createContact(new CreateContact() |
| 59 | + .email(email) |
| 60 | + .listIds(List.of(listId))); |
| 61 | + LOG.info("Created contact[id={}, email={},listIds={}]", createdContact.getId(), email, listId); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private static <T> T getProperty(final String identifier, final Function<String, T> caster) |
| 67 | + { |
| 68 | + String value = Optional.ofNullable(System.getenv(identifier)) |
| 69 | + .orElseGet(() -> System.getProperty(identifier)); |
| 70 | + if(value == null) |
| 71 | + { |
| 72 | + LOG.error("Required {} not set in environment variables or system properties", identifier); |
| 73 | + |
| 74 | + LOG.info("Please provide {} over console:", identifier); |
| 75 | + value = scanner.nextLine(); |
| 76 | + |
| 77 | + if(value == null || value.isBlank()) |
| 78 | + { |
| 79 | + LOG.error("Invalid input; Aborting"); |
| 80 | + System.exit(1); |
| 81 | + } |
| 82 | + } |
| 83 | + return caster.apply(value); |
| 84 | + } |
| 85 | + |
| 86 | + private Application() |
| 87 | + { |
| 88 | + } |
| 89 | + |
| 90 | + @SuppressWarnings("java:S2160") |
| 91 | + public static class GetContactInfoIdentifierStringParameter extends GetContactInfoIdentifierParameter |
| 92 | + { |
| 93 | + private final String value; |
| 94 | + |
| 95 | + public GetContactInfoIdentifierStringParameter(final String value) |
| 96 | + { |
| 97 | + this.value = value; |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public String toString() |
| 102 | + { |
| 103 | + return this.value; |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments