Skip to content

Commit 73a40a8

Browse files
authored
Merge pull request #153 from leandroshp/master
Add support to Email Channel on Verify
2 parents d6f0a1a + 79aa0fe commit 73a40a8

File tree

7 files changed

+136
-47
lines changed

7 files changed

+136
-47
lines changed

api/src/main/java/com/messagebird/MessageBirdClient.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.messagebird.objects.PurchasedNumbersResponse;
2828
import com.messagebird.objects.PurchasedNumbersFilter;
2929
import com.messagebird.objects.Verify;
30+
import com.messagebird.objects.VerifyMessage;
3031
import com.messagebird.objects.VerifyRequest;
3132
import com.messagebird.objects.VoiceMessage;
3233
import com.messagebird.objects.VoiceMessageList;
@@ -111,6 +112,7 @@ public class MessageBirdClient {
111112
private static final String LOOKUPPATH = "/lookup";
112113
private static final String MESSAGESPATH = "/messages";
113114
private static final String VERIFYPATH = "/verify";
115+
private static final String VERIFYEMAILPATH = "/verify/messages/email";
114116
private static final String VOICEMESSAGESPATH = "/voicemessages";
115117
private static final String CONVERSATION_PATH = "/conversations";
116118
private static final String CONVERSATION_SEND_PATH = "/send";
@@ -493,6 +495,20 @@ Verify getVerifyObject(String id) throws NotFoundException, GeneralException, Un
493495
return messageBirdService.requestByID(VERIFYPATH, id, Verify.class);
494496
}
495497

498+
/**
499+
* @param id id is for the email message part of a verify object
500+
* @return Verify object
501+
* @throws NotFoundException if id is not found
502+
* @throws UnauthorizedException if client is unauthorized
503+
* @throws GeneralException general exception
504+
*/
505+
public VerifyMessage getVerifyEmailMessage(String messageId) throws UnauthorizedException, GeneralException, NotFoundException {
506+
if (messageId == null || messageId.isEmpty()) {
507+
throw new IllegalArgumentException("ID cannot be empty for verify email message");
508+
}
509+
return messageBirdService.requestByID(VERIFYEMAILPATH, messageId, VerifyMessage.class);
510+
}
511+
496512
/**
497513
* @param id id for deleting verify object
498514
* @throws NotFoundException if id is not found
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.messagebird.objects;
2+
3+
import java.io.Serializable;
4+
5+
/**
6+
* Created by leandro.pinto on 22/06/15.
7+
*/
8+
public class VerifyMessage implements Serializable {
9+
10+
private String id;
11+
private String status;
12+
13+
public String getId() {
14+
return id;
15+
}
16+
17+
public void setId(String id) {
18+
this.id = id;
19+
}
20+
21+
public String getStatus() {
22+
return status;
23+
}
24+
25+
public void setStatus(String status) {
26+
this.status = status;
27+
}
28+
29+
public String toString() {
30+
return "VerifyMessage {" + " " +
31+
"id=" + this.id + " " +
32+
"status=" + this.status + " " +
33+
"}";
34+
}
35+
}

