From bbd299c9fba4781e80dc20af06ae7c16f5c8a594 Mon Sep 17 00:00:00 2001 From: Timothy Lim Date: Fri, 8 Jun 2018 22:19:40 +0800 Subject: [PATCH] Add deletion by ID and User ID --- README.md | 19 ++++++++++++ .../main/java/io/intercom/api/Contact.java | 31 +++++++++++++++++-- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 84db5314..e9e37ae5 100644 --- a/README.md +++ b/README.md @@ -199,6 +199,14 @@ contact = Contact.findByUserID("e1a7d875-d83a-46f7-86f4-73be98a98584"); contact.setName("Stitch Hessian"); Contact updated = Contact.update(contact); +// Update a contact by ID +Contact contact = new Contact().setID("541a144b201ebf2ec5000002").setName("Stitch Hessian"); +Contact updated = Contact.update(contact); + +// Update a contact by User ID +Contact contact = new Contact().setUserID("e1a7d875-d83a-46f7-86f4-73be98a98584").setName("Stitch Hessian"); +Contact updated = Contact.update(contact); + // Read a contact list by email ContactCollection contacts = Contact.listByEmail("jubal@serenity.io"); while(contacts.hasNext()) { @@ -216,10 +224,21 @@ ScrollableContactCollection contactsScroll = Contact.scroll(); List contacts = contactsScroll.getPage(); contactsScroll = contactsScroll.scroll(); +// List contacts (sorting) +Map params = Maps.newHashMap(); +params.put("sort", "created_at"); +params.put("order", "asc"); +ContactCollection contacts = Contact.list(params); // Remove a contact Contact.delete(contact); +// Remove a contact by id +Contact.delete(contact.getID()); + +// Remove a contact by user_id +Contact.deleteByUserID(contact.getUserID()); + // Convert a contact User converted = Contact.convert(contact, user); ``` diff --git a/intercom-java/src/main/java/io/intercom/api/Contact.java b/intercom-java/src/main/java/io/intercom/api/Contact.java index df2d66e9..392e6488 100644 --- a/intercom-java/src/main/java/io/intercom/api/Contact.java +++ b/intercom-java/src/main/java/io/intercom/api/Contact.java @@ -6,6 +6,7 @@ import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonProperty; import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Strings; import com.google.common.collect.Maps; import java.net.URI; @@ -66,7 +67,27 @@ public static Contact update(Contact c) public static Contact delete(Contact c) throws AuthorizationException, ClientException, ServerException, InvalidException, RateLimitException { - return DataResource.delete(c.getID(), "contacts", Contact.class); + if(!Strings.isNullOrEmpty(c.getID())) { + return delete(c.getID()); + } + else if(!Strings.isNullOrEmpty(c.getUserID())) { + return deleteByUserID(c.getUserID()); + } + else { + throw new InvalidException("to delete a contact you must provide a id or user_id value"); + } + } + + public static Contact delete(String id) + throws AuthorizationException, ClientException, ServerException, InvalidException, RateLimitException { + return DataResource.delete(id, "contacts", Contact.class); + } + + public static Contact deleteByUserID(String user_id) + throws AuthorizationException, ClientException, ServerException, InvalidException, RateLimitException { + Map params = new HashMap(); + params.put("user_id", user_id); + return DataResource.delete(params, "contacts", Contact.class); } public static User convert(Contact c, User u) @@ -416,8 +437,7 @@ public String getID() { return id; } - @VisibleForTesting - Contact setID(String id) { + public Contact setID(String id) { this.id = id; return this; } @@ -457,6 +477,11 @@ public String getUserID() { return userID; } + public Contact setUserID(String userID) { + this.userID = userID; + return this; + } + public Avatar getAvatar() { return avatar; }