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