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