api/src/main/java/com/messagebird/objects/VerifyRequest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class VerifyRequest implements Serializable {
1717
private Integer tokenLength;
1818
private Gender voice;
1919
private Language language;
20+
private String subject;
2021

2122
public VerifyRequest(String recipient) {
2223
this.recipient = recipient;
@@ -54,6 +55,10 @@ public void setType(VerifyType type) {
5455
this.type = type;
5556
}
5657

58+
public void setType(String type) {
59+
this.type = VerifyType.valueOf(type.toUpperCase());
60+
}
61+
5762
/**
5863
* The datacoding used by the template.
5964
*
@@ -112,4 +117,11 @@ public void setLanguage(Language language) {
112117
this.language = language;
113118
}
114119

120+
public void setSubject(String subject) {
121+
this.subject = subject;
122+
}
123+
124+
public String getSubject() {
125+
return subject;
126+
}
115127
}

api/src/main/java/com/messagebird/objects/VerifyType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ public enum VerifyType {
99

1010
FLASH("flash"),
1111
SMS("sms"),
12-
TTS("tts");
12+
TTS("tts"),
13+
EMAIL("email");
1314

1415
final String value;
1516

api/src/test/java/com/messagebird/VerifyTest.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212

1313
public class VerifyTest {
1414

15-
private static final String VERIFY_SMS_RESPONSE = "{\"id\": \"verify-id-sms\",\"href\": \"https://rest.messagebird.com/verify/verify-id-sms\",\"recipient\": 31612345678,\"reference\": null,\"messages\": {\"href\": \"https://rest.messagebird.com/messages/5958c0d5e2df41de8154e5e88bfeb5bc\"},\"status\": \"sent\",\"createdDatetime\": \"2018-09-25T14:38:12+00:00\",\"validUntilDatetime\": \"2018-09-25T14:38:42+00:00\"}";
16-
private static final String VERIFY_TTS_RESPONSE = "{\"id\": \"verify-id-tts\",\"href\": \"https://rest.messagebird.com/verify/verify-id-tts\",\"recipient\": 31612345678,\"reference\": null,\"messages\": {\"href\": \"https://rest.messagebird.com/voicemessages/c043ab473f8e4f2590ab9a16d25f2899\"},\"status\": \"sent\",\"createdDatetime\": \"2018-09-25T14:35:10+00:00\",\"validUntilDatetime\": \"2018-09-25T14:35:40+00:00\"}";
15+
private static final String VERIFY_SMS_RESPONSE = "{\"id\": \"verify-id-sms\",\"href\": \"https://rest.messagebird.com/verify/verify-id-sms\",\"recipient\": 31612345678,\"reference\": null,\"messages\": {\"href\": \"https://rest.messagebird.com/messages/5958c0d5e2df41de8154e5e88bfeb5bc\"},\"status\": \"sent\",\"createdDatetime\": \"2018-09-25T14:38:12+00:00\",\"validUntilDatetime\": \"2018-09-25T14:38:42+00:00\"}";
16+
private static final String VERIFY_TTS_RESPONSE = "{\"id\": \"verify-id-tts\",\"href\": \"https://rest.messagebird.com/verify/verify-id-tts\",\"recipient\": 31612345678,\"reference\": null,\"messages\": {\"href\": \"https://rest.messagebird.com/voicemessages/c043ab473f8e4f2590ab9a16d25f2899\"},\"status\": \"sent\",\"createdDatetime\": \"2018-09-25T14:35:10+00:00\",\"validUntilDatetime\": \"2018-09-25T14:35:40+00:00\"}";
17+
private static final String VERIFY_EMAIL_RESPONSE = "{\"id\": \"verify-id-email\",\"href\": \"https://rest.messagebird.com/verify/verify-id-email\",\"recipient\": \"[email protected]\",\"reference\": null,\"messages\": {\"href\": \"https://rest.messagebird.com/verify/messages/email/c043ab473f8e4f2590ab9a16d25f2899\"},\"status\": \"sent\",\"createdDatetime\": \"2018-09-25T14:35:10+00:00\",\"validUntilDatetime\": \"2018-09-25T14:35:40+00:00\"}";
1718

1819
@Test
1920
public void testSendVerifyTokenSms() throws GeneralException, UnauthorizedException {
@@ -47,11 +48,28 @@ public void testSendVerifyTokenTts() throws GeneralException, UnauthorizedExcept
4748
assertEquals("verify-id-tts", verify.getId());
4849
}
4950

51+
@Test
52+
public void testSendVerifyTokenEmail() throws GeneralException, UnauthorizedException {
53+
VerifyRequest verifyRequest = new VerifyRequest("[email protected]");
54+
verifyRequest.setType(VerifyType.EMAIL);
55+
56+
MessageBirdService messageBirdService = SpyService
57+
.expects("POST", "verify", verifyRequest)
58+
.withRestAPIBaseURL()
59+
.andReturns(new APIResponse(VERIFY_EMAIL_RESPONSE, 200));
60+
MessageBirdClient messageBirdClient = new MessageBirdClient(messageBirdService);
61+
62+
Verify verify = messageBirdClient.sendVerifyToken(verifyRequest);
63+
64+
assertEquals("verify-id-email", verify.getId());
65+
}
66+
5067
@Test
5168
public void testVerifyTypeValue() {
5269
// Important for generating proper JSON payloads...
5370
assertEquals("flash", VerifyType.FLASH.getValue());
5471
assertEquals("sms", VerifyType.SMS.getValue());
5572
assertEquals("tts", VerifyType.TTS.getValue());
73+
assertEquals("email", VerifyType.EMAIL.getValue());
5674
}
5775
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import com.messagebird.MessageBirdClient;
2+
import com.messagebird.MessageBirdService;
3+
import com.messagebird.MessageBirdServiceImpl;
4+
import com.messagebird.exceptions.GeneralException;
5+
import com.messagebird.exceptions.NotFoundException;
6+
import com.messagebird.exceptions.UnauthorizedException;
7+
import com.messagebird.objects.Verify;
8+
import com.messagebird.objects.VerifyMessage;
9+
import com.messagebird.objects.VerifyRequest;
10+
11+
/**
12+
* Created by leandro.pinto on 23/06/21.
13+
*/
14+
public class ExampleVerifyEmail {
15+
16+
public static void main(String[] args) throws UnauthorizedException, GeneralException, NotFoundException {
17+
18+
final String ACCESS_KEY = args[0];
19+
final String METHOD = args[1];
20+
21+
final MessageBirdService wsr = new MessageBirdServiceImpl(ACCESS_KEY);
22+
final MessageBirdClient messageBirdClient = new MessageBirdClient(wsr);
23+
Verify verify = null;
24+
25+
switch (METHOD) {
26+
case "send":
27+
VerifyRequest request = new VerifyRequest("<ADD RECIPIENT>");
28+
request.setType("email");
29+
request.setOriginator("<ADD ORIGINATOR>");
30+
request.setSubject("<ADD SUBJECT>");
31+
request.setTimeout(300);
32+
33+
verify = messageBirdClient.sendVerifyToken(request);
34+
System.out.println(verify.toString());
35+
36+
break;
37+
case "verify":
38+
final String VERIFY_ID = args[2];
39+
final String TOKEN = args[3];
40+
41+
verify = messageBirdClient.verifyToken(VERIFY_ID, TOKEN);
42+
System.out.println(verify.toString());
43+
break;
44+
case "view":
45+
final String MESSAGE_ID = args[2];
46+
VerifyMessage verifyMessage = messageBirdClient.getVerifyEmailMessage(MESSAGE_ID);
47+
System.out.println(verifyMessage.toString());
48+
break;
49+
}
50+
}
51+
}

examples/src/main/java/ExampleVerifyToken.java

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)